Address of char array

Pages: 12
Hi,

I create a char pointer, and then when I try to count the memory address of the first char, I get the entire string:

1
2
char* test = "Testing 123";
cout << endl << " address of first char is " << &test[0] << endl;


This prints out "Testing 123".

Not sure I understand the logic here?

Thanks

i
The << operator is overloaded to print the whole cstring when you provide a char* argument.

Try (int*) &test[0].
Last edited on
Hi,

Thanks, I will try. But I am not "cout" ing a char*, I am counting the address of the first element? Are you saying that &test[0] and *test are equivalent?

Regards

i
Hi,

It works thanks. As does :

 
cout << &test;


Although still dont understand why

1
2
cout >> &test[0];
cout >> test; 


are equivalent? Is it because &test[0] is just a pointer to a char due to &?

Regards

i
Last edited on
indy2005 wrote:
Are you saying that &test[0] and *test are equivalent?
Yes. No. &test[0] and test are equivalent.

Also, in general, the address of something of type T is of type T*
(i.e. &test[1] is of type char*, as test[1] is of type char).

EDIT: Correction...
Last edited on
Thanks.
Also, you cast to (int*) to get an address. Should this be an unsigned int?

Regards

i
Last edited on
indy2005 wrote:
Should this be an unsigned int?

It can be anything other than char. Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{
    char* test = "Testing 123";
    cout << (int*)&test[0] << endl;
    cout << (double*)&test[0] << endl;
    cout << (bool*)&test[0] << endl;
    cout << (void*)&test[0] << endl;

    cin.get();
    return 0;
}

Last edited on
Thanks,

Dont think I will ever grasp C ;-)

i
Hello indy2005.
You should just take a couple of days to read about pointers and you will grasp C just fine.

int array1[30];
- array1 points to the memory zone where the 30 int array begins.
int *array2;
- array2 points to the memory zone where a virtually unlimited int array begins, where the size is limited by the available memory.

You can quickly realize that array[i] and *(array+i) represent the same thing,since "array" is a pointer.
Declaring int array[30]; is meant to be used when you know the number of elements in the array (in our case 30),
while int* array; is used when you don't know how many elements the array will contain. Read about dynamic allocation and you'll make another big step in grasping C.
Are you saying that &test[0] and *test are equivalent?

Yes. No. &test[0] and test are equivalent.
I think m4ster's idea is wrong. In fact, I have a try to check your answer in VC, but the result is bad.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main()
{
	char *test = "Testing 123 ";
	cout << test[0] << endl;
	cout << *test << endl;
	cout << test << endl;

	return 0;
}


T
T
Testing 123

This show reveal that &test[0] and *test are equivalent.
Last edited on
zijuan0810 wrote:
This show reveal that &test[0] and *test are equivalent.

Nope, it reveals that test[0] and *test are equivalent :P

Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
    char *test = "Testing 123 ";
    
    //don't forget &
    cout << &test[0] << endl;
    
    cout << *test << endl;
    cout << test << endl;
    
    return 0;
}
Yes, I'm wrong.
Great thanks for m4ster r0shi !
Thanks all.

I think what confuses me is the multiple uses of * and & (not even sure if & has different meanings).

For example what you can pass into &parameters into functions, and what you can assign to &returntype functions.

Regards

i
Hi,

its simplest form, this will help you.
1
2
3
char* test = "Testing 123";
cout << endl << " address of first char is " << &test << endl;
 
Last edited on
@firix: That's not what the OP is looking for. That will try to take the address of the pointer itself, not the first character.
@zhuge

ok.
I thought try to take the address of the pointer itself

first address:

cout << (int*)&test[0] << endl;
Hi,

Why are we casting to (int*) i.e. pointer to int, to get the address of the first element?

Regards

i
Ok, I'll try to explain more. But first we must talk about function overloading. Take a look at this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

void print(int n)
{
    cout << "int overload called!" << endl;
    cout << n << endl;
}

void print(char c)
{
    cout << "char overload called!" << endl;
    cout << c << endl;
}

int main()
{
    int a;

    a=65;

    print(a); // prints 65
    print((char)(a)); // prints A

    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}

Notice that both functions have the same name (print) but they accept different argument types (int, char).
The compiler is not confused though; it knows the right function to call by looking at the argument you pass.
So, the first call of the print function in main calls the int overload, while the second one calls the char overload.

The << operator is also an overloaded function. Its behavior depends on the argument you pass.
The char * overload prints the whole cstring, while the other pointer overloads print the address.

Useful links:

http://cplusplus.com/doc/tutorial/functions/
http://cplusplus.com/doc/tutorial/functions2/
OK,

Thanks. cout and its overloads can be a bit confusing when you are trying to learn pointers, as you are grappling with cout behaviour as you describe also. I wasnt sure why you need to cast to a pointer just to get the address, but cout is overloaded to behave in a way which prints out the address of an int* pointer in hex?

Regards

i
Last edited on
indy2005 wrote:
cout is overloaded to behave in a way which prints out the address of an int* pointer in hex?

Yes, cout is overloaded to print int pointers in hex. Ok, I now understand what you want.
If you want to print the address in base 10, then, yes, you should cast it to an (unsigned) int/long.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main()
{
    char *test = "Testing 123 ";

    cout << (unsigned long) &test[0] << endl;

    return 0;
}
Pages: 12