Address of char array

Pages: 12
Dec 10, 2010 at 8:03pm
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
Dec 10, 2010 at 8:07pm
The << operator is overloaded to print the whole cstring when you provide a char* argument.

Try (int*) &test[0].
Last edited on Dec 10, 2010 at 8:13pm
Dec 10, 2010 at 8:32pm
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
Dec 10, 2010 at 8:39pm
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 Dec 10, 2010 at 8:49pm
Dec 10, 2010 at 8:41pm
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 Dec 10, 2010 at 10:40pm
Dec 10, 2010 at 8:49pm
Thanks.
Also, you cast to (int*) to get an address. Should this be an unsigned int?

Regards

i
Last edited on Dec 10, 2010 at 8:50pm
Dec 10, 2010 at 8:54pm
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 Dec 10, 2010 at 8:56pm
Dec 10, 2010 at 11:38pm
Thanks,

Dont think I will ever grasp C ;-)

i
Dec 11, 2010 at 3:31am
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.
Dec 11, 2010 at 6:17am
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 Dec 11, 2010 at 6:19am
Dec 11, 2010 at 10:59am
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;
}
Dec 11, 2010 at 5:42pm
Yes, I'm wrong.
Great thanks for m4ster r0shi !
Dec 11, 2010 at 9:08pm
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
Dec 11, 2010 at 10:11pm
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 Dec 11, 2010 at 10:12pm
Dec 11, 2010 at 11:16pm
@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.
Dec 12, 2010 at 7:54am
@zhuge

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

first address:

cout << (int*)&test[0] << endl;
Dec 12, 2010 at 3:26pm
Hi,

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

Regards

i
Dec 12, 2010 at 3:50pm
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/
Dec 12, 2010 at 4:08pm
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 Dec 12, 2010 at 4:10pm
Dec 12, 2010 at 4:39pm
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