Abridged FAQ Question

D

dave

From the FAQ:

1.32: What is the difference between char a[] = "string"; and
char *p = "string"; ?

A: The first declares an initialized and modifiable array; the
second declares a pointer initialized to a not-necessarily-
modifiable constant string.


I have this code in gcc (yes, it's C++, but it is a C question):

char * char_ptr = "stringthingy";

cout << "Size of char_ptr is " << sizeof(char_ptr) << "\n\n";
cout << "*char_ptr = " << *char_ptr << "\n";
cout << "char_ptr = " << char_ptr << "\n";

And this is the result:

Size of char_ptr is 4

*char_ptr = s
char_ptr = stringthingy

So sizeof() returns 4 as expected and the dereferenced pointer
returns 's' as expected. But why isn't an address returned for
the non-dereferenced pointer? If "stringthingy" is correct, why
doesn't sizeof() return 13?

I am confused!


TIA,

~Dave~
 
K

Keith Thompson

dave said:
From the FAQ:

1.32: What is the difference between char a[] = "string"; and
char *p = "string"; ?

A: The first declares an initialized and modifiable array; the
second declares a pointer initialized to a not-necessarily-
modifiable constant string.


I have this code in gcc (yes, it's C++, but it is a C question):

char * char_ptr = "stringthingy";

cout << "Size of char_ptr is " << sizeof(char_ptr) << "\n\n";
cout << "*char_ptr = " << *char_ptr << "\n";
cout << "char_ptr = " << char_ptr << "\n";

And this is the result:

Size of char_ptr is 4

*char_ptr = s
char_ptr = stringthingy

So sizeof() returns 4 as expected and the dereferenced pointer
returns 's' as expected. But why isn't an address returned for
the non-dereferenced pointer? If "stringthingy" is correct, why
doesn't sizeof() return 13?

It would have been much better to post C code. The behavior of the
C++ code you posted depends on the way C++ operator overload works.
It's also helpful to post a complete program rather than a code
fragment.

Here's a C program that corresponds to the C++ code you posted:

#include <stdio.h>
int main(void)
{
char *char_ptr = "stringthingy";

printf("Size of char_ptr is %d\n\n", sizeof char_ptr);
printf("*char_ptr = %c\n", *char_ptr);
printf("char_ptr = %s\n", char_ptr);
return 0;
}

In the last printf() call, the "%s" format expects a char* value that
points to a string, and that's what you give it. The pointer is
dererenced inside printf() itself.

If you wanted to print the pointer value itself, you could use the
"%p" format, which expects a void*:

printf("char_ptr = %p\n", (void*)char_ptr);

(The cast to void* isn't strictly necessary in this case for obscure
reasons, but it's a good idea.)
 
J

Joe Wright

dave said:
From the FAQ:

1.32: What is the difference between char a[] = "string"; and
char *p = "string"; ?

A: The first declares an initialized and modifiable array; the
second declares a pointer initialized to a not-necessarily-
modifiable constant string.
I'm going to ignore the C++ code below. It is not topical. Perhaps the
Answer above might be confusing you. Let me try.

char a[] = "string";

...describes an array of char in memory, the first character of which is
at address a. Now..

char *p = "string";

...describes a pointer which holds the address of the anonymous string.
This is two objects, the pointer and the string. In the first case we
have only one object, the array a.
 
J

Jack Klein

dave said:
From the FAQ:

1.32: What is the difference between char a[] = "string"; and
char *p = "string"; ?

A: The first declares an initialized and modifiable array; the
second declares a pointer initialized to a not-necessarily-
modifiable constant string.


I have this code in gcc (yes, it's C++, but it is a C question):

char * char_ptr = "stringthingy";

cout << "Size of char_ptr is " << sizeof(char_ptr) << "\n\n";
cout << "*char_ptr = " << *char_ptr << "\n";
cout << "char_ptr = " << char_ptr << "\n";

And this is the result:

Size of char_ptr is 4

*char_ptr = s
char_ptr = stringthingy

So sizeof() returns 4 as expected and the dereferenced pointer
returns 's' as expected. But why isn't an address returned for
the non-dereferenced pointer? If "stringthingy" is correct, why
doesn't sizeof() return 13?

It would have been much better to post C code. The behavior of the
C++ code you posted depends on the way C++ operator overload works.
It's also helpful to post a complete program rather than a code
fragment.

Here's a C program that corresponds to the C++ code you posted:

#include <stdio.h>
int main(void)
{
char *char_ptr = "stringthingy";

printf("Size of char_ptr is %d\n\n", sizeof char_ptr);

ITYM:
printf("Size of char_ptr is %d\n\n", (int)sizeof char_ptr);
 
F

fangshi

If you want to get the address that p point to you can do like this:
printf("%d",p);
If printf(p) or printf("%s",p) ,we also get string value ,not its
address.
I don't know exactly why isn't an address returned.

dave 写é“:
 
K

Keith Thompson

fangshi said:
If you want to get the address that p point to you can do like this:
printf("%d",p);
If printf(p) or printf("%s",p) ,we also get string value ,not its
address.
I don't know exactly why isn't an address returned.

Please don't top-post. See <http://www.caliburn.nl/topposting.html>
for more information.

If p is a pointer, such as
char *p = "string";
then
printf("%d", p);
*might* "work" on some implementations, but it's dangerously
non-portable (it invokes undefined behavio). Use the "%p" format
to print a pointer value:
printf("%p", (void*)p);

printf(p) can be dangerous, since the string pointed to by p might
contain format specifiers. printf("%s", p) is much safer. (As I
mentioned in another response in this thread, it prints the string
because the pointer is dereferenced inside the printf() function.)
 
F

Flash Gordon

fangshi wrote:

Please don't top post. You response belongs after or intermixed with the
text you are replying to.

Top posting fixed.
From the FAQ:

1.32: What is the difference between char a[] = "string"; and
char *p = "string"; ?

> If you want to get the address that p point to you can do like this:
> printf("%d",p);

No you can't, at least not if you want code that is guaranteed to work.
Your suggestion will fail on real systems. The correct way to print the
value of a pointer is
printf("%p",(void*)p);
> If printf(p) or printf("%s",p) ,we also get string value ,not its
> address.

Your first suggestion [printf(p)] is, in general, a very bad idea. If p
points to the string "99% rubbish" you have just invoked undefined
behaviour and anything can happen, the most likely result being it not
printing what you expected.
> I don't know exactly why isn't an address returned.

Do you mean why an address isn't printed? If so, the answer is you have
not told it to print an address.
 

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

Staff online

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top