初始化阵列时,VS2010中的“初始化”错误

时间:2014-02-05 03:19:09

标签: c arrays initialization

我需要分配一个包含6个数组的数组,它来自类型set[maxSetLength]

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#define maxSetLength 129

typedef short int set[maxSetLength]; 

int _tmain(int argc, _TCHAR* argv[]){
int i;
set a={0},b={0},c={0},d={0},e={0},f={0}; // Assigning 6 Sets (Arrays) initialized by zeros
set sets[6]={a,b,c,d,e,f}; //Inserting All Sets into one Array (Array Of Arrays)
}

在CodeBlocks中,它编译时没有错误,在VS2010中它没有错误,这些都是错误:

6次

error C2440: 'initializing' : cannot convert from 'set' to 'short'

6次

IntelliSense: a value of type "short *" cannot be used to initialize an entity of type "short"

12个整体错误

2 个答案:

答案 0 :(得分:0)

你需要使用指针(它们在C中很棘手)。尝试以下代码(我添加了一些调试,因此将其更改回0):

#include <stdio.h>
#include <string.h>

#define maxSetLength 129

typedef short int set[maxSetLength]; 

main()
{

int i;
set a={55},b={0},c={0},d={0},e={0},f={66}; // Assigning 6 Sets (Arrays) initialized by zeros
set sets[6]={*a,*b,*c,*d,*e,*f};

printf("%d\n", sets[0][0]);   // should be 55
printf("%d\n", sets[0][5]);   // should be 66

}

答案 1 :(得分:0)

set a={0},b={0},c={0},d={0},e={0},f={0}; // Assigning 6 Sets (Arrays) initialized by zeros
set *sets[6]={&a, &b, &c, &d, &e, &f}; //Inserting All Sets into one Array (Array Of Arrays)