473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

What is wrong with this: *p++=*s++

Hi,
I have a probelm here:

If I declare:
char *p="Hello";

char *s="world"

and I say

*p++=*s++;

I get a segmentation fault. But when I use arrays( say p[80], s[80])
instead of pointers *p and *s every thing is fine.
Can someone tell me what is wrong here?
Regards,
Matt
Nov 14 '05 #1
18 2467
ma********@hotmail.com (Matt) wrote in
news:f6************************@posting.google.com :
I have a probelm here:

If I declare:
char *p="Hello";

char *s="world"

and I say

*p++=*s++;

I get a segmentation fault. But when I use arrays( say p[80], s[80])
instead of pointers *p and *s every thing is fine.


Of course. p and s point to non-modifiable memory. On embedded systems
these *constant* strings might be in FLASH memory. Clearly you don't
expect to be able to modify FLASH memory with a simple write? No, nor may
you attempt to modify string literals on any system. You should learn to
define p and s correctly as:

const char *p = "Hello";
const char *s = "World";

Then you will get the correct compile time error instead of a run-time
error.

--
- Mark ->
--
Nov 14 '05 #2
Matt wrote:
Hi,
I have a probelm here:

If I declare:
char *p="Hello";

char *s="world"

and I say

*p++=*s++;

I get a segmentation fault. But when I use arrays( say p[80], s[80])
instead of pointers *p and *s every thing is fine.
Can someone tell me what is wrong here?


This is Question 16.6 in the comp.lang.c Frequently
Asked Questions (FAQ) list

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

--
Er*********@sun.com

Nov 14 '05 #3
"Eric Sosman" <Er*********@sun.com> wrote in message news:40**************@sun.com...
Matt wrote:
Hi,

I have a probelm here:

If I declare:

char *p="Hello";

char *s="world"

and I say

*p++=*s++;

I get a segmentation fault. But when I use arrays( say p[80], s[80])
instead of pointers *p and *s every thing is fine.
Can someone tell me what is wrong here?
This is Question 16.6 in the comp.lang.c


As that 16.6 states, it's covered even earlier in 1.32.
Frequently
Asked Questions (FAQ) list

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


--
Peter
Nov 14 '05 #4
Thanks guy for the help.
One more question:
Do we have any function in C to delete a substring in a string. I
know C++ does but what about C?
Matt
"Peter Nilsson" <ai***@acay.com.au> wrote in message news:<40******@news.rivernet.com.au>...
"Eric Sosman" <Er*********@sun.com> wrote in message news:40**************@sun.com...
Matt wrote:
Hi,

I have a probelm here:

If I declare:

char *p="Hello";

char *s="world"

and I say

*p++=*s++;

I get a segmentation fault. But when I use arrays( say p[80], s[80])
instead of pointers *p and *s every thing is fine.
Can someone tell me what is wrong here?


This is Question 16.6 in the comp.lang.c


As that 16.6 states, it's covered even earlier in 1.32.
Frequently
Asked Questions (FAQ) list

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

Nov 14 '05 #5

On Tue, 20 Jul 2004, Matt wrote:

One more question:
Do we have any function in C to delete a substring in a string. I
know C++ does but what about C?


Please remember to snip any quotes not relevant to your post
(which in this case was everything, which means you ought to have
started a new thread with a nice descriptive subject line).

No. You can, however, use 'memmove' (NOT 'memcpy' or 'strcpy',
though), like this:

char buffer[1000] = "foobarbaz";
char remove[] = "bar";

char *find_it = strstr(buffer, remove);
if (find_it != NULL) {
/*
The substring exists in the buffer.
Erase it by copying the rest of the string
over top of it. Note that we are copying
the length of the remaining string /plus one/,
so that we copy the terminating null character.

Note also that this comment is needlessly verbose,
for pedagogical reasons. :)
*/
memmove(find_it,
find_it+strlen(remove),
strlen(find_it)-strlen(remove)+1
);
}
puts(buffer);

HTH,
-Arthur
Nov 14 '05 #6
Thanks Arthur,

Wow, It was a very smart code. I learned a lot from you. I really appreciate.
Matt
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message news:<Pi**********************************@unix45. andrew.cmu.edu>...
On Tue, 20 Jul 2004, Matt wrote:

One more question:
Do we have any function in C to delete a substring in a string. I
know C++ does but what about C?


Please remember to snip any quotes not relevant to your post
(which in this case was everything, which means you ought to have
started a new thread with a nice descriptive subject line).

No. You can, however, use 'memmove' (NOT 'memcpy' or 'strcpy',
though), like this:

char buffer[1000] = "foobarbaz";
char remove[] = "bar";

char *find_it = strstr(buffer, remove);
if (find_it != NULL) {
/*
The substring exists in the buffer.
Erase it by copying the rest of the string
over top of it. Note that we are copying
the length of the remaining string /plus one/,
so that we copy the terminating null character.

Note also that this comment is needlessly verbose,
for pedagogical reasons. :)
*/
memmove(find_it,
find_it+strlen(remove),
strlen(find_it)-strlen(remove)+1
);
}
puts(buffer);

HTH,
-Arthur

Nov 14 '05 #7
In <f6*************************@posting.google.com> ma********@hotmail.com (Matt) writes:
One more question:
Do we have any function in C to delete a substring in a string.


No, because it is not needed often enough and anyone with half a clue
can put strstr and memmove together to get the job done. Just be
careful when computing the number of bytes to be moved.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8
ma********@hotmail.com (Matt) wrote in message news:<f6************************@posting.google.co m>...
Hi,
I have a probelm here:

If I declare:
char *p="Hello";

char *s="world"

and I say

*p++=*s++;

I get a segmentation fault. But when I use arrays( say p[80], s[80])
instead of pointers *p and *s every thing is fine.
Can someone tell me what is wrong here?
Regards,
Matt


hugo27 July 24, 2004
What is Matt's intent? To concat the two strings? Or to alter *p?
And what does *p++=*s++; actually do?

I know from my own investigations that a construct such as *p++
first increments the address in p (by 1, because char type).
This makes *p point to the 'e' in "Hello". Then it fetches the
the object, 'e', or makes it available. The 'H' is still there
in the string. It's as if *p++ moves the "start maker" of the
string.

Likewise, *s++ makes *s point at the 'o' in "world", and
fetches the code for 'o'.
Then the = executes. What happens? The program tries to insert
the code for 'o' on top of the code for 'e'. The 'e' is not a
variable, so a memory (segment) error results.

hugo.
Nov 14 '05 #9
On 24 Jul 2004 15:34:25 -0700, ob****@yahoo.com (hugo27) wrote:
ma********@hotmail.com (Matt) wrote in message news:<f6************************@posting.google.co m>...
Hi,
I have a probelm here:

If I declare:
char *p="Hello";

char *s="world"

and I say

*p++=*s++;

I get a segmentation fault. But when I use arrays( say p[80], s[80])
instead of pointers *p and *s every thing is fine.
Can someone tell me what is wrong here?
Regards,
Matt
hugo27 July 24, 2004
What is Matt's intent? To concat the two strings? Or to alter *p?
And what does *p++=*s++; actually do?

I know from my own investigations that a construct such as *p++
first increments the address in p (by 1, because char type).


Then you need to investigate further. While it is true that the ++
post increment operator will increment p, you have no idea when this
side-effect will actually occur other than sometime before the next
sequence point. Therefore, it is incorrect to assume it will occur
first.
This makes *p point to the 'e' in "Hello". Then it fetches the
While it is true that after p is incremented it points to the 'e',
this has absolutely no affect on the value returned by the expression
since the post increment operator expression will always evaluate to
the pre-incremented value of the operand. Therefore, the first time
the statement is executed would result in the 'H' being replaced
(except for the fact that the 'H' is part of a string literal and any
attempt to modify it results in undefined behavior).
the object, 'e', or makes it available. The 'H' is still there
Nope.
in the string. It's as if *p++ moves the "start maker" of the
string.
It is as if *p++ causes the character currently pointed to by p to be
replaced and then increments p.

Likewise, *s++ makes *s point at the 'o' in "world", and
fetches the code for 'o'.
Wrong for exactly the same reason.
Then the = executes. What happens? The program tries to insert
the code for 'o' on top of the code for 'e'. The 'e' is not a
variable, so a memory (segment) error results.


'e' is never a variable. As far as undefined behavior goes, a memory
is actually one of the more fortuitous manifestations.
<<Remove the del for email>>
Nov 14 '05 #10
hugo27 wrote:
ma********@hotmail.com (Matt) wrote in message
news:<f6************************@posting.google.co m>...
Hi,
I have a probelm here:

If I declare:
char *p="Hello";

char *s="world"

and I say

*p++=*s++;

I get a segmentation fault. But when I use arrays( say p[80], s[80])
instead of pointers *p and *s every thing is fine.
Can someone tell me what is wrong here?
Regards,
Matt
hugo27 July 24, 2004
What is Matt's intent? To concat the two strings? Or to alter *p?
And what does *p++=*s++; actually do?

I know from my own investigations that a construct such as *p++
first increments the address in p (by 1, because char type).


No, it doesn't. The effect ist identical to

*p = *s; p = p + 1; s = s + 1;

or

*p = *s; s = s + 1; p = p + 1;

This makes *p point to the 'e' in "Hello". Then it fetches the
the object, 'e', or makes it available. The 'H' is still there
in the string. It's as if *p++ moves the "start maker" of the
string.

Likewise, *s++ makes *s point at the 'o' in "world", and
fetches the code for 'o'.
Then the = executes. What happens? The program tries to insert
the code for 'o' on top of the code for 'e'. The 'e' is not a
variable, so a memory (segment) error results.


You're right in principle, but wrong in the details. "*p++ = *s++"
tries to move the 'w' to the place where the 'H' is stored,
not the 'o' to the place where the 'e' is stored. Since both
the place where the 'H' is stored and the place where the 'e'
is stored need not be writable memory locations, an error
may occur.

Kurt Watzka
Nov 14 '05 #11
Hi Matt,
If you want to add mystery to your puzzle try this:
char p [80];
char s [80];
p = "Hello";
s = "world";
Now it won't even compile. ;-) Let me know if this hint helps you figure out
what the problem is with your code.
Sincerely,
Hamed

"Matt" <ma********@hotmail.com> wrote in message
news:f6************************@posting.google.com ...
Hi,
I have a probelm here:

If I declare:
char *p="Hello";

char *s="world"

and I say

*p++=*s++;

I get a segmentation fault. But when I use arrays( say p[80], s[80])
instead of pointers *p and *s every thing is fine.
Can someone tell me what is wrong here?
Regards,
Matt

Nov 14 '05 #12
How you can do this? As far as I know, you are assigning a string to a
character. It is not compatible.

Matt
"Hamed" <ha***********@earthlink.net> wrote in message news:<kG*******************@newsread2.news.pas.ear thlink.net>...
Hi Matt,
If you want to add mystery to your puzzle try this:
char p [80];
char s [80];
p = "Hello";
s = "world";
Now it won't even compile. ;-) Let me know if this hint helps you figure out
what the problem is with your code.
Sincerely,
Hamed

"Matt" <ma********@hotmail.com> wrote in message
news:f6************************@posting.google.com ...
Hi,
I have a probelm here:

If I declare:
char *p="Hello";

char *s="world"

and I say

*p++=*s++;

I get a segmentation fault. But when I use arrays( say p[80], s[80])
instead of pointers *p and *s every thing is fine.
Can someone tell me what is wrong here?
Regards,
Matt

Nov 14 '05 #13
On 25 Jul 2004 15:48:38 -0700, ma********@hotmail.com (Matt) wrote:
How you can do this? As far as I know, you are assigning a string to a
character. It is not compatible.
p is not a char. It is an array of char. And a definition of the
form
char p[80] = "Hello";
will work quite nicely.

Matt
"Hamed" <ha***********@earthlink.net> wrote in message news:<kG*******************@newsread2.news.pas.ear thlink.net>...
Hi Matt,
If you want to add mystery to your puzzle try this:
char p [80];
char s [80];
p = "Hello";
s = "world";
Now it won't even compile. ;-) Let me know if this hint helps you figure out
what the problem is with your code.
Sincerely,
Hamed

"Matt" <ma********@hotmail.com> wrote in message
news:f6************************@posting.google.com ...
> Hi,
>
>
> I have a probelm here:
>
> If I declare:
>
>
> char *p="Hello";
>
> char *s="world"
>
> and I say
>
> *p++=*s++;
>
> I get a segmentation fault. But when I use arrays( say p[80], s[80])
> instead of pointers *p and *s every thing is fine.
> Can someone tell me what is wrong here?
>
>
> Regards,
> Matt


<<Remove the del for email>>
Nov 14 '05 #14
The difference between the declarations "char *str_pointer" and "char
str_array[80]" is that str_pointer points to a random address unless
you allocate memory before assign it a value and str_array has already
allocate memory when you declare it.
Therefore, if you want to use str_pointer, you need to allocate memory
for it by using functions like "malloc". (and of cource, free the
memroy after you use it.)


ma********@hotmail.com (Matt) wrote in message news:<f6************************@posting.google.co m>...
How you can do this? As far as I know, you are assigning a string to a
character. It is not compatible.

Matt
"Hamed" <ha***********@earthlink.net> wrote in message news:<kG*******************@newsread2.news.pas.ear thlink.net>...
Hi Matt,
If you want to add mystery to your puzzle try this:
char p [80];
char s [80];
p = "Hello";
s = "world";
Now it won't even compile. ;-) Let me know if this hint helps you figure out
what the problem is with your code.
Sincerely,
Hamed

"Matt" <ma********@hotmail.com> wrote in message
news:f6************************@posting.google.com ...
Hi,
I have a probelm here:

If I declare:
char *p="Hello";

char *s="world"

and I say

*p++=*s++;

I get a segmentation fault. But when I use arrays( say p[80], s[80])
instead of pointers *p and *s every thing is fine.
Can someone tell me what is wrong here?
Regards,
Matt

Nov 14 '05 #15
Hamed <ha***********@earthlink.net> wrote:
Hi Matt,
If you want to add mystery to your puzzle try this:
char p [80];
char s [80];
p = "Hello";
s = "world";
Now it won't even compile. ;-) Let me know if this hint helps you figure out
what the problem is with your code.


That's no puzzle at all.

char p[ 80 ];

gives you an array of 80 chars. And you can't change 'p' afterwards
anymore. But that's what you're trying to do when you write

p = "Hello";

because you're treating 'p' as if it where an char pointer by trying
to assign it the address of where the (6 char long) string "Hello" is
stored (possibly in read-only memory). But 'p' isn't a char pointer,
it is an array of chars. And since these are different types the
compiler won't accept that broken code.

You might be a bit confused because when you pass 'p' to a function
what the function actually gets is a pointer to the first element of
the array 'p' (instead of a copy of the array). But that is something
special about _function calls_ with array arguments and does not
change the fact that 'p' is an array which can't be converted to a
pointer here.

And the problem of the Matt has been already explained to him, strings
like the one assigned here to 's'

char *s = "Hello";

are strings that simply can't be changed but that's what he was
trying to do, violating the rules of the game (unfortunately,
there are some implementations that let you get away with this,
making learning this more difficult).
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #16
Hi Jens,
You didn't read my comments in its entirety. All I was trying to explain to
him was that when he does
char p[80] = "Hello"; He copies the chars to the array's allocated memory.
When he does char * p ="Hello"; he points to a read-only memory without
copying anything. By giving him the example, I was trying to tell him that
arrays are not pointers. Arrays and pointers are like orange and apple that
share some characteristics.
Sincerely,
Hamed

<Je***********@physik.fu-berlin.de> wrote in message
news:2m************@uni-berlin.de...
Hamed <ha***********@earthlink.net> wrote:
Hi Matt,
If you want to add mystery to your puzzle try this:
char p [80];
char s [80];
p = "Hello";
s = "world";
Now it won't even compile. ;-) Let me know if this hint helps you figure out what the problem is with your code.


That's no puzzle at all.

char p[ 80 ];

gives you an array of 80 chars. And you can't change 'p' afterwards
anymore. But that's what you're trying to do when you write

p = "Hello";

because you're treating 'p' as if it where an char pointer by trying
to assign it the address of where the (6 char long) string "Hello" is
stored (possibly in read-only memory). But 'p' isn't a char pointer,
it is an array of chars. And since these are different types the
compiler won't accept that broken code.

You might be a bit confused because when you pass 'p' to a function
what the function actually gets is a pointer to the first element of
the array 'p' (instead of a copy of the array). But that is something
special about _function calls_ with array arguments and does not
change the fact that 'p' is an array which can't be converted to a
pointer here.

And the problem of the Matt has been already explained to him, strings
like the one assigned here to 's'

char *s = "Hello";

are strings that simply can't be changed but that's what he was
trying to do, violating the rules of the game (unfortunately,
there are some implementations that let you get away with this,
making learning this more difficult).
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de

Nov 14 '05 #17
>*p++=*s++

when you do stuff like this use parentheses because (++) and (*) are
associative from right to left and have equal precedence however if for some
reason you want it to read *(p++)= (*s++) it creates a different result...
Nov 14 '05 #18
st***********@aol.com (Strix Nihildom) writes:
*p++=*s++
when you do stuff like this use parentheses


I disagree. This statement is idiomatic. Every C programmer
should understand what it means.
[...] if for some reason you want it to read *(p++)= (*s++) it
creates a different result...


No, those two expressions are equivalent.
--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_
Nov 14 '05 #19

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

Similar topics

3
by: Mike Henley | last post by:
I first came across rebol a while ago; it seemed interesting but then i was put off by its proprietary nature, although the core of the language is a free download. Recently however, i can't...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
28
by: Madhur | last post by:
Hello what about this nice way to open a file in single line rather than using if and else. #include<stdio.h> void main() { FILE *nd; clrscr();...
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
3
by: Siong.Ong | last post by:
Dear all, my PHP aims to update a MySQL database by selecting record one by one and modify then save. Here are my PHP, but I found that it doesnt work as it supposed to be, for example, when...
89
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the...
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
24
by: MU | last post by:
Hello I have some code that sets a dropdownlist control with a parameter from the querystring. However, when the querystring is empty, I get an error. Here is my code: Protected Sub...
2
by: mingke | last post by:
Hi... So I have problem with my if condition..I don't know what's wrong but it keeps resulting the wrong answer.... So here's the part of my code I have problem with: for (i=0; i<size2;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...

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.