Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
C array points, HELP!
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Gentree
Watchman
Watchman


Joined: 01 Jul 2003
Posts: 5347
Location: France, Old Europe

PostPosted: Thu Jun 25, 2015 6:43 pm    Post subject: C array points, HELP! Reply with quote

Hi,

I'm trying to do what appears to be fairly simple but I can't come to an agreement with the compiler. It a bit like Varufakis talking to the loan sharks, what every way I write it, it always gets refused.

I have two 2x3 arrays of integers and I want combine them and access the latter half with a variable name just like I did when they were separate.

uint8_t a1[2][3];
unit8_t a2[2][3];

I want to declare a2 with allocating memory then later initialize it to point half way into a1.

I would like a1 and s2 to be of the same type so that I don't need to rewrite and retest the existing code.

I was expecting to be able to declare a [0][0] or dimensionless array variable and then assign to point half way into a1.

Being able to do silly things with pointers seems to be the speciality of C so I'm sure it's just a case of getting the syntax right.

BTW the original reason was that I wanted to initialise both arrays with one fillchar(). Now it's just bugging me that I can't the frigging thing to work.

TIA, Gentree. 8)


I could just declare them sequentially and assume that they get contiguous allocation but that's pretty hacky, and anyway I want to get the damn thing to do what I want. :evil:
_________________
Linux, because I'd rather own a free OS than steal one that's not worth paying for.
Gentoo because I'm a masochist
AthlonXP-M on A7N8X. Portage ~x86
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 8583
Location: Somewhere over Atlanta, Georgia

PostPosted: Thu Jun 25, 2015 7:59 pm    Post subject: Reply with quote

So, since you want a single array to hold both, a1 grows and then is declared like this:
Code:
uint8_t a1[4][3];
Make sense? The next trick is to declare a2 as a pointer to an array of 3 uint8_t. Since the * (pointer to) symbol binds to the type on the left normally, we need some parentheses to make it come out right. Might as well go ahead & initialze it to point half way into a1 while we're at it, like this:
Code:
uint8_t (*a2)[3] = a1 + 2;
Used as an array reference or lvalue, as initialized above, a2[0][0] references a1[2][0].

That ought to work for you. ;)

- John
_________________
I can confirm that I have received between 0 and 999 National Security Letters.
Back to top
View user's profile Send private message
Gentree
Watchman
Watchman


Joined: 01 Jul 2003
Posts: 5347
Location: France, Old Europe

PostPosted: Thu Jun 25, 2015 8:25 pm    Post subject: Reply with quote

Hey, thanks John. I was sure there'd be someone around here that could help.

The first line was where I started, fairly obvious. It's the hieroglyphics of the second line that I could not work out. And having seen it, I'm still no wiser.

I get the pointer arithmetic on the RHS, could you explain what the LHS is about?

Thanks for you help.


Quote:
C has the speed and efficiency of assembler, combined with the readability of ...... assembler
;)
_________________
Linux, because I'd rather own a free OS than steal one that's not worth paying for.
Gentoo because I'm a masochist
AthlonXP-M on A7N8X. Portage ~x86
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 8583
Location: Somewhere over Atlanta, Georgia

PostPosted: Thu Jun 25, 2015 9:26 pm    Post subject: Reply with quote

Google "Right-Left Rule for C Declarations" for the comprehensive story. It takes a while to internalize. Here's how I read our fancy declaration:
  1. Start with the identifier. First accumulation is, "a2 is...". Look to the right, where we bang into the parenthesis, so...
  2. Look to the left (still inside the parentheses) where we find the "*" symbol, so second accumulation is, "a2 is a pointer to...". We've run out of stuff inside the parentheses, so jump outside and...
  3. Look to the right where we find [3], leading to the 3rd accumulation, "a2 is a pointer to an array of 3...". There's nothing else to the right so, ...
  4. Look to the left where we find uint8_t, leading to the final definition, "a2 is a pointer to an array of 3 uint8_t."
What could be simpler? ;)

(There are a few additional nuances but hopefully this gives you a good enough hunting license.)

- John
_________________
I can confirm that I have received between 0 and 999 National Security Letters.
Back to top
View user's profile Send private message
bstaletic
Tux's lil' helper
Tux's lil' helper


Joined: 05 Apr 2014
Posts: 120

PostPosted: Thu Jun 25, 2015 9:27 pm    Post subject: Reply with quote

Let me try to explain.

The name of an array is a pointer to the first element of thhe array itself. If you had one dimensional arrays the second line would be:
Code:
uint8_t *a2=a1+2;

Since you're working on two dimensional arrays you need to specify another dimension. You could have written:
Code:
uint8_t **a2=a1+2;

In this case you would have to allocate memory for the second dimension of the array. Your second dimension is fixed at 3, so there's no point in doing this, hence '[3]' part in the second line by Jonh R. Graham.

C has somewhat strange operator precedence. The [] operator will execute before * operator. Parenthesis makes up for that.
Back to top
View user's profile Send private message
Gentree
Watchman
Watchman


Joined: 01 Jul 2003
Posts: 5347
Location: France, Old Europe

PostPosted: Thu Jun 25, 2015 10:34 pm    Post subject: Reply with quote

Thanks for both your replies.
_________________
Linux, because I'd rather own a free OS than steal one that's not worth paying for.
Gentoo because I'm a masochist
AthlonXP-M on A7N8X. Portage ~x86
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum