poorvank
poorvank

Reputation: 7612

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?

Upvotes: 7

Views: 24351

Answers (10)

Surya498
Surya498

Reputation: 11

You can do in the windows system with mingw64 setup in visual studio code tool, here is my code

#include<stdio.h>

int main()
{
    int *c;
    c = (int *)0x000000000061fe14; // Allocating the address 8-bit with respect to your CPU arch.
    *c = NULL; // Initializing the null pointer for allocated address
    *c = 0x10; // Assign a hex value (you can assign integer also without '0x')
    printf("%p\n",c); // Prints the address of the c pointer variable
    printf("%x\n",*c); // Prints the assigned value 0x10 -hex 
}

It is tested with mentioned environment. Hope this helps Happy coding !!!

Upvotes: 1

AnT stands with Russia
AnT stands with Russia

Reputation: 320719

C language provides you with no means for "attaching" a name to a specific memory address. I.e. you cannot tell the language that a specific variable name is supposed to refer to a lvalue located at a specific address. So, the answer to your question, as stated, is "no". End of story.

Moreover, formally speaking, there's no alternative portable way to work with specific numerical addresses in C. The language itself defines no features that would help you do that.

However, a specific implementation might provide you with means to access specific addresses. In a typical implementation, converting an integral value Ato a pointer type creates a pointer that points to address A. By dereferencing such pointer you can access that memory location.

Upvotes: 3

Deepankar Bajpeyi
Deepankar Bajpeyi

Reputation: 5879

I think '&' in &a evaluates the address of i at the compile time which i think is a virtual address .So it is not a Lvalue according to your compiler. Use pointer instead

Upvotes: 0

Brenton Fletcher
Brenton Fletcher

Reputation: 1298

Assuming you are on an x86-type processer on a modern operating system, it is not possible to write to aribtray memory locations; the CPU works in concert with the OS to protect memory so that one process cannot accidentally (or intentionally) overwrite another processes' memory. Allowing this would be a security risk (see: buffer overflow). If you try to anyway, you get the 'Segmentation fault' error as the OS/CPU prevents you from doing this.

For technical details on this, you want to start with 1, 2, and 3.

Instead, you ask the OS to give you a memory location you can write to, using malloc. In this case, the OS kernel (which is generally the only process that is allowed to write to arbitrary memory locations) finds a free area of memory and allocates it to your process. The allocation process also marks that area of memory as belonging to your process, so that you can read it and write it.

However, a different OS/processor architecture/configuration could allow you to write to an arbitrary location. In that case, this code would work:

#include <stdio.h>
void main() {
  int *ptr;
  ptr = (int*)7000;
  *ptr = 10;
  printf("Value: %i", *ptr);
}

Upvotes: 4

Drakosha
Drakosha

Reputation: 12165

It's not possible, maybe possible with compiler extensions. You could however access memory at an address you want (if the address is accessible to your process):

int addr = 7200;
*((int*)addr) = newVal;

Upvotes: 0

anishsane
anishsane

Reputation: 20980

Use ldscript/linker command file. This will however, assign at link time, not run time.

Linker command file syntax depends largely on specific compiler. So you will need to google for linker command file, for your compiler.

Approximate pseudo syntax would be somewhat like this:

In linker command file:
.section start=0x1000 lenth=0x100 myVariables
In C file:
#pragma section myVariables
int myVar=10;

Upvotes: 0

docdocdoc9
docdocdoc9

Reputation: 148

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.

Upvotes: 6

Toribio
Toribio

Reputation: 4078

There's probably no way to determine which address a variable will have. But as a last hope for you, there is something called "pointer", so you can modify a value on address 7200 (although this address will probably be inaccessible):

int *i = (int *)7200;
*i = 10;

Upvotes: 0

ouah
ouah

Reputation: 145899

Not portably. But some compilers (usually for the embedded world) have extensions to do it.

For example on IAR compiler (here for MSP430), you can do this:

static const char version[] @ 0x1000 = "v1.0";

This will put object version at memory address 0x1000.

Upvotes: 2

melpomene
melpomene

Reputation: 85887

No.

Even if you could, 7200 is not a pointer (memory address), it's an int, so that wouldn't work anyway.

Upvotes: 0

Related Questions