NO

Author Topic: is it possible to do something like this ?  (Read 3042 times)

whatsup

  • Guest
is it possible to do something like this ?
« on: October 31, 2010, 04:38:14 PM »
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

  • Guest
Re: is it possible to do something like this ?
« Reply #1 on: November 17, 2010, 02:19:47 AM »
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.
« Last Edit: November 17, 2010, 02:21:39 AM by hellork »

mtx500

  • Guest
Re: is it possible to do something like this ?
« Reply #2 on: November 17, 2010, 10:33:01 AM »
It is possible even without dirty tricks:
Code: [Select]
typedef char row[4];
row a[3];

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

whatsup

  • Guest
Re: is it possible to do something like this ?
« Reply #3 on: November 18, 2010, 05:30:29 PM »
It is possible even without dirty tricks:
Code: [Select]
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:
Code: [Select]
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
« Last Edit: November 18, 2010, 05:38:16 PM by whatsup »

CommonTater

  • Guest
Re: is it possible to do something like this ?
« Reply #4 on: November 18, 2010, 06:24:12 PM »
mtx500 ... nice solution.

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