简体   繁体   English

C中的动态大小数组

[英]Dynamic size array in C

I am trying to make use of Array of nodes with dynamic memory allocation so that I can increase the number of nodes at runtime. 我试图利用具有动态内存分配的节点数组,以便可以在运行时增加节点数。 However I seem to be getting an error which is unknown to me. 但是我似乎遇到了一个我不知道的错误。 I am probably using the array in wrong way, so please check and correct me. 我可能以错误的方式使用了数组,因此请检查并更正我。 listNo used in the code is an integer variable. 代码中使用的listNo是整数变量。

Code: 码:

Node* lists = (Node*) malloc(100 * sizeof(lists));

printf("\n Enter the number of lists:");
scanf("%d", &nbrOfLists);

if(nbrOfLists < 0)
    return -1;
if(nbrOfLists>100)
    lists = realloc(lists, 100 * sizeof(lists));


lists[listNo] = NULL;  // getting error here incompatible types assigning Node from type 'void*'
lists[listNo]= insertValue(lists[listNo], val); 

I mean each element in an array has first element of an individual linked list. 我的意思是数组中的每个元素都有一个单独的链表的第一个元素。 The next element is another independent first node of another linked list. 下一个元素是另一个链表的另一个独立的第一个节点。

For linked list you do not need to allocate memory for all elements. 对于链表,不需要为所有元素分配内存。 You have to have single root element Node * root and link all other to it. 您必须具有单个根元素Node * root并将所有其他元素链接到它。 No need from malloc and realloc - that is the idea of "linked list". 不需要mallocrealloc这就是“链接列表”的想法。 In your example you have dynamic array of objects. 在您的示例中,您具有动态对象数组。

  1. You should learn pointers concepts. 您应该学习指针概念。 Code signals you don't understand it well. 代码表示您不太了解。 Just friendly long term note. 只是友好的长期笔记。
  2. lists has type of pointer to Node which is "address", in 32-bit OS it is 4 bytes, in 64 bit - 8 bytes. lists具有pointer to Nodepointer to Node类型,即“地址”,在32位OS中为4字节,在64位-8字节中。 But no relation to actual Node structure size. 但是与实际的Node结构大小无关。
  3. When you do `malloc(N * sizeof(lists)) you actually allocate N elements, each of which is address (4 or 8 bytes, again no relation to actually stored information). 当您执行malloc(N * sizeof(lists))时,实际上分配了N个元素,每个元素都是地址(4或8个字节,再次与实际存储的信息无关)。
  4. When you refer to lists[X] you actually say "take this address of Node , and then take X-th Node under it". 当您引用lists[X]您实际上说的是“获取Node这个地址,然后获取其下的第X个Node ”。 You mean Node , not "address of Node " so your compiler says "I cannot assign Node with NULL". 您的意思是Node ,而不是“ Node地址”,因此您的编译器会说“我不能为Node分配NULL”。

Hope this helps. 希望这可以帮助。 Please re-check pointers concept in C. Blocking issue for many people to grow further. 请重新检查C中的指针概念。许多人的阻塞问题将进一步发展。

To say "please allocate N elements of type Node " you should write something like: 要说“请分配Node类型的N个元素”,您应该编写类似以下内容的内容:

Node *lists = realloc(lists, N * sizeof(*lists));

And you'd better say Node *list than Node* list (practically it results the same code but please check what is the difference - it is basic principle and you should learn it yourself). 而且,您最好说Node *list不是Node* list (实际上它产生相同的代码,但是请检查有什么区别-这是基本原理,您应该自己学习)。

If you want an array of pointers to nodes, then you need a pointer to the first pointer to node of an array of pointers to nodes. 如果需要指向节点的指针数组,则需要指向指向节点的指针数组中指向节点的第一个指针的指针。 This would be a double pointer for a variable sized array: node **arrayofpoitnerstonode; 这将是可变大小数组的双指针:node ** arrayofpoitnerstonode; , then arrayofpoitnerstonode[0] would be the first pointer to a node, arrayofpoitnerstonode[1] would be the second pointer to a node, ... . ,则arrayofpoitnerstonode [0]将是指向节点的第一个指针,arrayofpoitnerstonode [1]将是指向节点...的第二个指针。

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

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