is it possible to do something like this ?

Started by whatsup, October 31, 2010, 04:38:14 PM

Previous topic - Next topic

whatsup

i have an array like this:

char a[3][4]

i want to declare a pointer to this array
in a way that every time i increase the pointer , the pointer will increase by 4 (in this case)

something like with function argument:

int myfunc(char a[][4])

every time i increase a, it will increase by 4

the same thing i want to do with var declaration (not argument declaration)

is it possible ?

thanks in advanced

hellork

#1
Possible. I have heard of this before, but I would guess that you ought to handle the unionized data in a byte-order-independent way from start to finish. Else things could get more complicated as the code would have to work out which of the 4 bytes returned by the punned pointer had the data. Hmm, that sentence will not translate well. Take a look at the popular opinions on the subject here on Wikipedia: http://en.wikipedia.org/wiki/Type_punning

Or just declare an array of uint32_t and treat the values as char. I guess it depends on what you want to do with it.

mtx500

It is possible even without dirty tricks:

typedef char row[4];
row a[3];

row *rp = &a[0];
rp++; // advances to next row, i.e. increases pointer by 4


whatsup

#3
Quote from: mtx500 on November 17, 2010, 10:33:01 AM
It is possible even without dirty tricks:

typedef char row[4];
row a[3];

row *rp = &a[0];
rp++; // advances to next row, i.e. increases pointer by 4



you are great man!
thank you very very much!!!

btw, i was trying somethings, and while trying , i found this:

char a[][10]  // no compiler error
static a[][10] // error - undefined size

and i started to wonder where pelles allocate the first one and where the second

i always thought they share the same memory area

CommonTater

mtx500 ... nice solution.

I was thinking  rp+=sizeof(a[0]);  to increment to the next row.