Re: declaring array of pointers

From: Al Bowers (xabowers_at_rapidsys.com)
Date: 09/19/04


Date: Sun, 19 Sep 2004 09:12:21 -0400


Steve Lambert wrote:
> Hi,
>
> I'd be grateful if someone could clarify this for me. In the linked list
> structure my intention is to declare an array of length 3 containing
> pointers to node
> eg. Node *Iterators[3]
> The compiler seems to interpret this as a pointer to an array of 3 nodes
> instead. This interpretation ensures that the second assigment to mynode
> below fails compilation with the given message.
>
> Could someone explain to me how to correctly declare an array of length 3
> containing pointers to node
>
> cheers
>
>
> #include <stdio.h>
>
> typedef struct Node
> {
> void *Data;
> struct Node *Next;
> struct Node *Previous;
> } Node;
>
> /* structure to represent a linked list */
> typedef struct LinkedList
> {
> Node *Head; /* start of linked list */
> Node *Tail; /* end of linked list */
> long NumberOfNodes;
> short NumberOfIterators;
> Node *Iterators[3];
> } LinkedList;
>
> LinkedList *mLinkedLists[2];
>
>
> void main()
> {
> Node *mynode;
> LinkedList *LL=NULL;
>
> LL = mLinkedLists[0];
>
> mynode = LL->Iterators[0]; <- this compiles
>

It may compile but it is very flawed and will not run.
LL has the value NULL and does not point to storage.
Since you are trying to access storage that is not, the
execution will fail.

You can type the member Iterators as
Node *Iterators[3];
making Iterators point to an array of 3 Node pointers.
Make a LinkedList object
LinkledList mylist;
Then you can access or assign Iterators with code something
like this:
mylist.Iterators[0] = malloc(sizeof Node);
or
Node newnode;
mylist.Iterators[0] = &newnode;
and
mylist.Iterators[0]->Data = /* whatever */

One the other hand out might make the member Iterators:
Node (*Iterators)[3];
Iterators will point to an array of 3 Node objects (not pointers).
Here is an example, in function main below:

#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
    int Data;
    struct Node *Next;
    struct Node *Previous;
} Node;

/* structure to represent a linked list */
typedef struct LinkedList
{
    Node *Head; /* start of linked list */
    Node *Tail; /* end of linked list */
    size_t NumberOfNodes;
    size_t NumberOfIterators;
    Node (*Iterators)[3];
} LinkedList;
                  /* Prototypes */
int AddNode(LinkedList *p, int data, size_t IterNo);
void FreeLinkedList(LinkedList *p);

int main(void)
{
    LinkedList myList = {NULL};
    size_t i,j;

    AddNode(&myList, 44,0);
    AddNode(&myList,33,2);
    printf("myList.NumberOfIterators = %u\n",
           myList.NumberOfIterators);
    for(i = 0; i < myList.NumberOfIterators;i++)
       for(j = 0; j < 3; j++)
       {
          myList.Iterators[i][j].Data = i+j;
          printf("myList.Iterator[%u][%u].Data = %d\n",
                 i,j, myList.Iterators[i][j].Data);
       }
    printf("\nmyList.Head->Data = %d\n",myList.Head->Data);
    FreeLinkedList(&myList);
    return 0;
}

int AddNode(LinkedList *p, int data, size_t IterNo)
{
    Node *new;
    Node (*itmp)[3];

    if((new = malloc(sizeof *new)) == NULL) return 0;
    itmp = realloc(p->Iterators,(sizeof *itmp)*IterNo);
    if(IterNo && itmp == NULL)
    {
       free(new);
       return 0;
    }
    p->Iterators = itmp;
    p->NumberOfIterators = IterNo;
    new->Data = data;
    if(p->NumberOfNodes == 0)
    {
       new->Previous = new->Next = NULL;
       p->Head = p->Tail = new;
    }
    else
    {
       new->Previous = NULL;
       new->Next = p->Head;
       p->Head->Previous = new;
       p->Head = new;
    }
    p->NumberOfNodes++;
    return 1;
}

void FreeLinkedList(LinkedList *p)
{
    Node *tmp;

    for(tmp = p->Head; tmp;p->Head = tmp)
    {
       tmp = p->Head->Next;
       free(p->Head);
    }
    free(p->Iterators);
    p->Head = p->Tail = NULL;
    p->Iterators = NULL;
    p->NumberOfIterators = p->NumberOfNodes = 0;
    return;
}

-- 
Al Bowers
Tampa, Fl USA
mailto: xabowers@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


Relevant Pages

  • Re: A Stupid Question about Structure
    ...     int data; ... struct datastruct *next; ... I think you are confused about the use of pointers. ...
    (comp.lang.c)
  • Re: Simple question, err... I think
    ... Your nodes contain no other indication of which pointers are valid, ... struct CountedObject ... int is_red; ... bool lament(char const s) ...
    (comp.programming)
  • Re: porting problems encountered
    ... Tru64 compiles long variables to size 8 bytes while VMS and HP-UX ... platform, the size of the structure would be 12 bytes. ... when it got to the AS/400 with 128 bit pointers. ... Using the struct from before: ...
    (comp.os.vms)
  • Re: strict aliasing rules in ISO C, someone understands them ?
    ... I understand now that my interpretation of the standard was totally ... an aggregate is a struct or an array. ... struct s1 {int i; double d;}; ... Pointers are just a means of accessing the objects, ...
    (comp.lang.c)
  • Re: Array of pointers in a struct
    ... >typedef struct trieNode ... This is an array of 27 pointers. ... >And this is the code I make a new trie node, initialize and return it. ...
    (comp.lang.c)