A problem about char pointer ?

C

comp.lang.c++

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[]?
thanks?
 
V

Victor Bazarov

comp.lang.c++ said:
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_.

Yes, please.

V
 
J

Jim Langston

Victor said:
comp.lang.c++ said:
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_.

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.
 
P

Paul Brettschneider

Victor said:
comp.lang.c++ said:
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_.

Reminds me of my first paid coding job, which consisted of making a large
code base containing myriads of goodies like
char *s;
[...]
return strncpy(" ", s, 5);
and lots of global variables reentrant, because it was to be used in a
shared library.

I don't miss those times. ;)
 
C

comp.lang.c++

Victor said:
comp.lang.c++ said:
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_.
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.
thanks ,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!
 
T

Triple-DES

thanks ,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!

Since your function takes a pointer to char, it should not be pointing
to a constant. Just make sure to use const char pointers when dealing
with literals. Or preferably, std::string

DP
 
J

Jim Langston

comp.lang.c++ said:
Victor said:
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_.

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.
thanks ,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!

Use constant correctness in your code. If anything is constant, declare it
as such. The following will not compile with your code:

int main()
{
const char* t="abcde";
chg(t);
}

The error that MSVC++ .net 2003 gives me is:
error C2664: 'chg' : cannot convert parameter 1 from 'const char *' to 'char
*'

The reason you are allowed to have a char pointer point to non constant
memory in the first place is because it was allowed in C. For backwards
compatability. As such the programmer has to be a bit more careful with
what they are doing.

Use the const keyword wherever you can.
 

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

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,102
Latest member
GregoryGri
Top