小编Rai*_*ain的帖子

struct typedef中的声明规则

我正在阅读'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"不一样吗?

c struct typedef declaration

6
推荐指数
1
解决办法
3215
查看次数

为什么这个局部变量可以在函数中保持其值

可能的重复:
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)

我只是不明白为什么......有人有一些想法吗?非常感谢。

python

2
推荐指数
1
解决办法
1761
查看次数

标签 统计

c ×1

declaration ×1

python ×1

struct ×1

typedef ×1