【发布时间】:2014-03-09 02:29:15
【问题描述】:
如何将数据设置为 3 行数组同时包含 4 个元素的数组?
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
typedef struct
{
char value[6];
} MyType;
typedef struct
{
int size;
MyType *values;
} NewType;
static NewType car;
static MyType mytype = {{'\0'}};
void Init(NewType *car)
{
car->size = 3; // Will contain 3 rows of 4 elements
car->values = (MyType*) calloc(car->size,sizeof(MyType));
}
// Get data into
void Write(NewType *car, MyType *data)
{
strcpy(car->values[0].value, data[0].value); // ** Here is where complains
printf("%d\n", car->values[0]); // Printing wrong data!
}
int main()
{
Init(&car);
strcpy(mytype.value, "Hello");
Write(&car, &mytype);
system("PAUSE");
}
【问题讨论】:
-
您希望
printf显示什么?如果你把它改成printf("%s\n", car->values[0].value);,那么它将打印Hello。 -
"Hello" 需要 6 个数组而不是 4 个数组。
标签: c arrays visual-studio-2010 struct