0

I have a linked list defined as such:

typdef struct _seg
{
    int bits[256];        // # of bits in bits[] array = 256

    struct _seg *next;    // link to the next segment
} seg;

And I was wondering how I can access the bit array inside each node of this list. If it were a regular int variable and the list was called p, I could just do p->bits = 13;. But I don't know how to get to and modify the list in this case. Can someone please help me out?

P.S. (not as important) Does anyone know what the seg; does at the very end?

yiwei
  • 4,022
  • 9
  • 36
  • 54
  • 1
    Here you find the meaning of "typedef": http://stackoverflow.com/a/612350/126125 – Markon May 03 '13 at 13:54
  • 1
    @Markon Thanks! That makes a lot of sense and makes things a lot easier! – yiwei May 03 '13 at 13:56
  • The way you've declared your structure, the `bits` array is an array of ints, so it has `256*sizeof(int)*8` bits. – Caleb May 03 '13 at 14:05

2 Answers2

1

p->bits is an array of 256 integers. You can access it with p->bits[0] = 13.

Or in general p->bits[i] = 13 where 0 <= i < 256.

typdef struct _seg { ...
}seg;

With this you can define a variable of this struct type using seg as

seg SomeName;

No need for struct _seg someName;.

Gui13
  • 12,993
  • 17
  • 57
  • 104
Rohan
  • 52,392
  • 12
  • 90
  • 87
1

To access the nodes in your list, you'll have to use a loop to iterate on all your elements:

seg* p = createList();

seg* current = p; // start at first element
while( current != NULL ){
    for( int i=0; i<256; i++ ) {
        current->bits[i] = 13;  // modify bits inside
    }
    current = current->next; // go to next element in the list
}
Gui13
  • 12,993
  • 17
  • 57
  • 104