Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Initialising an array with pointers into the same array.

Reply
Thread Tools

Initialising an array with pointers into the same array.

 
 
celephicus
Guest
Posts: n/a
 
      06-07-2010
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
 
Reply With Quote
 
 
 
 
Denis McMahon
Guest
Posts: n/a
 
      06-07-2010
On 07/06/10 09:00, celephicus wrote:

> 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[i].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
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      06-07-2010
celephicus <> writes:

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

--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Initialising classes with pointers to this pauldepstein@att.net C++ 5 04-08-2008 09:27 PM
trouble initialising array Nick Keighley C Programming 4 06-22-2007 10:03 AM
"Partially elided initialisation" on initialising 2-D array mark.bergman@thales-is.com C Programming 3 12-21-2006 03:23 PM
initialising 2-dim character array Jeremy Targett C Programming 6 07-25-2006 11:31 PM



Advertisments