Max Koretskyi
Max Koretskyi

Reputation: 105547

Does address-of operator return address of value referenced by the variable

I'm reading this chapter on pointers and it states the following:

The address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known as address-of operator. For example:

int myvar = 23...;
foo = &myvar;

Is my understanding correct that it's the address of an object new MyStruct(), not the variable itself? I don't yet have good understanding of how variables (not objects they reference) are stored, and it's very likely that they are not used at all when program is compiled.

Upvotes: 1

Views: 1884

Answers (4)

RazaUsman_k
RazaUsman_k

Reputation: 704

A variable is like a container ( a box ) that has an address. you can change the contents inside the box but you cannot change the address of the box.

Different data types are like boxes of different kind ( each has its purpose ) for example you can't store water in a card board box similarly you can't store a name in an integer

Whenever you use the & operator it gives you the address of the box with which it is used

int a = 5 ; // put 5 in the box a 
int *ptr ;  // creates a special pointer box which can store addresses
ptr = &a ; // takes the address of box a and puts it in the ptr box

pointer is also a different box that can hold addresses so if you just say ptr = a the compiler will give you the error saying that this box cannot store integers

besides the article doesn't uses the word object

Upvotes: 2

There is no object 23..., that is a value. There is an "object" named myvar, and it has an address. That address doesn't change if you assign a new value to the variable, it's a property of the variable itself.

int var = 23;
int *foo = &var;
var = 35; // foo isn't changed here, it holds the same address.

*foo = 42; // again, foo itself isn't modified

assert(var == 42); // But we modified var through the address in foo

As a side note, because you also tagged this C++. I'll just mention that there is certain complexity added to the whole mix, due to the fact C++ allows you to overload operators (among them &) for your own classes. But for primitive variables, using & will always yield the address of said variable.

Upvotes: 5

Thomas Padron-McCarthy
Thomas Padron-McCarthy

Reputation: 27652

It is the address of the variable. The value is stored in the variable itself, not referenced from it.

I think you may have been confused by the word "object"?

When we are talking about C, "object" does not have the same meaning as in for example Python or Java. In C, it means a memory location, not a value. The (draft C11) standard defines an object as "a region of data storage in the execution environment, the contents of which can represent values", so it is the variable itself that is called an "object".

If you do a similar thing in C++, it is the same thing:

MyStruct myvar = MyStruct();
foo = &myvar;

In this code snippet, you still get the address of the variable. C++ is not like Java, where similar code would create a variable that contains the address (in Java called a "reference") of an object, that is stored elsewhere. In C++, the MyStruct instance is stored in the variable.

To get C++ to work like Java, and store the MyStruct instance elsewhere in memory, you need to use explicit pointers:

MyStruct* myvar = new MyStruct();
foo = &myvar;

But the operator & will still give you the address of the variable, not of the MyStruct instance! To get the address of the instance, just use myvar, without the operator.

Upvotes: 1

LPs
LPs

Reputation: 16243

No. You are assigning to foo the address of variable myvar.

If you really wants to have the address of a literal you can do it using compound literals

#include<stdio.h>

int main(void)
{
    int myvar = 23;
    int *foo = &myvar;

    printf ("foo          : %p\n", (void *)foo );
    printf ("myvar address: %p\n", (void *)&myvar );

    printf ("CL address   : %p\n", (void *)(&(int){23}) );

    return 0;
}

Upvotes: 2

Related Questions