Newbie: pointerof string array

Z

Zalek Bloom

Hello,

Why a pointer of a string array does not prints any address, but a
string itself? Here is my test:

#include <iostream.h>
#include <string.h>
#include <stdlib.h>

void main()
{
char c[] = "the string";
char * pc = &c[0] ;

cout << "string c = " << c << endl;
cout << "pointer of c = " << pc << endl ;
}

Here are results:

string c = the string
pointer of c = the string

Why pointer of c - pc - does not prints any address?

Thanks,

Zalek
 
Z

Zalek Bloom

Zalek Bloom said:
Hello,

Why a pointer of a string array does not prints any address, but a
string itself? Here is my test:

#include <iostream.h>
#include <string.h>
#include <stdlib.h>

void main()
{
char c[] = "the string";
char * pc = &c[0] ;

cout << "string c = " << c << endl;
cout << "pointer of c = " << pc << endl ;
}

Here are results:

string c = the string
pointer of c = the string

Why pointer of c - pc - does not prints any address?

Thanks,

Zalek

What you have is a pointer to char, not a pointer to a string arrays.
Pointers to char are one way of representing strings in C++. If you really
want to see the address cast to void*.

cout << "pointer of c = " << (void*)pc << endl ;

You can do this with c as well (BTW c is not a string, its a char array).
You need to get the definitions of terms like string, array and pointer
sorted out.

cout << "string c = " << (void*)c << endl;

john

John,
Thanks for explanation, but I have problem to understand your
sentence:
"Pointers to char are one way of representing strings in C++".

Does this mean that string: char c[] = "string";
and the pointer to char: char *pc = &c[0]
both are the same thing? What is a difference between them?

Thanks,

Zalek
 
R

Rolf Magnus

Zalek said:
Hello,

Why a pointer of a string array does not prints any address, but a
string itself? Here is my test:

#include <iostream.h>

This is not a standard header.
#include <string.h>
#include <stdlib.h>

You're not using anything from those two C headers.
void main()

main() _must_ return int.
{
char c[] = "the string";
char * pc = &c[0] ;

cout << "string c = " << c << endl;

In this place, c is implicitly converted to a const char* that points to
the first element of your array, and that pointer goes to the specified
operator<<, which interprets that pointer as one to the first character
of a C style string and writes that string to the standard output.
cout << "pointer of c = " << pc << endl ;

Here, you use an explicitly converted pointer, but the rest is the same,
so again, the string is printed.
 
J

John Harrison

Zalek Bloom said:
Zalek Bloom said:
Hello,

Why a pointer of a string array does not prints any address, but a
string itself? Here is my test:

#include <iostream.h>
#include <string.h>
#include <stdlib.h>

void main()
{
char c[] = "the string";
char * pc = &c[0] ;

cout << "string c = " << c << endl;
cout << "pointer of c = " << pc << endl ;
}

Here are results:

string c = the string
pointer of c = the string

Why pointer of c - pc - does not prints any address?

Thanks,

Zalek

What you have is a pointer to char, not a pointer to a string arrays.
Pointers to char are one way of representing strings in C++. If you really
want to see the address cast to void*.

cout << "pointer of c = " << (void*)pc << endl ;

You can do this with c as well (BTW c is not a string, its a char array).
You need to get the definitions of terms like string, array and pointer
sorted out.

cout << "string c = " << (void*)c << endl;

john

John,
Thanks for explanation, but I have problem to understand your
sentence:
"Pointers to char are one way of representing strings in C++".

Does this mean that string: char c[] = "string";
and the pointer to char: char *pc = &c[0]
both are the same thing? What is a difference between them?

The first is an array (because you wrote []) the second is a pointer
(because you wrote *).

On the other hand a string (in the sense you are using) is a sequence of
chars terminated by the null character. An array can hold the chars to form
a string, and a pointer can point to the first char in a string. So both a
char array and a pointer to char can represent a string, but neither
actually is a string.

As to the difference between arrays and pointers, that's quite subtle. For
many purposes they are the same, you can use [] on both pointers and arrays
for instance

char a[] = "abc";
char* p = "abc";

cout << a[0] << p[1];

One difference is that you cannot assign to an array

a = "x"; // illegal
p = "y"; // ok

But really you need a good book to explain these things. Any good book will
have a large section on the differences and similarlities between pointers
and arrays.
Thanks,

Zalek

john
 
G

Greg Comeau

Hello,

Why a pointer of a string array does not prints any address, but a
string itself? Here is my test:

#include <iostream.h>
#include <string.h>
#include <stdlib.h>

void main()
{
char c[] = "the string";
char * pc = &c[0] ;

cout << "string c = " << c << endl;
cout << "pointer of c = " << pc << endl ;
}

Here are results:

string c = the string
pointer of c = the string

Why pointer of c - pc - does not prints any address?

There is a point where it can be argued that arrays and
pointers in C are broken. That said, the most common
use tends to be to want the characters of a string.
If you want to print raw addresses of things, try something
like a reinterpret_cast to void *.
 
G

Greg Comeau

Zalek Bloom wrote:
main() _must_ return int.
{
char c[] = "the string";
char * pc = &c[0] ;

cout << "string c = " << c << endl;

In this place, c is implicitly converted to a const char*

To char * not const char *.

Just to avoid confusion... since c is a char[], it will most
directly collapse to a char *. However, one also notes
that it will be passed to the appropriate op<<, and hence
"become" a const char *, hence another "level" of
implicit conversion. Of course, it is no longer c
at these points.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top