array in c

Thread Starter

meera

Joined Jun 7, 2007
6
i'm using c to write program for microchip microcontroller.

recently i come across with one program from internet which i confuse with one of the code

normally for array in c, i just write
bitmap[]={5,5,5};
temp=bitmap[0]-1; then i get temp=4;

but now i saw from the code, it's written there
temp1=bitmap;
temp2=bitmap-1;

may i know what's mean by bitmap-1?(just the array name)

i try to run some simple simulation for the code, i get the answer
temp1=10;
temp2=9;

but i don't understand where its value come from.

thanks.
 

rjenkins

Joined Nov 6, 2005
1,013
I'm guessing it's using pointers, but without seeing the rest of the code and specifically the variable declarations, it is only a guess..
 

AlexR

Joined Jan 16, 2008
732
It is indeed using pointers. The name of any array is in fact a pointer to the first element of that array if you have an array test[];
then a = test[0]; and a = test; will produce the same result.
 

Papabravo

Joined Feb 24, 2006
21,225
The syntax and semantics came from the original K&R. Specifically, an array name, foo, was to be interpreted as:

&foo[0]

or in other words the address of the first element in the array.
 

rjenkins

Joined Nov 6, 2005
1,013
Yes, but it cannot be the array name -1, that would be one element below the start of the array.

It would only make sense with a pointer used within the array.
 

Papabravo

Joined Feb 24, 2006
21,225
Depends on the presence or absence of array bounds checking. In the freewheeling early days it was unlikely to be there. Such things can very occasionally be useful but more often than not are a source of great pain and frustration. Evaluating such an expression is probably benign; deferencing is probably not benign
 

Thread Starter

meera

Joined Jun 7, 2007
6
thanks a lot to all of your reply.
yup, the result is represent address of the pointer 1st array..i check it on FileRegister and get the answer.

Thanks
 
Top