Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an address be assigned to a variable in C?

Is it possible to assign a variable the address you want, in the memory?

I tried to do so but I am getting an error as "Lvalue required as left operand of assignment".

int main() {
  int i = 10;
  &i = 7200;
  printf("i=%d address=%u", i, &i);
}

What is wrong with my approach? Is there any way in C in which we can assign an address we want, to a variable?

like image 740
poorvank Avatar asked Nov 26 '12 05:11

poorvank


1 Answers

Not directly. You can do this though : int* i = 7200; .. and then use i (ie. *i = 10) but you will most likely get a crash. This is only meaningful when doing low level development - device drivers, etc... with known memory addreses.

like image 151
docdocdoc9 Avatar answered Sep 30 '22 01:09

docdocdoc9