错误:预期的标识符或'。'标记前的'('

安格斯

交流电

#include<stdio.h>
#include "a.h"

int main(){
        s.a = 10;
        printf("\n add : %d \n",add(2,2));
        printf("\n sub : %d \n",sub(2,2));
        printf("\n struct is : %d \n",s.a);
        return 0;
}

公元前:

#include<stdio.h>
int add(int a,int b){
        return (a+b);
}
int sub(int a,int b){
        return (a-b);
}

啊:

#ifndef A_HEADER
#define A_HEADER

typedef struct _s{
        int a;
        int b;
}s;

int add(int a,int b);
int sub(int a,int b);

#endif

O / p:

[root@s new]# ls
a.c  a.h  b.c  include 

[root@s new]# gcc -c a.c -o a.o
a.c: In function ‘main’:
a.c:4: error: expected identifier or ‘(’ before ‘.’ token
a.c:7: error: expected expression before ‘s’

我收到上述错误。找不到错误的原因

保罗·R

s是一种类型-您需要声明此类型实例

int main(){
    s my_s;       // my_s is an instance of the struct type `s`
    my_s.a = 10;  // initialise the `a` element of `my_s`
    printf("\n add : %d \n",add(2,2));
    printf("\n sub : %d \n",sub(2,2));
    printf("\n struct is : %d \n",my_s.a); // <<<
    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章