Pelles C forum

C language => Beginner questions => Topic started by: whatsup on October 31, 2010, 04:38:14 PM

Title: is it possible to do something like this ?
Post by: whatsup 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
Title: Re: is it possible to do something like this ?
Post by: hellork 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.
Title: Re: is it possible to do something like this ?
Post by: 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

Title: Re: is it possible to do something like this ?
Post by: whatsup on November 18, 2010, 05:30:29 PM
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
Title: Re: is it possible to do something like this ?
Post by: CommonTater on November 18, 2010, 06:24:12 PM
mtx500 ... nice solution.

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