Nikhil Singh
Nikhil Singh

Reputation: 81

c++: Pointers and address

#include <iostream>

using namespace std;

void test_func(char *data,int length)
{
  cout<<"data:"<<data<<endl;
  cout<<"length:"<<length<<endl;

  void *addr=data;
  cout<<"*addr:"<<addr<<endl;
  cout<<"Address of data:"<<&data<<endl;
  cout<<"data again:"<<*(&data)<<endl;
}

int main()
{
  char msg[]="Hello world!";
  test_func(msg,6);

  return 0;
}

I was trying some other program where I came accrross void *addr=data. so I tried to understand it by writing a separate program for it. And here I am not able to understand what is the value stored in addr. I got the output of following program as:

data:Hello world!
length:6
*addr:0x794e0553d310
Address of data:0x794e0553d2e8
data again:Hello world!

Any help would be appreciated. Thanks in advance.

Upvotes: 1

Views: 94

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 310910

When an array designator is passed to a function as an argument it is converted to a pointer to its first element.

Function parameters are local variables of the function.

Thus you can imagine function test_func along with its call as test_func(msg,6); the following way

void test_func( /* char *data,int length */)
{
    char *data = msg;
    int length = 6;

    cout<<"data:"<<data<<endl;
    cout<<"length:"<<length<<endl;

    void *addr=data;
    cout<<"*addr:"<<addr<<endl;
    cout<<"Address of data:"<<&data<<endl;
    cout<<"data again:"<<*(&data)<<endl;
}

In this case expression &data used in statement

cout<<"Address of data:"<<&data<<endl;

is the address of function local variable data Accotding to the output

Address of data:0x794e0553d2e8

variable data is placed at address 0x794e0553d2e8 inside the function. The variable itself contains the address of the first character of array msg because it was assigned such a way when the function was called.:)

    char *data = msg;

So the address of the variable data and the value stored in the variable differ.

As for this expression

*(&data)

then it is equivalent to expression

data

Thus statement

cout<<"data again:"<<*(&data)<<endl;

outputs the string the address of the first character of which is stored in pointer data.

operator << is overloaded for pointers of type char * It considers the object pointed to by such a pointer as a string and outputs it accordingly. Take into account that &data has type char **. It is not the same as char *. So for pointers of type char ** (and of other types) there is used overloaded operator << that interpretates such pointers as const void * and simply outputs the corresponding value.

Upvotes: 0

Noel
Noel

Reputation: 770

operator << is overloaded for cout, so if you pass a char* it prints the content (even though char* is a pointer).

However, if you pass a void pointer (addr in your example) it will print the address itself. So addr is the address of the first element of the message.

On the other hand, &data is a pointer to a pointer, so cout prints the address of data (with data being a pointer to your message).

Upvotes: 2

Steephen
Steephen

Reputation: 15824

void *addr=data"

When you write above statement, addr is a pointer of type void* and which has an address in memory. And it will be different from the address of the variable data of type char*. So when you print as follows

cout<<"*addr:"<<addr<<endl;
cout<<"Address of data:"<<&data<<endl;

Each statement will print corresponding address of those variables.

*addr=data;

The above statement will not change the address of variable addr instead it will redirect the address location of addr to the address location of data.

Upvotes: 0

TartanLlama
TartanLlama

Reputation: 65580

data is a pointer to the first char of a string, which is how strings are implemented in C. When you assign it to addr, you are just getting rid of the static type of data.

The reason you get different results when you output addr and data is that std::cout treats char* as a special case: it is output as a string rather than having its pointer value printed. void* doesn't share this, so you just see the address.

Upvotes: 0

Related Questions