在下面的程序中,我尝试将结构传递给函数。但我得到错误,我不明白为什么。我在这个程序中犯了什么错误?
我gcc
用来编译这个c
程序。
#include <stdio.h>
struct tester {
int x;
int *ptr;
};
void function(tester t);
int main() {
tester t;
t.x = 10;
t.ptr = & t.x;
function(t);
}
void function(tester t) {
printf("%d\n%p\n",t.x,t.ptr);
}
错误:
gcc tester.c -o tester
tester.c:8:15: error: unknown type name ‘tester’
tester.c: In function ‘main’:
tester.c:12:2: error: unknown type name ‘tester’
tester.c:13:3: error: request for member ‘x’ in something not a structure or union
tester.c:14:3: error: request for member ‘ptr’ in something not a structure or union
tester.c:14:13: error: request for member ‘x’ in something not a structure or union
tester.c: At top level:
tester.c:18:15: error: unknown type name ‘tester’
注意: 如果我printf
用cout
和stdio
替换iostream
并将扩展名命名为.cpp
(!),我不会收到任何错误。这是为什么 ?难怪我用它编译它g++