1

In this code: Pointer_pu32 points to variable a, but which is pointer Boot_st point to?

 int * Boot_st;
 int a = 15;
 int * Pointer_pu32 = &a;
 Boot_st = (int *)(*Pointer_pu32); /* what was this code line mean? */

When I try to run

printf("Address of A : %ld\n", &a);
printf("Boot_st : %ld\n", Boot_st);
printf("Address Boot_st : %ld\n", &Boot_st);
printf("Pointer_pu32 : %ld\n", Pointer_pu32);

Here is the result:

Address of A: 3996460
Boot_st : 15
Address Boot_st : 3996472
Pointer_pu32 : 3996460

That means: Boot_st contains the value of a, could you explain the behavior of the pointer Boot_st and Pointer_pu32?

3
  • 2
    Also note that the %ld format expects a value of the type long int. And that (void *, cast is needed) pointers should be printed by the %p format. Mismatching printf format specifier and value type leads to undefined behavior. Commented Apr 11, 2022 at 7:01
  • Boot_st = (int *)(*Pointer_pu32); is synonymous with Boot_st = a;, which is assigning an int to an int* and therefore nonsense. Besides using the wrong format specifier for printing pointers (as pointed out, should be %p), printing &Boot_st is worthless. Its address means nothing relevant to the code in question. The address it holds is relevant, but only in so far as it exercised an improper assignment from int in the first place. And none of these has anything to do with either the embedded or null-pointer tags, which consequently should not even be here. Commented Apr 11, 2022 at 7:05
  • Hi WhozCraig , In case : uint32 * Pointer_pu32 = (uint32*) 0x00002004 (manually assigned) and Boot_st = (uint32 *)(*Pointer_pu32) .That mean Boot_st contain value in address 0x00002004 ,right? .Boot_st is another pointer type uint32 in this case Commented Apr 11, 2022 at 7:26

1 Answer 1

3

This code line Boot_st = (int *)(*Pointer_pu32); means

  • take this pointer to an integer Pointer_pu32
  • dereference it (*Pointer_pu32) to read the integer value
  • cast it, to consider that value as a pointer to an integer (int *)
  • write that to a variable of type "pointer to an integer" Boot_st =
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Yunnosch , in caseuint32 * Pointer_pu32 = (uint32*) 0x00002004 (manually assigned) and Boot_st = (uint32 *)(*Pointer_pu32).That mean Boot_st contain value in address 0x00002004 ,right? with (uint32 *Boot_st)
Yes. This is a pattern often used in the context of memory mapped peripherals. Be careful with the data type sizes.

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.