On Mar 11, 5:06 am, "Jim Langston" <tazmas...@[EMAIL PROTECTED]
> wrote:
> Victor Bazarov wrote:
> > comp.lang.c++ wrote:
> >> this is a sample example about this question
> >> #include<stdio.h>
> >> void chg(char* t)
> >> {
> >> char *s=t;
> >> char p=*t;
> >> while(*t++=*++s);
> >> *--t=p;
> >> }
> >> int main()
> >> {
> >> //char * t="abcde";
> >> char t[]="abcde";
> >> chg(t);
> >> printf(t);
> >> printf("\n");
> >> return 0;
> >> }
> >> I want know why the function chg() cann't work when define t with
> >> char*, and what's the different
> >> between char* and char[]?
>
> > The difference is relatively obscure, unfortunately. When you declare
> > 't' to be an array (char t[]), then each element of 't' is allowed to
> > be modified, IOW it's OK to modify it. When you declare 't' as a mere
> > pointer, and initialise it with a literal, then 't' points to the
> > non-modifiable memory, and any attempt to change the value of elements
> > of 't' has _undefined_behaviour_.
>
> >> thanks?
>
> > Yes, please.
>
> Just to reiterate, the problem isn't, per se, that t is defined as a
char*,
> but where t is pointing to.
>
> For example,
>
> char x[] = "abcde";
> char* t = x;
> chg(t);
>
> with your code would work. Because t is now not pointing to constant
> memory.
>
> --
> Jim Langston
> tazmas...@[EMAIL PROTECTED]
,I think I konw .Because "abcde" is a constant ,t just point to
it.and so t should be a constant pointer in fact.and char t[]="abcde"
is declare a array and give it's elements vuales by "abcde" , and t
is point to the first element's address.and I want to know how to
judge if the pointer is point to a constant in my function ? thanks!


|