1

I was reading through a C program which interfaces with hardware registers. The person has been using hexadecimal numbers as the index to an array such as :

app_base_add[0x30]  

I know that a[i] means *(a+i) which is *(a+(i*sizeof(typeof(a)))) so a hexadecimal index is probably a offset of the desired memory location in the address space w.r.t app_base_add.

Is this right?
And also given , say :

#define mm2s_start_add 0xc0000000;  

how would these assignments be different from each other in usage?

volatile unsigned int *app_base_add;  
app_base_add[0x30>>2]=0x1;    
app_base_add[0x30>>2]=1<<2;  
app_base_add[0x30>>2]=((unsigned int)mm2s_start_add);  //is this assignment valid??
app_base_add[0x30>>2]=((unsigned int *)mm2s_start_add);
3
  • 3
    Whether you write the index in hexadecimal or decimal (or octal, if you wish) notation makes no difference to the compiler. None whatsoever. The last assignment is a bit problematic because you first cast an integer to a pointer, then that pointer back to an integer. If the compiler is lenient, it may compile it as is, otherwise you'd need an explicit cast to an integer type after casting to a pointer type. Commented Oct 19, 2012 at 11:39
  • @DanielFischer: Hexadecimal and decimal do make a difference to the compiler. This is unlikely to matter in array indices, but there can be effects. See my comment to one of the answers below. Commented Oct 19, 2012 at 16:39
  • @EricPostpischil Right, if you want negative indices, you may need an explicit cast to a signed type with hexadecimal notation. Although the cases where it matters are rare, I should have mentioned it. Commented Oct 19, 2012 at 17:18

3 Answers 3

2

There is no difference writing 0x30 or 48 as index, it may just be easier to read for the programmer if say his documentation of the memory was written only with hex values but its just a matter of taste.

e.g.

app_base_add[0x30>>2]=0x1; 
is the same as writing app_base_add[12]=0x1; 
or even app_base_add[0x0C]=0x1; 
Sign up to request clarification or add additional context in comments.

Comments

1

At compile time, all values are treated them same way, even if they are written in hexadecimal, binary or decimal.

0x2a == 42 == b101010 == 052

The only assignement which could throw a warning is the cast to unsigned int, because your destination type is not an unsigned int

1 Comment

It is not correct to state that values are treated the same way regardless of the base used. Hexadecimal values that are not representable in an int have the type unsigned int (if they are representable in that), whereas decimal values that are not representable in an int have the type long int (if they are representable in that). This is unlikely to matter in array indices, but it can have effects. With common 32-bit ints and 64-bit longs, a[2147483648-2147483649] is a[-1] but a[0x80000000-0x80000001] is a[4294967295].
0

volatile unsigned int *app_base_add;
//app_base_add is a pointer to volatile memory(not initialized :()

app_base_add[0x30>>2]=0x1; // = app_base_add[12] = 1;

app_base_add[0x30>>2]=1<<2; // = app_base_add[12] = 4;

app_base_add[0x30>>2]=((unsigned int)mm2s_start_add); //is this assignment valid?? // yes its valid // = app_base_add[12] = 3221225472

app_base_add[0x30>>2]=((unsigned int *)mm2s_start_add); // = app_base_add[12] = interpret 3221225472 as an unsigned integer pointer and store it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.