-1

I have code as below :

unsigned int* Adc = 0x2000;

As my understanding the address of ADC is 0x2000, is it right ?
And if I want to assign value to Adc, i just write like this :

Adc = 88;

Is it correct or not ?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Rambe
  • 17
  • 1
  • No it is not correct, this is not valid C code. You'll want something like `volatile uint16_t* Adc = (volatile uint16_t*)0x2000; .. *Adc = 88;` – Lundin Sep 04 '18 at 08:33
  • @Lundin the duplicate is backwards actually – Antti Haapala -- Слава Україні Sep 05 '18 at 09:47
  • @AnttiHaapala Gah, you are correct. They say one thing in the title then do another. Not a good canonical dupe. Regardless, you can't do it the other way around either, for the same reasons. I'll dig for a better canonical post. – Lundin Sep 05 '18 at 11:18
  • @AnttiHaapala I found no good canonical dupe but this is a very common FAQ. So I ended up writing a Q&A style one myself: [“Pointer from integer/integer from pointer without a cast” issues](https://stackoverflow.com/questions/52186834/pointer-from-integer-integer-from-pointer-without-a-cast-issues). Proof-reading much appreciated :) – Lundin Sep 05 '18 at 13:53

1 Answers1

3

As my understanding the address of ADC is 0x2000, is it right ?

Almost, unsigned int* Adc = 0x2000; initializes Adc with that value. If 0x2000 is not a valid value to assign that pointer, the result is undefined behavior (UB), so hopefully there is some system dependent info indicating this is a valid address for an unsigned pointer.

The pointer Adc exists someplace in memory at some address. The address of Adc makes little difference to your goal.

To be clear, the pointer Adc has a value of 0x2000 and Adc exist someplace in memory at some address.

Is it correct or not ?

Not quite. Adc = 88; assigns a new value to the pointer Adc from its original 0x2000.

To change the memory pointed to by Adc, de-reference it: *Adc = 88;

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 1
    Well `Adc = 88` doesn't assign, because it is a constraint violation... what an implementation would do then is up to the implementation. – Antti Haapala -- Слава Україні Sep 04 '18 at 04:10
  • @AnttiHaapala Assigning an integer to a pointer is implementation-defined, not a constraint violation. – Barmar Sep 04 '18 at 04:18
  • 1
    @Barmar an integer cannot be converted to a pointer without an explicit cast, except if it is the null pointer constant (same goes for the initialization too). – Antti Haapala -- Слава Україні Sep 04 '18 at 04:26
  • @AnttiHaapala, Sorry i still not fulyy get it. Between these 2 codes : unsigned int* Adc = 0x2000; and unsigned int Adc = 0x2000; The difference is first Adc is Pointer and the second Adc is variable ? But both have same value that 0x2000, Is it Correct? – Rambe Sep 04 '18 at 04:50
  • @Rambe They are both variables. The first is a pointer to an `unsigned int`, the other is an `unsigned int`. There is nothing special about pointers. – molbdnilo Sep 04 '18 at 08:09
  • @Barmar It is a constraint violation of simple assignment C17 6.5.16.1 §1. – Lundin Sep 04 '18 at 08:35
  • @Lundin Yeah, someone else already clarified that. I was just thinking about the conversion, not the castless syntax. – Barmar Sep 04 '18 at 08:37
  • @AnttiHaapala I understand now, thank you for your explanation. – Rambe Sep 05 '18 at 09:35