0

I have a list. Each element of the list is a struct.

struct A
{
   int size;
}

The data looks like:

list[0]->size = a number.

How can I assign a pointer to each member of the list?

int *p;
for(i = 0; i < listSize; i++)
{
  p = &list[i];
} 

That won't work since I am assigning only one pointer to the last element of the list. Should I make a list of pointers?

This should solve the XY problem. How do I create pointers for every element of my list?

EDIT: List looks like this

A **list;

I want to sort by pointers instead of by structures so that it is faster.

Trying this now:

A ***p = (A***) malloc(sizeof(A***));

for(i = 0; i < listLength; i++)
    p[i] = &list[i];

for(i = 0; i < listLength; i++)
    printf( p[i]->size); // Error.
Mike John
  • 818
  • 4
  • 11
  • 29
  • 1
    This sounds like a classic [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is it exactly that you want to accomplish? – Pankrates Oct 12 '13 at 02:32
  • `list[i]` is already a pointer to each element. You're asking the wrong question. – gcochard Oct 12 '13 at 02:40
  • I can't help noticing that your example struct doesn't have a link member to point to the next struct instance in the list. Without link pointers somewhere, you don't have a linked list. Is that what you're asking about? - Probably not as you seem to be using an array, not a linked list, but then again you haven't `malloc`d memory for that array above - you seem to be using an uninitialized pointer as if it were an array. –  Oct 12 '13 at 02:47
  • 1
    I actually malloc, I just didn't want to bore anyone with trivial stuff. – Mike John Oct 12 '13 at 02:49
  • I think I saw it and it looked promising, but it was deleted before I could fully read it. – Mike John Oct 12 '13 at 02:56
  • @Mike - I should withdraw - I'm too tired and making stupid mistakes that are probably confusing things even more. –  Oct 12 '13 at 03:03
  • Do you really need to (m,c)alloc a struct? I can see needing to allocate some of the members if they are pointers, but the pointer to a struct itself can just be set to point to the definition, and go from there. `LIST_t list, *pList;` then in someFunc(), `pList = &list;` – ryyker Oct 12 '13 at 03:09
  • Mike, look ***[HERE](http://stackoverflow.com/a/522067/645128)*** for what looks like a very good example. And you are right, there was a good example up before that person took it down, @Micheal.yxd is also on the way to a good example. You were also correct in that I was not on the right path. – ryyker Oct 12 '13 at 03:26

2 Answers2

1

you can create array of pointer like: struct A *arr_pointer[N]

basically, you link struct should like that:

struct A {
    int size;
    struct A *next;
};
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
micheal.yxd
  • 118
  • 1
  • 7
  • You have identified the right concept, it is the basis, I believe, for what would satisfy the OP's requirement. [Look here](http://stackoverflow.com/a/522067/645128) – ryyker Oct 12 '13 at 03:32
0
typedef struct {
    int size;
} A, *pA;

typedef struct {
    int size;
} B, *pB;

//and so on...

//Now your list can be a collection of these
typedef struct {
    A a;
    B b;
    //additional members if defined
} LIST;

LIST list[20], *pList;  //[edited to make array of LIST]
//prototype List function

 LIST * updateList(LIST *a); 

int main(void)
{
   pList = &list[0];  //[edit to init pointer to array of lists
   //access and use pointers to list as necessary
   LIST *b = updateList(pList);
   //Use the updated list here
   printf( "b[0].a.size is: %d\n" , b[0].a.size);
   printf( "b[1].a.size is: %d\n" , b[1].a.size);
   printf( "b[2].a.size is: %d\n" , b[2].a.size);
   printf( "b[3].b.size is: %d\n" , b[3].b.size);
   printf( "b[4].b.size is: %d\n" , b[4].b.size);
   printf( "b[5].b.size is: %d\n" , b[5].b.size);

   return 0;
}

LIST * updateList(LIST *a)
{
    //do some manipulations to LIST here...
       a[0].a.size=1;
       a[1].a.size=2;
       a[2].a.size=3;
       //and so on.
       a[3].b.size=4;
       a[4].b.size=5;
       a[5].b.size=6;
       //and so on.
    return a;
}

Will this work for you?

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • I think you are doing something else. I just need to have p0...pn pointers for every element in my list[0..n] – Mike John Oct 12 '13 at 02:41
  • define an array of LIST. `LIST list[20], pList;` Then `pList = &list[0];`. new you have a pointer for every element in your array of lists. I may be completely missing the point, but this architecture should support an array of structure member pointers. If I have missed the point, please give out a few more clues. Sometimes I can be thick :) (see edited answer) – ryyker Oct 12 '13 at 02:43
  • Mike, (repeated from above comment) look ***[HERE](http://stackoverflow.com/a/522067/645128)*** for what looks like a very good example. And you are right, there was a good example up before that person took it down, @Micheal.yxd is also on the way to a good example. You were also correct in that I was not on the right path. win some, lose some. Good luck! – ryyker Oct 12 '13 at 03:27