簡體   English   中英

錯誤:結構初始化程序中的多余元素; '->' 的無效類型參數(有 'int')

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

我正在嘗試初始化我的數組的元素 3 和 5,一個在我的主要方法內部,一個在外部(由於作業要求 - 已經提交了這個,我只是為了考試而更正學習)。

但是,我面臨兩個錯誤-“結構初始化程序中的多余元素”和“無效類型參數'->'(具有'int') ”。

在此處輸入圖像描述

請參閱下面的我正在嘗試構建的文件片段。

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

我已經搜索了這兩個錯誤並閱讀了其他線程,但不理解解決方案的解釋。 有人可以簡單地解釋我在這里做錯了什么嗎?

你不能像這樣初始化一個數組。 您需要像在第 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
  };

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM