Re: Linked List & Dynamic Memory Allocation





one-trick-pony wrote:
Sorry for confusion. I deliberately left code out because it was
irrelevant to the question I am asking. I have program working-I have
a linked list and it prints all the records fine. The only problem is
connecting/attaching a new dynamically allocated records to the linked
list.

Will this work if user enters 5 or 6 or even 100 or x number of
records? Your code worked for adding on only 1 more record. It keeps
the 3 hardcoded records then added one more with the help of following
lines of code:

m_nMode[MAX-1].next = pNewRec;
pNewRec->next = NULL;

However, if user enters more than 1 the only one attached to the list
is the very last entry that user made before choosing to stop. Still
need help. Thanks

Is this purely an exercise? If not, there are tools in the STL to make your life easier.

But if it is...

First, I wouldn't call a node, 'nodes'. Your starting array is a list of 'nodes'.

Node nodes[3];

So you have the array populated and the last node has a node pointer of null or the next one on the heap. You can walk the pointers to find the last node.

Node* pNode;
for( pNode= nodes[ 3 ].next; pNode->next; pNode= pNode->next )
;

pNode->next= new Node;

Or you can just keep a pointer to the last node that you update when you add nodes, like:

pNode= pNode->next;

Use this pNode to setup your new node and keep it around to add the next node.

But for an occasional TCHAR array, I don't think I've used an array on the stack for a decade. (I'm really thinking here! :)

Best, Dan.

.



Relevant Pages

  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: why cannot assign to function call
    ... hypothetical C-like languages, ... sizeof business would still indicate that a pointer was being passed. ... talk about variables of an array type. ... the earlier version of the standard didn't have numbered ...
    (comp.lang.python)
  • Re: multi dimensional arrays as one dimension array
    ... please - where does the standard say that such a conversion ... Pointer conversion yields a pointer to the same object as ... exist only where there are array declarations. ...
    (comp.lang.c)
  • Re: Evaluating unary *
    ... 'arr' exists, ... value can be used with the same syntax as would be used to access a 2D array of the kind you're referring to, but that 2D array is just a different way of looking as the same object that was already created by the definition of 'arr'. ... to me, it makes sense to return a pointer to the first value of an array, but to return the address of the pointer to the first value of an array, is not directly possible as such. ... lea eax, ...
    (comp.std.c)
  • Re: Copying an array slice (Was: Re: Difficulties with passing multi-dimensional arrays)
    ... > the pointer to array of unknown size. ... but it still compiles without a cast. ... unknown at compile time so nothing can be checked. ...
    (comp.lang.c)

Loading