简体   繁体   English

更改包含结构打印的数组的值

[英]Changing values of an array containing a struct printing

I would like to access the name field from the array items and print the name but I am having trouble. 我想从数组项目访问名称字段并打印名称,但是遇到了麻烦。

I created a pointer 'L' in callInitialize and set it to the upper struct type List which I named 'studentList'. 我在callInitialize中创建了一个指针“ L”,并将其设置为我命名为“ studentList”的上层结构类型List。

int callInitialize () {

/*Initialize a List*/
List studentList;
List *L;
L = &studentList;
Initialize(L);

#ifdef DEBUG
    printf("L->count after function call Initialize = %d\n", L->count);
    printf("L->items[0].name after function call Initialize = %s\n", studentList.items[0].name);
#endif 

return 0;
}

I then called Initialize and tried to set the value to test but this is incorrect. 然后,我调用了Initialize并尝试设置要测试的值,但这是不正确的。

void Initialize (List *L) {
char test = "I like you";
L->count = 0;
L->items[0].name = test;
}

I am unsure of why L->items[0].name = test; 我不确定为什么L-> items [0] .name = test; is not appropriate. 是不合适的。 I am getting an incompatible types error but name is char and test is char? 我收到了不兼容的类型错误,但名称是char,而测试是char?

Also, once I do change that value how would I print that? 另外,一旦我更改了该值,我将如何打印该值? My thinking was that %s would be correct since the type of the field name is char. 我的想法是,%s是正确的,因为字段名称的类型为char。 Print is above in callIntialize as a debug statement. 打印在callIntialize上方,作为调试语句。

My struct declarations: 我的结构声明:

#define MAXNAMESIZE 20
typedef struct {
char name[MAXNAMESIZE];
int grade;   
} Student;

typedef Student Item;
#define MAXLISTSIZE 4
typedef struct {
Item items[MAXLISTSIZE];
int count;
} List;

Thank you for any help. 感谢您的任何帮助。

String copying in C doesn't work like that. 在C中进行字符串复制不能那样工作。 Strings are simply arrays of characters, with a null character as the last element used; 字符串只是字符数组,最后一个使用的字符为空。 so to copy a string you need to copy the individual characters from the source array to the corresponding elements in the destination array. 因此,要复制字符串,您需要将单个字符从源数组复制到目标数组中的相应元素。 There are library functions for doing this, such as strncpy() . 有一些库函数可以执行此操作,例如strncpy()

So you would need to change: 因此,您需要更改:

L->items[0].name = test;

..to something like: ..像这样:

strncpy(L->items[0].name,test,MAXNAMESIZE);
L->items[0].name[MAXNAMESIZE - 1] = '\0';

..where the second line just makes sure there's a null character at the end, in case test was longer than MAXNAMESIZE . ..其中第二行只是确保结尾处有一个空字符,以防万一testMAXNAMESIZE

If the name member of Student had been declared as a char * and allocated with malloc() , rather than declared as an array of char , the assignment would have worked, but probably not done what you wanted -- it would have changed name to point to the same string as test (not a copy of it) and just discarded the original value, leaking the malloc() ed memory. 如果Studentname成员已声明为char *并使用malloc()分配,而不是声明为char数组,则该分配会起作用,但可能未完成您想要的操作-它将name更改为指向与test相同的字符串(而不是它的副本),只是丢弃了原始值,从而泄漏了malloc() ed内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM