C语言 声明struct类型,将值插入该类型的数组中,并打印出数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12989391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:10:33  来源:igfitidea点击:

Declare struct type, insert values into array of the type, and print out array

c

提问by Steven Liu

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. 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

在这个程序中,我想定义一个名为 person 的结构体,以及一个用于将元素插入到声明为 person 类型的数组中未使用空间的插入函数。最后,我想将结果打印为标准输出。任何人都可以给我一个提示以纠正任何错误吗?干杯

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

回答by Tuntuni

The first problem is your struct person. You're declaring name as a charwhile it should be a char* (pointer)or a char[] (array).

第一个问题是你的结构人。您将 name 声明为char而它应该是char* (pointer)char[] (array)

typedef struct 
{
    char *name; //or char name[100];
    int age;
}
person;

Next, your insertfunctionhas incorrect arguments. You don't want an array of persons (you could do it but this is simpler), you want a pointer to a person structso 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

回答by md5

You have a problem with the parameter p.

您的参数有问题p

static void insert (p[], char *name, int ages)

You've forgotten its type (person). Then you redeclare it; the following instruction is invalid:

你忘记了它的类型 ( person)。然后你重新声明它;以下指令无效:

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]);