我收到以下警告,无法打印数组。/* 警告:格式 %d 需要类型为"int"的参数,但参数 2 的类型为"char *" */O/P: [4196160][4196160] ??
#include<stdlib.h>
#include <stdio.h>
# define MAX 3
typedef struct{
int rollno;
int credits;
}list;
typedef struct{
int no_of_students;
list studentList[MAX];
}recordList;
void setList(list *l, int rollno, int credits, int *msgLen)
{
l->rollno = rollno;
l->credits = credits;
*msgLen = sizeof(rollno) + sizeof(credits) + sizeof(list);
}
int main()
{
recordList *r;
list l;
r = malloc(sizeof(recordList));
r->no_of_students = 2;
int index = 0;
int msgLen = 0;
setList(&r->studentList[index], 1, 5, &msgLen);
setList(&r->studentList[index++], 2, 6, &msgLen); //trying to fill the data for index = 1
for(int i=0; i<2; i++)
{
printf("[%d]", "[%d]", r->studentList[i].rollno, r->studentList[i].credits);
}
return 0;
}
因为没有任何内存分配给 r。 使用 malloc 分配内存:
#include <stdlib.h>
...
recordList *r = malloc(sizeof(recordList));