结构数组声明

struct book as[ARRAYSIZE];

例子


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

#define SIZE 10
#define ARRAYSIZE 6

struct book{
int id;
// char name[SIZE];
char * name;
};


void iterItem(void * data) {
// struct book bs[] = data;

struct book *b = data;

for(int i=0;i<ARRAYSIZE;i++) {
struct book *bb = (b+i);
printf("id=%d,name=%s \n", bb->id, bb->name);
}

}

int main(){

struct book as[ARRAYSIZE];

int ids[] = {1,2,3,4,5,6};
char *strs[] = {"c++","c","c primer","c primer plus","c++ primer","c++ primer plus"};
// char strs[] = {"c++"};

int len = sizeof(ids)/sizeof(ids[0]);

printf("ids.length=%d \n",len);

for(int i=0;i<len;i++) {
as[i].id = ids[i];
as[i].name = strs[i];
};

int asLen = sizeof(as)/sizeof(as[0]);
printf("as.length=%d \n",asLen);

iterItem(&as);

return 1;
}

输出

ids.length=6

as.length=6

id=1,name=c++

id=2,name=c

id=3,name=c primer

id=4,name=c primer plus

id=5,name=c++ primer

id=6,name=c++ primer plus