Interpreting C pointers, dereferencing and indirection

  • Thread starter Basher1
  • Start date
  • Tags
    Pointers
In summary, the first statement stores a NULL value into memory location "pt" which is then pointed to by the second statement. The third statement sets "pt" to point to the value "NULL".
  • #1
Basher1
27
0
I have been going through a book on embedded development. I have been followin the C programs in the book with about 95% success as C is not completely new to me. But I am new to pointers so I was hoping someone would kindly clear a few things for me.

#define INPUT (*((volatile unsigned long *)0x40004004))

the hex number 0x40004004 represents the address of an general purpose I/O port for a microcontroller. Now I know that one of the * symbols(I don't know exactly which one, but I am guessing the first one from the left) represents the indirection operator which treats the address as an address and not a literal number. I also know that the volatile unsigned long is used so the compiler knows it needs to read a 32-bit unsigned number from the address. But I don't know why the there is another * in this statement.

another two examples that are leaving me stumped

*(long*)pt = NULL

and

FreePt = (long*)*pt

how do I interpret these?
 
Physics news on Phys.org
  • #2
The first star is saying that the variable is a pointer and the parenthetical (long *) is casting the variable to be a volatile unsigned long pointer. Volatile means the compiler should leave it alone and not optimize code related to it and unsigned long pointer.

http://en.wikipedia.org/wiki/Volatile_variable

I'd say the statement *(long*)pt = NULL means store a NULL value into the memory location where the pt points and to store a long value (4 bytes for some C systems) ie fill it with 4 bytes of zeros.

Similarly the statement FreePt = (long*)*pt tells the computer to get the value pointed at by pt and store it in FreePt. the (long *) says the value is in fact a long pointer so you're using a pointer to retrieve a pointer and assigning it to FreePt so somewhere in your code you should see a declaration for FreePt as long *FreePt;

Here's some more on pointers and advanced pointers:

http://www.thegeekstuff.com/2011/12/c-pointers-fundamentals/
 
Last edited:
  • Like
Likes 1 person
  • #3
Basher1 said:
#define INPUT (*((volatile unsigned long *)0x40004004))
"INPUT" could be used as follows:
Code:
{
  unsigned long ulLatestInput1, ulLatestInput2;

  ulLatestInput1 = INPUT;
  ulLatestInput2 = INPUT;
  if(ulLatestInput1==ulLatestInput2) ...
}
Interpreted as follows:
Hex value 0x40004004 is being cast as a volatile pointer to an unsigned long value. So, there is a 32 bit value at memory address 0x40004004. The fact that it is being declared "volatile" indicates that it can change at any moment. If there "volatile" was not declared, then in the my code snippet above the compiler might optimize the code to effectively "ulLatestInput2 = ulLatestInput1 = INPUT;" so that the "if" would always evaluate to true. The "volatile" declaration would prevent this optimization so that the memory location was reexamined before setting "ulLatestInput2".

The other asterisk is the first one. The term "((volatile unsigned long *)0x40004004)" is a pointer to a memory location. If you want to read that value, your need to preceded it with an asterisk or follow is with "[0]".

Basher1 said:
*(long*)pt = NULL

FreePt = (long*)*pt
In the first statement, there is a pointer "pt". You haven't specified its type, but I will presume it to be a short pointer ("short *"), and is pointing into an array of short values.
So in the first statement, the "(long *)" is causing "pt" to be recast as a long pointer. It's processor dependent, but this probably means it's being interpreted as pointing to a 32-bit value instead of a 16-bit value. Consider this code:
Code:
{
#define NULL (0)
  short naArray[5], *pt;

  pt = naArray+2;
  *(long *)pt = NULL;
}
So we have set pt to point to the third element in array "naArray". Exactly what happens with "*(long *)pt = NULL;" depends of whether the machine you are working on is Big Endian or Little Endian. In both cases, naArray[2] is set to zero, but since "(long *)pt" points to a 32-bit value, either naArray[1] or naArray[3] will also be set to zero - depending on your computer's Endian.

FreePt must have been declared "long *". It is being set to point to the same value that was just "NULL"ed in the previous statement.
 
  • #4
Thats the thing. He's already declared "*Pt" as long. Sorry, I should have specified this information.
 
  • #5



I can provide an explanation for the concepts of pointers, dereferencing, and indirection in C programming.

Pointers in C are variables that store memory addresses instead of actual values. They are used to indirectly access and manipulate data stored in memory. The asterisk symbol (*) is used to declare a pointer variable, and the ampersand symbol (&) is used to get the address of a variable.

Dereferencing a pointer means accessing the value stored at the memory address pointed to by the pointer. In your example, the first * symbol in the line #define INPUT (*((volatile unsigned long *)0x40004004)) is the dereferencing operator. It tells the compiler to read the value stored at the memory address represented by the hex number 0x40004004.

Indirection, on the other hand, is the process of accessing a memory address indirectly through a pointer. In your example, the second * symbol in the line #define INPUT (*((volatile unsigned long *)0x40004004)) is the indirection operator. It tells the compiler to treat the hex number 0x40004004 as a memory address and not a literal number.

In the line *(long*)pt = NULL, the first * symbol is the dereferencing operator, and the second * symbol is the indirection operator. This statement assigns the value NULL to the memory address pointed to by the pointer variable pt.

In the line FreePt = (long*)*pt, the first * symbol is the indirection operator, and the second * symbol is the dereferencing operator. This statement assigns the value stored at the memory address pointed to by pt to the pointer variable FreePt.

Overall, pointers, dereferencing, and indirection are important concepts in C programming that allow for efficient memory management and manipulation. It is important to understand their usage and syntax in order to successfully write and debug C programs.
 

Related to Interpreting C pointers, dereferencing and indirection

What is a pointer in C?

A pointer in C is a variable that holds the address of another variable. It allows for indirect access to a specific location in memory, which is useful for manipulating data and creating dynamic data structures.

How do you declare a pointer in C?

To declare a pointer in C, you use an asterisk (*) before the variable name. For example, to declare a pointer to an integer, you would write int *ptr;

What is dereferencing a pointer in C?

Dereferencing a pointer in C means accessing the value stored at the memory address that the pointer is pointing to. This is done by using the asterisk (*) operator before the pointer's name. For example, *ptr will give you the value stored at the address stored in the variable ptr.

What is the difference between pointer indirection and pointer arithmetic in C?

Pointer indirection refers to accessing the value stored at the memory address that a pointer is pointing to. Pointer arithmetic refers to performing arithmetic operations on a pointer, such as incrementing or decrementing its value. Pointer arithmetic is used to navigate through arrays and other data structures in memory.

How do you use pointers to create and manipulate data structures in C?

Pointers are essential for creating and manipulating data structures in C. By using pointers, you can dynamically allocate memory and create linked data structures such as linked lists, trees, and graphs. Pointers also allow for efficient manipulation of complex data structures by providing direct access to different elements within the structure.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
5K
  • Programming and Computer Science
Replies
17
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
19
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Replies
1
Views
1K
Back
Top