Pointers to more Pointers...
Author Message
Pointers to more Pointers...

Quote:
> How do you explicitly store the addresses of pointers within an array
of
> pointers besides dynamic allocation.

> Consider this code:

> node **table,*a,*b,*c,d;
> table=(node **) malloc(2*sizeof(node *));
> a=(node *) malloc(3*sizeof(node));
> b=(node *) malloc(3*sizeof(node));
> table[0]=&a;

You have made table to be an array of two node pointers (type
*node).Therefore you need to assign to the table elements a node pointer
and not the address of a node pointer. Also, you need to test the return
values of malloc. Allocation may fail.

table[0] = a;

Assuming space was allocated, table[0] now is a node pointer to an array
of three type node.

Quote:
> table[1]=&b;

table[1] = b;

Quote:
> c=&d;

> it doesn't seem to work.

In c language, you do not need to cast the return value of function
malloc.

--
Al Bowers

GIBCO Labs.
Tampa FL. USA.

Sent via Deja.com http://www.*-*-*.com/

Sat, 24 Aug 2002 03:00:00 GMT
Pointers to more Pointers...
How do you explicitly store the addresses of pointers within an array of
pointers besides dynamic allocation.

Consider this code:

node **table,*a,*b,*c,d;
table=(node **) malloc(2*sizeof(node *));
a=(node *) malloc(3*sizeof(node));
b=(node *) malloc(3*sizeof(node));
table[0]=&a;
table[1]=&b;
c=&d;

it doesn't seem to work.

Although this works fine:

node **table,*a;
a=(node *) malloc(3*sizeof(node));
table=&a;

Could you explain this to me

-CSK
http://snap.to/csk

Sun, 25 Aug 2002 03:00:00 GMT

 Page 1 of 1 [ 2 post ]

Relevant Pages