だから私は数日前に 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 ‘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;
}