473,466 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

A problem about char pointer ?

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?
Mar 10 '08 #1
6 1726
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.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 10 '08 #2
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
ta*******@rocketmail.com
Mar 10 '08 #3
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_.
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. ;)
Mar 10 '08 #4
On Mar 11, 5:06 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
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...@rocketmail.com
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!
Mar 11 '08 #5
On 11 Mar, 02:27, "comp.lang.c++" <cnhenu...@gmail.comwrote:
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
Mar 11 '08 #6
comp.lang.c++ wrote:
On Mar 11, 5:06 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>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...@rocketmail.com
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.
--
Jim Langston
ta*******@rocketmail.com
Mar 14 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: drowned | last post by:
I'm having a problem understanding why the code in this little program I've got is behaving the way it does... if anyone can explain it, I'd be grateful: #include <iostream> using namespace...
11
by: Dmitry D | last post by:
Hi, I'm new to C++ (started learning in the beginning of this summer), and I have the following question (sorry if it sounds stupid): In many code samples and source files, I see NULL expression...
36
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
13
by: chellappa | last post by:
hi , please explain me , char pointer , char Array, char ,string... i have some many confussion about this... please clarify to me.. i need some example about this program by chellappa.ns
7
by: Yuri_Юрий | last post by:
I'm confused about the VARIABLE LENGTH ARRAYS. {scanf("%d",&n);float a;} In which compiler can I use it? I tried VC++6.0 SP6,but it's reported error:CONSTANT EXPRESSION! Another question, What...
12
by: Aff | last post by:
Brothers , i am facing a problem which is as follow: class poly { private: char name; int capacity; public: poly(char , int );
14
by: nospam | last post by:
From the book "There is an important difference between these definitions: char amessage="now is the time"; char *pmessage ="now is the time"; snip On the other hand, pmessage is a...
160
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.