问题描述
如果我有静态变量:
#mainmenu loop
def main():
optcmd = 0
print("\n\nwelcome to the store. what do you wanna do?"
"\n here are all the commands. enter a number to output. enjoy!"
"\n 1: view your shopping list"
"\n 2: add to the shopping list"
"\n 3: delete items from the shopping list"
"\n 4: clear the shopping list"
"\n 5: go to isle 1"
"\n 6: go to isle 2"
"\n 7: go to isle 3"
"\n 8: go to isle 4"
"\n 9: checkout"
"\n 10: leave the store")
while not optcmd:
optcmd = int(input('>> '))
if optcmd<=0 or optcmd>10:
print('invalid code...\nPlease re-enter code...')
optcmd=0
return optcmd
#functions for each command
#view list
def viewList(shoplist):
if shoplist==[]:
print('the list is empty.')
else:
print(shoplist)
#add
def addList(shoplist):
item = input("what would you like to add? ")
shoplist.append(item)
print(item,' has been added.')
#delete
def removeList(shoplist):
item = input("what would you like to remove? ")
shoplist.remove(item)
print(item,' has been deleted.')
#shopping list
shoplist = []
selected = main()
#selection item
while selected:
#if/else statements for selection
if selected == 1:
viewList(shoplist)
selected = main()
elif selected == 2:
addList(shoplist)
selected = main()
elif selected == 3:
removeList(shoplist)
selected = main()
elif selected == 4:
pass
selected=''
elif selected == 5:
pass
selected=''
elif selected == 6:
pass
selected=''
elif selected == 7:
pass
selected=''
elif selected == 8:
pass
selected=''
elif selected == 9:
pass
selected=''
elif selected == 10:
print('thanks for visiting :)')
selected=''
如果指针看起来像这样,我希望有一个指针指向该变量:
static int a;
如果我将此f返回到带有静态类型为int *类型的指针的赋值语句的函数调用中,那么该变量可以在该函数中访问吗?
也:
static int* f;
f=&a;
解决方法
int a;
static int* f;
f=&a; // does this mean now a is a static variable and it will be retained until the program ends?
“ [D]这意味着现在a是一个静态变量吗?”
不,不是。 static
属性仅适用于您明确定义为static
的变量。在上面的示例中,只有f
是static
。 a
的生命周期将在其周围范围结束时结束,从而使指向该点的任何指针在该时间点都是无效的。
static int b;
int* c;
c=&n; // is this possible?
“ [这可能吗?”
是的,很好。局部静态变量的生命周期是程序的整个生命周期。全局变量(静态或非静态)也具有完整程序的生命周期。这意味着指向它们的指针将始终保持有效。