Initialising an array with pointers into the same array.

Discussion in 'C Programming' started by celephicus, Jun 7, 2010.

  1. celephicus

    celephicus Guest

    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
     
    celephicus, Jun 7, 2010
    #1
    1. Advertising

  2. 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.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
     
    Denis McMahon, Jun 7, 2010
    #2
    1. Advertising

  3. 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.
     
    Ben Bacarisse, Jun 7, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeremy Targett

    initialising 2-dim character array

    Jeremy Targett, Jul 25, 2006, in forum: C Programming
    Replies:
    6
    Views:
    337
    Keith Thompson
    Jul 26, 2006
  2. Replies:
    3
    Views:
    350
    Al Balmer
    Dec 21, 2006
  3. Nick Keighley

    trouble initialising array

    Nick Keighley, Jun 21, 2007, in forum: C Programming
    Replies:
    4
    Views:
    305
  4. Replies:
    5
    Views:
    339
    Craig Scott
    Apr 8, 2008
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    711
Loading...

Share This Page