2014-09-19 228 views
-4
#include<stdio.h> 
struct s_{ 
     int b; 
}s; 

int func1(s** ss){ 
     *ss->a = 10; 
} 
int func(s* t){ 
     func1(&t); 
} 
int main(){ 
     s a; 
     func(&a); 
     printf("\n a : %d \n",a.b); 
     return 0; 
} 

尝试示例程序并获得o/p错误。error'expected')'before'*'token

O/P:

[[email protected]]# gcc d.c 
d.c:6: error: expected ‘)’ before ‘*’ token 
d.c:9: error: expected ‘)’ before ‘*’ token 
d.c: In function ‘main’: 
d.c:13: error: expected ‘;’ before ‘a’ 
d.c:14: error: ‘a’ undeclared (first use in this function) 
d.c:14: error: (Each undeclared identifier is reported only once 
d.c:14: error: for each function it appears in.) 
+3

你忘了''struct'前typedef'关键字? – 2014-09-19 11:39:36

+6

有一个错误,请阅读[运算符优先顺序](http://en.cppreference.com/w/c/language/operator_precedence)。对于另一个错误,名为'a'的结构中没有成员。对于另一个*错误,'s'是一个变量。 – 2014-09-19 11:40:11

回答

6
  1. 您省略了typedef,您需要声明您的结构别名s
  2. 该结构的成员是b而不是a
  3. 您无法从您的功能中返回任何内容。这些应该是无效的功能。
  4. 您需要在附近左右。
  5. 无参数mainCint main(void)

 

#include <stdio.h> 

typedef struct s_{ 
     int b; 
}s; 

void func1(s** ss){ 
     (*ss)->b = 10; 
} 

void func(s* t){ 
     func1(&t); 
} 

int main(void) 
{ 
     s a; 
     func(&a); 
     printf("\n a.b : %d \n", a.b); 
     return 0; 
} 
+0

6.为清晰起见,您应该将main定义为int main(void)。 – Jens 2014-09-19 11:44:07

3

看你的代码后,很明显,你错过了typedef关键字struct

struct s_{ 
     int b; 
}s; 

前应

typedef struct s_{ 
     int b; 
}s; 

*ss->a = 10; //wrong. operator precedence problem 
      // `->` operator have higher precedence than `*` 

有没有会员名称a。它应该是

(*ss)->b = 10; 
1

如图所示sstruct s_类型的一个对象。你不能把它用作函数原型的类型。你是否想要引入一个类型别名typedef