编译错误:与要求不是结构或联合错误 [英] Compile error: with request not something structure or union error

查看:139
本文介绍了编译错误:与要求不是结构或联合错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个程序中,我试图创建一个结构,然后初始化与结构类型a数组,把名字和年龄到阵列中,并打印出结果。然而,当我编译文件,它说,姓名和年龄是不是结构或联合。任何人都可以发现我的错误,请,谢谢

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;/ *这些阵列只是用来给'插入'的参数,
   创造了人的数组* /
字符*名称[7] = {西蒙,苏西,阿尔弗雷德,芯片,约翰,添
          哈丽雅特};
INT年龄[7] = {22,24,106,6,18,32,24};
/ *声明你的结构,一个人在这里* /
typedef结构{
  字符*名称;
  INT年龄;
}人;静态无效插入(人** P,字符* S,INT N){   * P =的malloc(sizeof的(人));  静态INT nextfreeplace = 0;  / *把名字和年龄成数组参数的下一个自由的地方在这里* /
    (* P) - >名称=秒;
    (* P) - GT;年龄= N;  / *修改nextfreeplace这里* /
  nextfreeplace ++;
  }INT主(INT ARGC,字符** argv的){  / *在这里声明人数组* /
   人* P [7];   //成员和年龄插入unusage阵列。
  的for(int i = 0;我7;;我++){
    插入(安培; P [I],名称[I],年龄[I]);
    P [I] = P [I + 1];
  }  / *打印人民阵列这里* /
  的for(int i = 0;我7;;我++){
    的printf(名称:%s,年龄:%I \\ N,P [I] .names,P [I] .ages);
  }}


解决方案

在声明 P 为指针数组结构。在printf的行,你解引用一次 P P [I] ,但p是仍然指向一个结构,你想与来访问它的字段 - >

 的for(int i = 0;我7;;我++){
  的printf(名称:%s,年龄:%I \\ N,P [I] - >名称中的[I] - >年龄);
}

和你在增加 I 你的循环,你不需要移动你的P [I]指针,删除 P [I] = p [I + 1]

 的for(int i = 0;我7;;我++){
  插入(安培; P [I],名称[I],年龄[I]);
}

In this program, I am trying to create a struct, and then initialise a array with that struct type, put name and ages into the array, and print out the results. However, when I compile the file, it says that "names" and ages" are not something structure or union. Can anyone spot my errors please. Thank you

#include <stdio.h>
#include <stdlib.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 *names;
  int ages; 
}  person;

static void insert (person **p, char *s, int n) {

   *p = malloc(sizeof(person));

  static int nextfreeplace = 0;

  /* put name and age into the next free place in the array parameter here */
    (*p)->names=s;
    (*p)->ages=n;

  /* 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("name: %s, age:%i\n", p[i].names, p[i].ages);
  }

}

解决方案

you declare p as an array of pointer to structure. On printf line, you dereference once p with p[i], but p is still a pointer to a structure, you want to access its fields with ->

for (int i=0; i < 7; i++) {
  printf("name: %s, age:%i\n", p[i]->names, p[i]->ages);
}

And as you increment i in your for loop, you dont need to move your p[i] pointer, remove, p[i] = p[i + 1]

for (int i=0; i < 7; i++) {
  insert (&p[i], names[i], ages[i]);
}

这篇关于编译错误:与要求不是结构或联合错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆