我正在阅读'C编程语言',并遇到了关于struct的 typedef的问题.代码是这样的:
typedef struct tnode *Treeptr;
typedef struct tnode { /* the tree node: */
char *word; /* points to the text */
int count; /* number of occurrences */
struct tnode *left; /* left child */
struct tnode *right; /* right child */
} Treenode;
Run Code Online (Sandbox Code Playgroud)
到我们写的时候
typedef struct tnode *Treeptr;
Run Code Online (Sandbox Code Playgroud)
tnode仍未声明,但我们没有得到任何编译错误,但是当我们将上面的语句更改为:
typedef Treenode *Treeptr;
Run Code Online (Sandbox Code Playgroud)
我们得到编译错误:
error: parse error before '*' token
warning: data definition has no type or storage class
Run Code Online (Sandbox Code Playgroud)
是什么导致了差异?"struct tnode"与"Treenode"不一样吗?
可能的重复:
Python 中的“最不令人惊讶”:可变默认参数
我最近在Python中遇到了一个问题。代码:
def f(a, L=[]):
L.append(a)
return L
print f(1)
print f(2)
print f(3)
Run Code Online (Sandbox Code Playgroud)
输出将是
[1]
[1, 2]
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
但为什么函数f中的局部列表:L中的值保持不变呢?因为 L 是局部变量,所以我认为输出应该是:
[1]
[2]
[3]
Run Code Online (Sandbox Code Playgroud)
我尝试了另一种方式来实现这个功能:代码:
def f(a, L=None):
if L is None:
L = []
L.append(a)
return L
Run Code Online (Sandbox Code Playgroud)
这次,输出是:
[1]
[2]
[3]
Run Code Online (Sandbox Code Playgroud)
我只是不明白为什么......有人有一些想法吗?非常感谢。