Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: A problem a...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 7 Topic 43781 of 48417
Post > Topic >>

Re: A problem about char pointer ?

by "comp.lang.c++" <cnhenuake@[EMAIL PROTECTED] > Mar 10, 2008 at 06:27 PM

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!
 




 7 Posts in Topic:
A problem about char pointer ?
"comp.lang.c++"  2008-03-10 08:00:40 
Re: A problem about char pointer ?
"Victor Bazarov"  2008-03-10 11:14:34 
Re: A problem about char pointer ?
"Jim Langston"   2008-03-10 14:06:10 
Re: A problem about char pointer ?
Paul Brettschneider <p  2008-03-10 23:57:41 
Re: A problem about char pointer ?
"comp.lang.c++"  2008-03-10 18:27:25 
Re: A problem about char pointer ?
"Jim Langston"   2008-03-12 18:06:51 
Re: A problem about char pointer ?
Triple-DES <DenPlettfr  2008-03-10 22:54:51 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Nov 21 9:55:17 CST 2008.