473,382 Members | 1,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Abridged FAQ Question

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~
Feb 6 '06 #1
7 1547
dave <da**@comteck.com> writes:
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 6 '06 #2
dave wrote:
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.


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~

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Feb 6 '06 #3
On Mon, 06 Feb 2006 09:25:23 GMT, Keith Thompson <ks***@mib.org> wrote
in comp.lang.c:
dave <da**@comteck.com> writes:
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);
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.)


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 7 '06 #4
Jack Klein <ja*******@spamcop.net> writes:
On Mon, 06 Feb 2006 09:25:23 GMT, Keith Thompson <ks***@mib.org> wrote
in comp.lang.c:

[...]
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);


Yes, thanks.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 7 '06 #5
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 写道:
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~


Feb 7 '06 #6
"fangshi" <fa***********@gmail.com> writes:
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 7 '06 #7
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"; ?

<snip>
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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Feb 7 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Spencer | last post by:
An Abridged Python Tutorial There are tips for the novice and tricks that will add to your programming kicks. But the cardinal rule that you must learn at school is that spaces and tabs never...
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.