gpt4 book ai didi

c - 错误: expected identifier or ‘(’ before ‘.’ token

转载 作者:行者123 更新时间:2023-11-30 20:44:55 25 4
gpt4 key购买 nike

a.c

#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;
}

b.c:

#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

输出:

[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’

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

最佳答案

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;
}

关于c - 错误: expected identifier or ‘(’ before ‘.’ token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25810858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com