[英]Declare struct type, insert values into array of the type, and print out array
In this program, I would like to define a struct called person, and an insert function for inserting an element into unused space in the array which is declared as type person. 在此程序中,我想定义一个称为person的结构,以及一个用于将元素插入到声明为person类型的数组的未使用空间中的插入函数。 Lastly, I would like to print out the results as standard output.
最后,我想将结果打印为标准输出。 Could anyone give me a hint for correct anything which is wrong?
谁能给我一个纠正错误的提示? Cheers
干杯
Error: 错误:
arrays.c:16:22: error: expected ')' before '[' token
arrays.c: In function 'main':
arrays.c:34:5: warning: implicit declaration of function 'insert'
arrays.c:41:5: warning: format '%s' expects type 'char *', but argument 2 has type 'char **'
Code 码
#include <stdio.h>
/* these arrays are just used to give the parameters to 'insert',
to create the 'people' array */
char *names[7]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim",
"Harriet"};
int ages[7]= {22, 24, 106, 6, 18, 32, 24};
/* declare your struct for a person here */
typedef struct{
char name;
int ages;
} person;
static void insert (p[], char *name, int ages) {
static int nextfreeplace = 0;
/* put name and age into the next free place in the array parameter here */
person p[0] = {&name, age};
/* modify nextfreeplace here */
nextfreeplace++;
}
int main(int argc, char **argv) {
/* declare the people array here */
person p[7];
//insert the members and age into the unusage array.
for (int i=0; i < 7; i++) {
insert (p[i], &names[i], ages[i]);
p[i]= p[i+1];
}
/* print the people array here*/
for (int i=0; i < 7; i++) {
printf("%s is %d years old\n", &names[i], ages[i]);
}
return 0;
}
The first problem is your struct person. 第一个问题是您的结构人。 You're declaring name as a char while it should be a char* (pointer) or a char[] (array) .
您将名称声明为char,而应为char *(指针)或char [](数组) 。
typedef struct
{
char *name; //or char name[100];
int age;
}
person;
Next, your insert function has incorrect arguments. 接下来,您的insert 函数具有错误的参数。 You don't want an array of persons (you could do it but this is simpler), you want a pointer to a person struct so you can edit it.
您不想要一个人数组(您可以这样做,但是这很简单),您想要一个指向人结构的指针以便可以对其进行编辑。
static void insert(person *p, char *name, int age)
{
p->name = name;
p->age = age;
}
Finally, this is how you would populate your array and print it out: 最后,这是填充数组并打印出来的方式:
int main()
{
//names and ages...
person people[7];
for (int i = 0; i < 7; i++)
{
insert(&people[i], names[i], ages[i]);
}
for (int i = 0; i < 7; i++)
{
printf("name: %s, age: %i\n", people[i].name, people[i].age);
}
}
Example: http://ideone.com/dzGWId . 例如: http : //ideone.com/dzGWId 。
You have a problem with the parameter p
. 您对参数
p
有问题。
static void insert (p[], char *name, int ages)
You've forgotten its type ( person
). 您忘记了它的类型(
person
)。 Then you redeclare it; 然后,您重新声明它; the following instruction is invalid:
以下指令无效:
person p[0] = {&name, age};
In the function call, you don't use an array, but a case of an array. 在函数调用中,您不使用数组,而是使用数组的情况。 So your function should be:
因此,您的功能应为:
typedef struct
{
char *name;
int ages;
} person;
static void
insert (person *p, char *s, int n)
{
p->name = s;
p->ages = n;
}
And the call: 并致电:
insert (&p[i], names[i], ages[i]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.