简体   繁体   English

错误:结构初始化程序中的多余元素; '->' 的无效类型参数(有 'int')

[英]Errors: Excess elements in struct initializer ; invalid type argument of '->' (have 'int')

I am attempting to initialize elements 3 and 5 of my array, one inside of my main method and one outside (due to assignment requirements - already submitted this, I am just correcting to study for an exam).我正在尝试初始化我的数组的元素 3 和 5,一个在我的主要方法内部,一个在外部(由于作业要求 - 已经提交了这个,我只是为了考试而更正学习)。

However, I am facing two errors - " excess elements in struct initializer " and " invalid type argument '->' (have 'int') ".但是,我面临两个错误-“结构初始化程序中的多余元素”和“无效类型参数'->'(具有'int') ”。

在此处输入图像描述

Please see below for a snippet of the file I am trying to build.请参阅下面的我正在尝试构建的文件片段。

 10 #include <stdio.h>
 11 #include <stdlib.h>
 12 #include <string.h>
 13 #include "project.h"
 14
 15 //Create an array of size 1000 of your datatype as global variable
 16 struct PROJECT arr[1000];
 17
 18   arr[4] = {"gaston","trad",9,2020,150,'N'}; //Attempted to initialize 5th element
 19
 20 int main(int argc, char * argv[],char * envp[])
 21 {
 22   */*
 23   //Initialize 5th element of array - ignore this
 24   strcpy(arr[4].beta,"gaston");
 25   strcpy(arr[4].type,"trad");
 26   arr[4].grade=9;
 27   arr[4].year=2020;
 28   arr[4].weight=150;
 29   arr[4].sent='N';*/*
 30
 31   //Initialize 3rd element of array
 32   strcpy(arr[2]->beta,"jug");
 33   strcpy(arr[2]->type,"speed");
 34   arr[2]->grade=12;
 35   arr[2]->year=2022;
 36   arr[2]->weight=125;
 37   arr[2]->sent='Y';

I have searched both of these errors and read through other threads but don't understand the solution explanations.我已经搜索了这两个错误并阅读了其他线程,但不理解解决方案的解释。 Would someone be able to explain in simple terms what I am doing wrong here?有人可以简单地解释我在这里做错了什么吗?

You can't initialize an array like that.你不能像这样初始化一个数组。 You need to either set it like you do on lines 24 - 29 (assuming your character pointers have enough space), or if you want to do brace initialization, you need to do it for the elements before as well.您需要像在第 24 - 29 行那样设置它(假设您的字符指针有足够的空间),或者如果您想进行大括号初始化,您也需要对之前的元素进行设置。

struct PROJECT arr[1000] =
  {  NULL, NULL, 0, 0, 0, 0, // Assuming "sent" is a character.
     NULL, NULL, 0, 0, 0, 0,
     NULL, NULL, 0, 0, 0, 0,
     NULL, NULL, 0, 0, 0, 0,
     "gaston", "trad", 9, 2020, 150, 'N',
     ... //etc
  };

See: http://www.crasseux.com/books/ctutorial/Initializing-arrays.html见: http ://www.crasseux.com/books/ctutorial/Initializing-arrays.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM