简体   繁体   English

从C中的列表解引用结构中的变量

[英]dereference a variable in a struct from a list in C

I am trying to dereference variables in a struct that is in a list, so here is my list and struct 我试图取消引用列表中的结构中的变量,所以这是我的列表和结构

struct packet {
 unsigned short block_num;
 unsigned short block_size;
 unsigned short crc;
 unsigned char  *payload;
}packet;

/* Function and type declaration for list of packets */ / *数据包列表的功能和类型声明* /

typedef struct List{
    struct packet *p;
    struct List *next;
}List;

And here I am trying to do a toString(): 在这里,我试图做一个toString():

char *tostring(List *h) {
 char *str= malloc(STRSIZE);
 char num[sizeof(List)*100];
 str[0] = '\0';
 while (h != NULL) {
     sprintf(num, "package Number: %d \n", h->p->block_num);
     sprintf(num, "block size: %d \n", h->p->block_size);
     sprintf(num, "CRC: %d", h->p->crc);
     strncat(str, num, STRSIZE - strlen(str) - 1);
     h = h->next;
 }
 return str;
}

And I hit a segmentation fault, please help, thanks ! 而且我遇到了细分错误,请帮忙,谢谢!

So, in C#, I will use list.ElementAt(i).block_num , how is it done in C ? 因此,在C#中,我将使用list.ElementAt(i).block_num,如何在C中完成?

edit: new ones, the problem lies on my dereferencing 编辑:新的,问题出在我的取消引用

char *tostring(List *h) {
 char *str= malloc(STRSIZE);
 printf("line1");
 char num[sizeof(List)*100];
 char size[sizeof(List)*100];
 char crcs[sizeof(List)*100];
 char messages[sizeof(List)*100];
while (h != NULL) {
    sprintf(num, "package Number: %d \n", h->p->block_num);      
    sprintf(size, "block size: %d \n", h->p->block_size);       
    sprintf(crcs, "CRC: %d", h->p->crc);
    sprintf(messages, "CRC: %s", h->p->payload);
    strncat(str,num,sizeof(num));
    strncat(str,size,sizeof(size));
    strncat(str,crcs,sizeof(crcs));
    strncat(str,messages,strlen(num));
    h = h->next;

}
return str;
}

Are you using sizeof(*h) to determine the number of List elements in an array? 您是否正在使用sizeof(*h)确定数组中List元素的数量?

sizeof(*h) returns the size of List . sizeof(*h)返回List的大小。 Since tostring() only get's a pointer to a List , it has no way of knowing how many elements are pointed to. 由于tostring()仅获得指向List的指针,所以它无法知道所指向的元素数。

In any case, it appears you are then writing a string to a character array of this size. 无论如何,似乎您正在将字符串写入此大小的字符数组。 What does the size of List have to do with the number of elements in the array? List的大小与数组中的元素数量有什么关系? What are you trying to do? 你想做什么?

There are few things that may go wrong here 这里有些事情可能会出错

  1. Your SRTSIZE may be low to hold all the text that you start appending to str. 您的SRTSIZE可能很低,无法容纳您开始附加到str的所有文本。 For that you can start with a big buffer for now to eliminiate that. 为此,您现在可以从一个很大的缓冲区开始以消除这种情况。

  2. As posted from previous answers you are always overwriting num. 如先前答案中所述,您始终会覆盖num。 You should write to 'str' after you create num every time 每次创建num后,您都应该写“ str”

  3. strncat implementation seems incorrect. strncat实现似乎不正确。 You should use strlen(num). 您应该使用strlen(num)。

    strncat(str, num, strlen(num)); strncat(str,num,strlen(num));

char * strncat ( char * destination, const char * source, size_t num ); char * strncat(char *目标,const char *源,size_t num); The final parameter expects Maximum number of characters to be appended. 最后一个参数要求追加最大字符数。

refer : http://www.cplusplus.com/reference/cstring/strncat/ 请参考: http : //www.cplusplus.com/reference/cstring/strncat/

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

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