C STRUCT strcopy简易初学者程序-can';无法修复编译错误
几天前我刚开始用C语言编程,现在我正在学习结构 我有这个程序,但不幸的是,由于某种原因,我没有编译。我花了很多时间试图修复它,但我似乎找不到任何问题 以下是我得到的编译错误:C STRUCT strcopy简易初学者程序-can';无法修复编译错误,c,string,struct,compilation,compiler-errors,C,String,Struct,Compilation,Compiler Errors,几天前我刚开始用C语言编程,现在我正在学习结构 我有这个程序,但不幸的是,由于某种原因,我没有编译。我花了很多时间试图修复它,但我似乎找不到任何问题 以下是我得到的编译错误: arrays.c:21: error: two or more data types in declaration specifiers arrays.c: In function ‘insert’: arrays.c:26: error: incompatible type for argument 1 of ‘strc
arrays.c:21: error: two or more data types in declaration specifiers
arrays.c: In function ‘insert’:
arrays.c:26: error: incompatible type for argument 1 of ‘strcpy’
/usr/include/string.h:128: note: expected ‘char * restrict’ but argument is of type ‘struct person’
arrays.c:32: warning: no return statement in function returning non-void
arrays.c: In function ‘main’:
arrays.c:46: error: expected ‘;’ before ‘)’ token
arrays.c:46: error: expected ‘;’ before ‘)’ token
arrays.c:46: error: expected statement before ‘)’ token
我不确定我的代码出了什么问题,我的主函数(第46行)甚至出现了错误。
以下是我的完整程序代码:
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
/* these arrays are just used to give the parameters to 'insert',
to create the 'people' array */
#define HOW_MANY 7
char *names[HOW_MANY]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim",
"Harriet"};
int ages[HOW_MANY]= {22, 24, 106, 6, 18, 32, 24};
/* declare your struct for a person here */
struct person
{
char name [32];
int age;
}
static void insert (struct person people[], char *name, int age)
{
static int nextfreeplace = 0;
static int nextinsert = 0;
/* put name and age into the next free place in the array parameter here */
strcpy(people[nextfreeplace],name);
people[nextfreeplace].age = age;
/* modify nextfreeplace here */
nextfreeplace = nextfreeplace + 1;
nextinsert = nextinsert + 1;
}
int main(int argc, char **argv) {
/* declare the people array here */
struct person people[12];
int i;
for (i =0; i < HOW_MANY; i++)
{
insert (people, names[i], ages[i]);
}
/* print the people array here*/
for (i =0; i < HOW_MANY); i++)
{
printf("%s\n", people[i].name);
printf("%d\n", people[i].age);
}
return 0;
}
#包括
#包括
#包括
/*这些数组仅用于为“insert”提供参数,
创建“人员”数组的步骤*/
#定义有多少个7
char*姓名[人数]={“西蒙”、“苏西”、“阿尔弗雷德”、“奇普”、“约翰”、“蒂姆”,
“哈丽特”};
整数[多少]={22,24,106,6,18,32,24};
/*在此处为某人声明您的结构*/
结构人
{
字符名[32];
智力年龄;
}
静态void insert(struct person people[],char*name,int age)
{
静态int-nextfreeplace=0;
静态int nextinsert=0;
/*将name和age放在数组参数的下一个空闲位置*/
strcpy(人员[nextfreeplace],姓名);
人[nextfreeplace]。年龄=年龄;
/*在此处修改nextfreeplace*/
nextfreeplace=nextfreeplace+1;
nextinsert=nextinsert+1;
}
int main(int argc,字符**argv){
/*在这里声明人员数组*/
结构人[12];
int i;
对于(i=0;i<多少;i++)
{
插入(人员、姓名[i]、年龄[i]);
}
/*在此处打印人员数组*/
对于(i=0;i<多少);i++)
{
printf(“%s\n”,人员[i].name);
printf(“%d\n”,人[i]。年龄);
}
返回0;
}
像这样
char *names[]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim",
"Harriet"};
int ages[]= {22, 24, 106, 6, 18, 32, 24};
一个想法是这样做:
char *names[]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim",
"Harriet", 0}; //notice the 0
for(int i=0;names[i];i++)
{
printf("%s",names[i]);
}
但我不确定这是否是最好的实现。- 第19行,应为“;”后结构
- 第46行:应为“;”在“for”语句说明符中,应为“;”在表达式之后,for循环的主体为空
- 第26行将“struct person”传递给不兼容类型“const void*”的参数
后者更复杂。在
结构声明后需要一个分号:
struct person
{
char name [32];
int age;
}; /* <-- here */
在for
循环中有一个杂散的)
:
for (i =0; i < HOW_MANY); i++)
for(i=0;i<多少);(i++)
。。。应该是:
for (i =0; i < HOW_MANY; i++)
for(i=0;i
strcpy(人[nextfreeplace].name,name)
会解决你这个问题中的主要问题我很困惑。你为什么漏掉了多少?我需要用它来定义我使用的名字和年龄的数量,在我的例子中是“7”。此外,这并没有修复我的任何错误。当您枚举数组中的元素时,不能包括数组的长度,编译器将自行计算。我相信您没有访问该值的权限,要解决这个问题,您需要在列表末尾添加一个NULL,以便知道何时停止。有人应该纠正我,指定元素的数量不是错误的,除非您提供的初始值设定项多于元素的数量。如果你能提前知道元素的数量,这比在末尾加0要好。@titus:你错了。您当然可以在初始化时指定数组的大小。有没有办法知道枚举列表中实际有多少元素?谢谢!这解决了我在程序中遇到的问题。你的回答真的很好——简短但直截了当,并且足够明确,让我能够理解我的错误!再次非常感谢:)@Sarah97:很高兴能帮忙!事实上,第26行没有那么复杂。它只需要结构的字段名+1无论如何,请停止从问题中删除代码。我已经回滚了您的更改两次。代码与您的问题和您收到的答案100%相关。当你删除它时,你似乎试图对你的教授隐藏你的问题?
for (i =0; i < HOW_MANY; i++)