You are here:

C/Declaration of pointer

Advertisement


Question
QUESTION: I'm so glad I found this nice site to ask a question that is killing me.
I fairly know how pointers work on C, but I can figure for the life of me how this declaration works:

#define GPIO_PORTA_DATA_R (*((volatile unsigned long*)0x400043FC))

-I understand that GPIO.. gets assigned the address 0x40004..Is that right? If so why use (* at the beginning?
Could you elaborate how to interpret what is actually assigned to GPIO..?
-I have several books on C language, but none use such strange way of declaring a pointer. Could you point me to a source material where I can study these kind of pointer uses?

Thanks a lot in advance for your time to answer me.
All the best,
Elkin

ANSWER: Hi Elkin,

This statement casts 0x400043FC to a unsigned long pointer, volatile is used to prevent the value from being optimized. The outer "*" dereferences the pointer to a unsigned long. The actual value of the unsigned long is whatever exists in memory at the defined memory address. If you are interested in understanding what is happening in this statement, then reading about pointer arithmetic in C is a good place to start.

-Tony

Example:



volatile unsigned long * x = malloc(sizeof(unsigned long));
unsigned long value = 123;
memcpy(x, &value, sizeof(unsigned long));
printf("The address %p holds value %lu\n", x, *x);
//The address 0x1441010 holds value 123


---------- FOLLOW-UP ----------

QUESTION: Thanks Tony for your answer. But I'm confused aboout the second "*". Does it mean that memory address 0x400043FC is assigned as pointer?

Elkin

Answer
Hi Elkin,

The statement is evaluated in the following steps.
1. (volatile unsigned long*)0x400043FC ,The memory address is cast to a unsigned long pointer.
2. * , the pointer is dereferenced to the unsigned long value at address 0x400043FC.
I hope this answers your question.

-Tony

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tony Nazzal

Expertise

I will answer any c language programming and syntax questions.

Experience

I have almost a decade of c programming experience.

Education/Credentials
I have a bachelor of science degree and several years of industry experience.

©2016 About.com. All rights reserved.