1

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.

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.

10
  • 2
    do you want array of nodes or array of node pointers? Commented Feb 24, 2015 at 22:10
  • I suggest further reading about pointers and arrays. Commented Feb 24, 2015 at 22:12
  • array of node pointers. Each array element is a node pointing to next node in a list Commented Feb 24, 2015 at 22:12
  • You should declare a few variables first such as listNo, val, nbrOfLists and It looks like Node is your own invented data type? So you need to define that too. is it a struct? Commented Feb 24, 2015 at 22:12
  • 1
    array of node pointers vs Each array element is a node - it is contradiction Commented Feb 24, 2015 at 22:13

3 Answers 3

1

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. No need from malloc and realloc - that is the idea of "linked list". In your example you have dynamic array of objects.

Sign up to request clarification or add additional context in comments.

1 Comment

I mean each element in an array has first element of an individual linked list.
0
  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. But no relation to actual Node structure size.
  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).
  4. When you refer to lists[X] you actually say "take this address of Node, and then take X-th Node under it". You mean Node, not "address of Node" so your compiler says "I cannot assign Node with NULL".

Hope this helps. Please re-check pointers concept in C. Blocking issue for many people to grow further.

To say "please allocate N elements of type Node" you should write something like:

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).

2 Comments

Thanks a lot sir. However, I am able to achieve what I want using Node *lists[100]; but not the above method. Anyways I will revise my concepts again.
@AbhishekSingh, do you understand that if you declare something like Node *lists[100] you actually have just 100 pointers to Node? Is it your real goal? As far as I understand you need pointer to 100 Node structures. Right?
0

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; , then arrayofpoitnerstonode[0] would be the first pointer to a node, arrayofpoitnerstonode[1] would be the second pointer to a node, ... .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.