Is this correct?

Y

Yang Lee

Hi,

If I write

char *p="hello world";

is this correct in C or do i have to assign memory block and then
strcpy the string to pointer.

If not correct in C , is it allowable in C++ , i seen such syntax in
some books of c++;

thanks
lee
 
J

Jack Klein

Hi,

If I write

char *p="hello world";

is this correct in C or do i have to assign memory block and then
strcpy the string to pointer.

If not correct in C , is it allowable in C++ , i seen such syntax in
some books of c++;

thanks
lee

This is correct in both C and C++.

The pointer p is initialized to point to the first character of the
unnamed array of characters holding the string literal.

In C++, the type of this array is array of constant char. In C the
language does not specify whether the string literal is constant or
not. But in both languages, it is undefined behavior is you try to
modify the string literal.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
D

David

Lee,

Hi,

If I write

char *p="hello world";

is this correct in C or do i have to assign memory block and then
strcpy the string to pointer.

This is correct in C and C++.

The quoted text ("hello world") will be stored in the progams memory
space. The pointer p will point to this string in memory.

The size of the ("hello world") memory block is likely to be 12 bytes.
If you just want this to be used as a constant or for a string space
that is less than the original memory block that is okay. If you were
to copy a larger string over this definition of p, you would overwrite
some part of memory that you do not know what it is. That can be
dangerous.
If not correct in C , is it allowable in C++ , i seen such syntax in
some books of c++;

thanks
lee

David
 
K

Kevin Goodsell

David said:
Lee,




This is correct in C and C++.

The quoted text ("hello world") will be stored in the progams memory
space. The pointer p will point to this string in memory.

The size of the ("hello world") memory block is likely to be 12 bytes.
If you just want this to be used as a constant or for a string space
that is less than the original memory block that is okay. If you were
to copy a larger string over this definition of p, you would overwrite
some part of memory that you do not know what it is. That can be
dangerous.

This is blatantly wrong. Any attempt to modify a string literal results
in undefined behavior. It doesn't matter at all whether the new string
is shorter or longer than the old. Undefined is undefined.

-Kevin
 
K

Kevin Goodsell

Yang said:
Hi,

If I write

char *p="hello world";

is this correct in C or do i have to assign memory block and then
strcpy the string to pointer.

If not correct in C , is it allowable in C++ , i seen such syntax in
some books of c++;

It's technically allowed, but almost always a mistake. In C++ it is
deprecated.

String literals may not be modified, therefore pointers to string
literals should be const. This allows the compiler to warn you if you
attempt to modify a string literal. Otherwise the likely result will be
a crash at runtime.

So while this is allowed:

char *p = "some string";

You should never use it. Use one of these instead:

const char *p = "some string";
char const *p = "some string";
const char * const p = "some string";
char const * const p = "some string";

-Kevin
 
R

Robert Stankowic

David said:
Lee,



This is correct in C and C++.

The quoted text ("hello world") will be stored in the progams memory
space. The pointer p will point to this string in memory.

The size of the ("hello world") memory block is likely to be 12 bytes.
If you just want this to be used as a constant or for a string space
that is less than the original memory block that is okay. If you were
to copy a larger string over this definition of p, you would overwrite
some part of memory that you do not know what it is. That can be
dangerous.

If he tries to modify _any_ part of this string literal, _very_ surprising
things may happen :)
strcpy(p, "my world");
will segfault if he is lucky. With some bad luck it will format his
harddisk, destroy his monitor (both can _really_ happen, believe me).
On a DS9000 an iron hand may come out of the CD drive and hit his nose :)
(which is one of the less nasty reactions of a DS9000 if it encounters UB).

It is perfectly correct in C, but it is _not_ allowed to modify the string
literal.
Robert
 
M

Martin Dickopp

Robert Stankowic said:
If he tries to modify _any_ part of this string literal, _very_ surprising
things may happen :)
strcpy(p, "my world");
will segfault if he is lucky. With some bad luck it will format his
harddisk, destroy his monitor (both can _really_ happen, believe me).

Very true. A `void main (int argc, char **argv)' (in a program not written
by me) almost destroyed my monitor once. I could only save it by pulling
the power plug.

Martin
 
R

Robert Stankowic

Martin Dickopp said:
Very true. A `void main (int argc, char **argv)' (in a program not written
by me) almost destroyed my monitor once. I could only save it by pulling
the power plug.

Yep. And a "wild" pointer (under DOS, with Watcom C) once really caused the
program to crash into the low level format routine and happily formatted my
HD.
It could, btw, as well play Tchernobyl and destroy certain brands of
mainboards with a writeable, but soldered BIOS chip.
Such is (C) life :)
 
K

Keith Thompson

Martin Dickopp said:
Very true. A `void main (int argc, char **argv)' (in a program not written
by me) almost destroyed my monitor once. I could only save it by pulling
the power plug.

Seriously? If you can provide details, your story could be excellent
ammunition in arguments with who equate "it works for me" with "it's
correct".
 
M

Malcolm

Yang Lee said:
If I write

char *p="hello world";

is this correct in C or do i have to assign memory block and then
strcpy the string to pointer.
It is correct, but normally you would just pass the string literal to the
function that needs it.

eg printf("Hello world");

rather than
char *p = "Hello world";
printf(p);

if you need modify the string, you do need to copy to an array

eg

char str[256];

strcpy(str, "Hello world");
strcat(str, " from Yang Lee");
If not correct in C , is it allowable in C++ , i seen such syntax in
some books of c++;
It's rather old-fashioned C++. Modern C++ would use the string class to
store string literals.
 
P

Peteris Krumins

const char *p = "some string";
char const *p = "some string";
const char * const p = "some string";
char const * const p = "some string";

What are the differences, if any, between these definitions?


P.Krumins
 
M

Mark McIntyre

On 16 Dec 2003 23:35:20 GMT, in comp.lang.c , Peteris Krumins

read from right to left.

p is a pointer to char which is const...
ie pointer to const char

const pointer to char

const pointer to const char

syntax error

In all cases, the data pointed to is a string literal, which is by
definition nonmodifiable anyway.
 
A

Alex

Mark McIntyre said:
On 16 Dec 2003 23:35:20 GMT, in comp.lang.c , Peteris Krumins
<[email protected]> wrote:
read from right to left.

Are you sure that that's what you're doing?
p is a pointer to char which is const...
ie pointer to const char
const pointer to char

Nope, it is equivalent to the first example. Const pointer
to char would be:

char * const p;
const pointer to const char
syntax error

Nope. Again, as above, const pointer to const char.

Alex
 
K

Keith Thompson

I wrote:
[...]
Seriously? If you can provide details, your story could be excellent
ammunition in arguments with who equate "it works for me" with "it's
correct".

That should be "... arguments with *people* who equate ...".
 
K

Kevin Goodsell

Peteris said:
What are the differences, if any, between these definitions?

Unless I've made a mistake, the first two are equivalent to each other,
and the last two are equivalent to each other.

The first two both mean that p is a pointer to type const char. In other
words, the pointer may be modified (made to point elsewhere), but you
may not use that pointer for the purpose of modifying the object to
which it points.

The last two both mean that p is a const pointer to type const char. The
pointer cannot be made to point elsewhere, nor can the thing to which it
points be modified via the pointer.

So, the only difference is whether or not you can reassign p to point
somewhere else.

-Kevin
 
R

Robert Stankowic

Keith Thompson said:
Seriously? If you can provide details, your story could be excellent
ammunition in arguments with who equate "it works for me" with "it's
correct".
<OT>
Why not? void main() may cause a (non protected) implementation to mess up
the stack upon return from main(), run wild and set the screen resolution to
something the monitor cannot handle. On some old monitors the result is
disastrous.

"Taking over in poor visibility worked always for me" said the guy when he
woke up in the hospital....
</OT>
 
G

glen herrmannsfeldt

Kevin Goodsell wrote:

(snip)
This is blatantly wrong. Any attempt to modify a string literal results
in undefined behavior. It doesn't matter at all whether the new string
is shorter or longer than the old. Undefined is undefined.

For compatibility with K&R C it has to be allowed. I know some
compilers have it as an option. Others may not have the
compatibility mode anymore.

-- glen
 
C

Christopher Benson-Manica

glen herrmannsfeldt said:
For compatibility with K&R C it has to be allowed. I know some
compilers have it as an option. Others may not have the
compatibility mode anymore.

K&R2, p194:
"...the behavior of a program that attempts to alter a string
literal is undefined."

I assume by K&R C you're speaking of pre-ANSI...?
 
M

Martin Dickopp

Keith Thompson said:
Seriously? If you can provide details, your story could be excellent
ammunition in arguments with who equate "it works for me" with "it's
correct".

I was configuring suspend-to-disk on my GNU/Linux system, and it turned
out that the graphics state was not properly restored, resulting in a
messed up screen. Luckily, the problem could be avoided by switching to
text mode before suspending, and switching back to graphics mode after
resuming.

I didn't want to do this manually, so I modified the scripts which are run
before suspending and after resuming to switch to a text console and back.
Unfortunately, the `main' function of the program to switch virtual
terminals returned `void'. The undefined behaviour in this case had the
effect that the program incorrectly reported unsuccessful execution after
switching virtual terminals. This in turn caused the script to terminate
immediately and report unsuccessful execution to its own parent process,
the power management daemon.

An unrelated BIOS bug (maybe it was supposed to be a feature...) caused
the BIOS to retry immediately if suspend-to-disk failed. The effect was
that the system switched forth and back between graphics and text mode in
fast succession. Monitors don't like that... :)

Martin
 

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
474,122
Messages
2,570,716
Members
47,282
Latest member
hopkins1988

Latest Threads

Top