Initialising an array with pointers into the same array.

C

celephicus

Greetings,

This is what I want to do:

int a, b;
int *arr[] = { &a, &b, &arr[1], };

Of course this won't work. Is there a (portable) way to achieve this?
The application is initialising an array that is a linked list, and I
don't want to do it at runtime as I am on an embedded system with not
much memory.

Regards

TomH
 
D

Denis McMahon

Of course this won't work. Is there a (portable) way to achieve this?
The application is initialising an array that is a linked list, and I
don't want to do it at runtime as I am on an embedded system with not
much memory.

If you want to allocate the memory at compile time, maybe an array of
structs would be best?

Any method of allocating at compile time will set an upper limit on the
memory used, it will also set an upper limit on the number of items you
can store.

Given that memory is the constraint, I'll assume a singly linked list.

#define LISTSIZE 1000;

typedef struct listmember_s {
int next = -1;
sometype somename;
sometype somename;
sometype somename;
} listmember_t;

listmember_t list[LISTSIZE];
int head = -1; /* 0 is a valid element, -1 means empty list */

Use head to point to the array index of the current list head, and use
list[member].next to point to the next member.

list[member].next = 0 for the tail of the list.

When you delete a member from the list, set it's next member to -1. This
will provide an easy way to determine if a given array element is
currently a valid member of the list.

finding a free array element:

int findfree() {
for (int i = 0; i < LISTSIZE; i++) if (list.next == -1) return i;
return -1;
}

This may not be the best way, but I think it might do what you want?

Rgds

Denis McMahon
 
B

Ben Bacarisse

celephicus said:
This is what I want to do:

int a, b;
int *arr[] = { &a, &b, &arr[1], };

Of course this won't work. Is there a (portable) way to achieve this?

I'm not sure what the "this" is. Your code tries to set arr[2] to a
pointer of an incompatible type (you can "fix" that with a cast but the
result will be odd). Also you don't say if this code is at file scope
or not and that makes a difference.
The application is initialising an array that is a linked list, and I
don't want to do it at runtime as I am on an embedded system with not
much memory.

I think you've reduced your example beyond its usefulness! If you were
initialising an array so that it behaves like a linked list the elements
would be list nodes not pointers:

struct node { int v; struct node *next; };

struct node list[] = {
{ 2, list+1 },
{ 4, list+2 },
{ 8, list+3 },
{ 10, 0 },
};

I think you need to back up and show something closer to the code you
really have.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top