char * against char[] 
Author Message
 char * against char[]


Quote:
>If I use
>char *p = "sddsfsdf";
>p[5]='s';

>a segmentualtion fault will occur

>but char p[]="SDfsfdsd"
>p[5]='s';
>no error is occur..why?
>for me,I just think char p[] is just a constant pointer  with known
>number of size,while char *p is not a constant pointer..

>and the expression "Sfsdfsdffsd" will allocate enough space for the
>whole element and return the adddress of the first element......

You have not read the FAQ have you ?

--
-hs-
CLC-FAQ: http://www.*-*-*.com/ ~scs/C-faq/top.html
ISO-C Library: http://www.*-*-*.com/
"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC



Sat, 24 Aug 2002 03:00:00 GMT  
 char * against char[]

Quote:
> >If I use
> >char *p = "sddsfsdf";
> >p[5]='s';

> >a segmentualtion fault will occur

> >but char p[]="SDfsfdsd"
> >p[5]='s';
> >no error is occur..why?
> >for me,I just think char p[] is just a constant pointer  with known
> >number of size,while char *p is not a constant pointer..

> >and the expression "Sfsdfsdffsd" will allocate enough space for the
> >whole element and return the adddress of the first element......

http://www.eskimo.com/~scs/C-faq/q1.32.html

Quote:
> --
> -hs-
> CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
> ISO-C Library: http://www.dinkum.com/htm_cl
> "It's specified. But anyone who writes code like that should be
> transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC

Sent via Deja.com http://www.deja.com/
Before you buy.


Sat, 24 Aug 2002 03:00:00 GMT  
 char * against char[]

Quote:

> If I use
> char *p = "sddsfsdf";
> p[5]='s';

> a segmentualtion fault will occur

> but char p[]="SDfsfdsd"
> p[5]='s';
> no error is occur..why?
> for me,I just think char p[] is just a constant pointer  with known
> number of size,while char *p is not a constant pointer..

> and the expression "Sfsdfsdffsd" will allocate enough space for the
> whole element and return the adddress of the first element......

See the C language FAQ, question 16.6 at

http://www.eskimo.com/~scs/C-faq/q16.6.html\

--
Rex Barzee



Sat, 24 Aug 2002 03:00:00 GMT  
 char * against char[]

Quote:

>If I use
>char *p = "sddsfsdf";
>p[5]='s';

>a segmentualtion fault will occur

>but char p[]="SDfsfdsd"
>p[5]='s';
>no error is occur..why?
>for me,I just think char p[] is just a constant pointer with known
>number of size,

No!  char p[] is *not* a "constant pointer" - it's an array.  The two
are very different.

Quote:
>while char *p is not a constant pointer..

char *p is a pointer.  It points to a string literal.  Modifying a string
literal is not permitted in C.

HTH
John
--
John Winters.  Wallingford, Oxon, England.

The Linux Emporium - the source for Linux CDs in the UK
See http://www.linuxemporium.co.uk/



Sat, 24 Aug 2002 03:00:00 GMT  
 char * against char[]

Quote:

> char *p is a pointer.  It points to a string literal.
> Modifying a string literal is not permitted in C.
> John Winters.  Wallingford, Oxon, England.

So you're saying that construction like this:

void fun (void)
{
 char *p = "0123456789";

 p[1] = 'x';
 printf (p);

Quote:
}

is not legal ??
Have C definition changed in the last 5 years (I'm not up to date) ??

Gregpol

==================================
Posted via http://nodevice.com
Linux Programmer's Site



Sat, 24 Aug 2002 03:00:00 GMT  
 char * against char[]


Quote:
> If I use
> char *p = "sddsfsdf";
> p[5]='s';

> a segmentualtion fault will occur

Correct.

    char *p = "sddsfsdf";

gives you a POINTER to a CONSTANT string "sddsfsdf".  You are not allowed to
modify the string.  You can modify p if you like, to point to something
else.

Quote:
> but char p[]="SDfsfdsd" <-- note you're missing a semicolon here; I assume
it's a typo
> p[5]='s';

Well,

    char p[] = "SDfsfdsd";

gives you an ARRAY of nine characters (the eight contained between the
quotes plus a '\0' at the end to mark the end of the string).  You are free
to modify this array.

As you see, a pointer and an array are NOT the same thing.  They are
initialized differently, among other things.

comp.lang.c FAQ can be found at http://www.eskimo.com/~scs/C-faq/top.html



Sat, 24 Aug 2002 03:00:00 GMT  
 char * against char[]
If I use
char *p = "sddsfsdf";
p[5]='s';

a segmentualtion fault will occur

but char p[]="SDfsfdsd"
p[5]='s';
no error is occur..why?
for me,I just think char p[] is just a constant pointer  with known
number of size,while char *p is not a constant pointer..

and the expression "Sfsdfsdffsd" will allocate enough space for the
whole element and return the adddress of the first element......



Sun, 25 Aug 2002 03:00:00 GMT  
 char * against char[]
 > > char *p is a pointer.  It points to a string literal.
 > > Modifying a string literal is not permitted in C.
...
 > So you're saying that construction like this:
 >  char *p = "0123456789";
 >  p[1] = 'x';
...
 > is not legal ??

Yup, he is saying that.

 > Have C definition changed in the last 5 years (I'm not up to date) ??

Not in this aspect.
--
dik t. winter, cwi, kruislaan 413, 1098 sj  amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn  amsterdam, nederland; http://www.cwi.nl/~dik/



Sun, 25 Aug 2002 03:00:00 GMT  
 char * against char[]

Quote:

>  > > Modifying a string literal is not permitted in C.
> ....
>  > So you're saying that construction like this:
>  >  char *p = "0123456789";
>  >  p[1] = 'x';
> ....
>  > is not legal ??
> Yup, he is saying that.
>  > Have C definition changed in the last 5 years (I'm
> not up to date) ??
> Not in this aspect.

I'm not sure, but I think it has similar status to an
expression like *NULL = x;

ie the construct is legal but the results are undefined.

* Sent from AltaVista http://www.altavista.com Where you can also find related Web Pages, Images, Audios, Videos, News, and Shopping.  Smart is Beautiful



Sun, 25 Aug 2002 03:00:00 GMT  
 char * against char[]

...
 > >  > > Modifying a string literal is not permitted in C.
...
 > I'm not sure, but I think it has similar status to an
 > expression like *NULL = x;
 >
 > ie the construct is legal but the results are undefined.

No.  The behaviour is undefined.  Which is C's way to say that something
is not permitted in a conforming program.  Undefined results can occur
in valid C programs as long as you do not use them.  (E.g.:
     int f(void) { return; }
is valid in C89, but you can not now use 'i = f();'.)
--
dik t. winter, cwi, kruislaan 413, 1098 sj  amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn  amsterdam, nederland; http://www.cwi.nl/~dik/



Sun, 25 Aug 2002 03:00:00 GMT  
 char * against char[]



Quote:


> ...
>  > >  > > Modifying a string literal is not permitted in C.
> ...
>  > I'm not sure, but I think it has similar status to an
>  > expression like *NULL = x;

>  > ie the construct is legal but the results are undefined.

> No.  The behaviour is undefined.  Which is C's way to say that something
> is not permitted in a conforming program.

WRONG.  The C-STANDARD cannot say anything by itself.  Only SUBJECTS can.
The STANDARD is not a thing-for-itself in your characterization.  Karl M,
chair.


Sun, 25 Aug 2002 03:00:00 GMT  
 char * against char[]


Quote:

>> char *p is a pointer.  It points to a string literal.
>> Modifying a string literal is not permitted in C.
>> John Winters.  Wallingford, Oxon, England.

>So you're saying that construction like this:

>void fun (void)
>{
> char *p = "0123456789";

> p[1] = 'x';
> printf (p);
>}

>is not legal ??
>Have C definition changed in the last 5 years (I'm not up to date) ??

This became unambiguously invalid C in 1989 when the first C standard was
published.

--
-----------------------------------------


-----------------------------------------



Sun, 25 Aug 2002 03:00:00 GMT  
 char * against char[]

Quote:

> WRONG.  The C-STANDARD cannot say anything by itself.  Only SUBJECTS can.
> The STANDARD is not a thing-for-itself in your characterization.  Karl M,
> chair.

   Great, now he thinks he's furniture.




Sun, 25 Aug 2002 03:00:00 GMT  
 char * against char[]

Quote:


>> char *p is a pointer.  It points to a string literal.
>> Modifying a string literal is not permitted in C.
>> John Winters.  Wallingford, Oxon, England.

>So you're saying that construction like this:

>void fun (void)
>{
> char *p = "0123456789";

> p[1] = 'x';
> printf (p);
>}

>is not legal ??

Correct.

Quote:
>Have C definition changed in the last 5 years (I'm not up to date) ??

No, it was illegal 5 years ago too.

John
--
John Winters.  Wallingford, Oxon, England.

The Linux Emporium - the source for Linux CDs in the UK
See http://www.linuxemporium.co.uk/



Sun, 25 Aug 2002 03:00:00 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Converting char array of literals chars to escape chars

2. char *c,char c[ ] and char *c[ ]

3. char, unsigned char, signed char

4. A char pointer (char *) vs. char array question

5. Convert char* to char __gc[]

6. problem assigning char* to char*

7. char** and const char** ...

8. char* -> char (newbie question)

9. char *c vs. char c[]

10. what is the difference between this types: char and System.Char

11. Char to Wide-Char tool

12. NewBie: difference between char* and unsigned char*

 

 
Powered by phpBB® Forum Software