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

Home Posts Topics Members FAQ

what's wrong with my dummy code?

hi there!
I just wrote a function that reverse a string. It seems all ok to me,
but the program goes on segmentation fault on *s = *t_str.
Here's the code:

#include <stdio.h>
#include <stdlib.h>

void reverse(char *s)
{
char t;
char* t_str;

for (t_str = s; *t_str != '\0'; t_str++);
t_str--;

for (; s < t_str; s++, t_str--) {
t = *s;
*s = *t_str;
*t_str = t;
}
}

int main(int argc, char *argv[])
{
char* s = "Hello world!";
reverse(s);
printf("%s", s);

return 0;
}

what's wrong??!
thanks!

--

Shya,
Nov 15 '05 #1
12 2351
shya wrote:
[...] what's wrong??!


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

--
post tenebras lux. post fenestras tux.
Nov 15 '05 #2
shya wrote:
char* s = "Hello world!";


char s[] = "Hello world!";

will be OK.

Nov 15 '05 #3

shya wrote:
hi there!
I just wrote a function that reverse a string. It seems all ok to me,
but the program goes on segmentation fault on *s = *t_str.
Here's the code:

#include <stdio.h>
#include <stdlib.h>

void reverse(char *s)
{
char t;
char* t_str;

for (t_str = s; *t_str != '\0'; t_str++);
t_str--;

for (; s < t_str; s++, t_str--) {
t = *s;
*s = *t_str;
*t_str = t;
}
}

int main(int argc, char *argv[])
{
char* s = "Hello world!";
reverse(s);
printf("%s", s);

return 0;
}

what's wrong??!
thanks!

--

Shya,


PS: Changing the arguments of a function isn't a good habit.

Nov 15 '05 #4
Cong Wang wrote:
PS: Changing the arguments of a function isn't a good habit.


I prefer to alter function parameters at every given oportunity.

--
pete
Nov 15 '05 #5

shya wrote:
hi there!
I just wrote a function that reverse a string. It seems all ok to me,
but the program goes on segmentation fault on *s = *t_str.
Here's the code:

#include <stdio.h>
#include <stdlib.h>

void reverse(char *s)
{
char t;
char* t_str;

for (t_str = s; *t_str != '\0'; t_str++);
t_str--;

for (; s < t_str; s++, t_str--) {
t = *s;
*s = *t_str;
*t_str = t;
}
}

int main(int argc, char *argv[])
{
char* s = "Hello world!";
reverse(s);
printf("%s", s);

return 0;
}

what's wrong??!
thanks!

What compiler are you using? For gcc you may need to use the
--writable-strings flag. gcc --writable-strings gg.c && ./a.out

!dlrow olleH> ~/tmp>

--
Cristian Trofinenco (CW42)
Tel.: (613) 763-8674 (ESN 393-8674)
ct*****@nortel.com

Nov 15 '05 #6
Trofinenco, Cristian [CAR:CW42:EXCH] wrote:

<snip code that modifies a string literal>
What compiler are you using? For gcc you may need to use the
--writable-strings flag.
> gcc --writable-strings gg.c && ./a.out

!dlrow olleH> ~/tmp>


That is the *wrong* answer. Although it gets the expected result on your
version of gcc, it is NOT available on the latest version of gcc and
other compilers may well not have this feature. Also, this group only
deals with standard C, not extensions available on specific compilers.

The correct answer, one which I believe was given by someone else, is
*don't* modify string literals since the standard states that this is
undefined behaviour. Use an initialised character array instead.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #7
Cong Wang wrote:
[...]
PS: Changing the arguments of a function isn't a good habit.


I guess you've never used sprintf, strcpy, fread, ...

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Nov 15 '05 #8
Kenneth Brody <ke******@spamcop.net> writes:
Cong Wang wrote:
[...]
PS: Changing the arguments of a function isn't a good habit.


I guess you've never used sprintf, strcpy, fread, ...


Those functions don't (necessarily) change their parameters; they
change the objects that their parameters point to.

What was being called a bad habit was this kind of thing:

void foo(int x) {
{
x ++; /* changing the parameter */
}

There's nothing incorrect about doing this, but it can cause confusion
(though of course it won't affect the actual argument that was passed
to the function). Whether it's good or bad style in general is a
debate that I'm not going to get into, at least for now.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #9

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
Kenneth Brody <ke******@spamcop.net> writes:
Cong Wang wrote:
[...]
PS: Changing the arguments of a function isn't a good habit.
I guess you've never used sprintf, strcpy, fread, ...


Those functions don't (necessarily) change their parameters; they
change the objects that their parameters point to.

Which is exactly what Cong complained about.
What was being called a bad habit was this kind of thing:

void foo(int x) {
{
x ++; /* changing the parameter */
} No, it wasn't that kind of thing.
There's nothing incorrect about doing this, but it can cause confusion I disagree. Personal style issue, nothing more.
(though of course it won't affect the actual argument that was passed
to the function). Whether it's good or bad style in general is a
debate that I'm not going to get into, at least for now. Good... because the actual code in question was as follows:
void reverse(char *s)
{
char t;
char* t_str;

for (t_str = s; *t_str != '\0'; t_str++);
t_str--;

for (; s < t_str; s++, t_str--) {
t = *s;
*s = *t_str;
*t_str = t;
}
}


Sometimes it is the PURPOSE of a function to change the object
that the parameters point to... Ken was merely trying to point that out.

Mark
Nov 15 '05 #10
"Mark" <so***@localbar.com> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
Kenneth Brody <ke******@spamcop.net> writes:
Cong Wang wrote:
[...]
PS: Changing the arguments of a function isn't a good habit.

I guess you've never used sprintf, strcpy, fread, ...


Those functions don't (necessarily) change their parameters; they
change the objects that their parameters point to.

Which is exactly what Cong complained about.
What was being called a bad habit was this kind of thing:

void foo(int x) {
{
x ++; /* changing the parameter */
}

No, it wasn't that kind of thing.


Yes, it was; see below.
There's nothing incorrect about doing this, but it can cause confusion

I disagree. Personal style issue, nothing more.
(though of course it won't affect the actual argument that was passed
to the function). Whether it's good or bad style in general is a
debate that I'm not going to get into, at least for now.

Good... because the actual code in question was as follows:
void reverse(char *s)
{
char t;
char* t_str;

for (t_str = s; *t_str != '\0'; t_str++);
t_str--;

for (; s < t_str; s++, t_str--) {
t = *s;
*s = *t_str;
*t_str = t;
}
}


Sometimes it is the PURPOSE of a function to change the object
that the parameters point to... Ken was merely trying to point that out.


I think you missed it. The argument to reverse() is called s. The
function changes the value of s in the second for loop; see "s++".
(It also changes what s points to, but that's a different matter.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #11
shya wrote:
hi there!
I just wrote a function that reverse a string. It seems all ok to me,
but the program goes on segmentation fault on *s = *t_str.
Do you still get one if instead of trying to reverse a string literal (a
bad thing) you try to reverse an array containing a string? Change char* s = "Hello world!"; to
char s[] = "Hello world!";

While you're at it, make your program portable by changing
printf("%s", s);

to
printf("%s\n", s);

Not writing an end-of-line character at the end of the last line of
output is not a good idea.
Nov 15 '05 #12
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Mark" <so***@localbar.com> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
Kenneth Brody <ke******@spamcop.net> writes:
Cong Wang wrote:
[...]
> PS: Changing the arguments of a function isn't a good habit.

I guess you've never used sprintf, strcpy, fread, ...

Those functions don't (necessarily) change their parameters; they
change the objects that their parameters point to.

Which is exactly what Cong complained about.
What was being called a bad habit was this kind of thing:

void foo(int x) {
{
x ++; /* changing the parameter */
}

No, it wasn't that kind of thing.


Yes, it was; see below.
There's nothing incorrect about doing this, but it can cause confusion

I disagree. Personal style issue, nothing more.
(though of course it won't affect the actual argument that was passed
to the function). Whether it's good or bad style in general is a
debate that I'm not going to get into, at least for now.

Good... because the actual code in question was as follows:
void reverse(char *s)
{
char t;
char* t_str;

for (t_str = s; *t_str != '\0'; t_str++);
t_str--;

for (; s < t_str; s++, t_str--) {
t = *s;
*s = *t_str;
*t_str = t;
}
}


Sometimes it is the PURPOSE of a function to change the object
that the parameters point to... Ken was merely trying to point that out.


I think you missed it. The argument to reverse() is called s. The
function changes the value of s in the second for loop; see "s++".
(It also changes what s points to, but that's a different matter.)


Yes, I missed it :-)
Thanks for pointing that out.

After re-reading the thread I see that rather than sprintf() strcpy()
and fread() Ken should have said strcat(), strcpy(), strcmp() !!!
Many implementations of those functions do 'change' their parameters,
and why not? Parameters belong to the function... they are theirs
to use and change as the function sees fit.

Now please allow me to take this thread in another direction...
(I think I already did ;-) I know you don't want to get into the
'style' issue, but why not?

To make a broad statement such as: changing the arguments of a
function isn't a good habit - is not a good habit. Depending on the
circumstances, it doesn't bother me in the least to see it done.

consider the following:

void
do_something(int max_times)
{
while(max_times--)
something();
return;
}

Looks fine to me... doesn't bother me in the least.
Personally, I prefer that to code which used a copy
of max_times for no other reason than to be anal.

I've seen the arguments before...
for: clean concise code.
against: can't determine initial value.
for: who gives a 5hit? I don't need the initial value.
against: something() may break, debugger lost the initial value.
for: go up a frame
.... [it goes on and on] ...
against: I once read on comp.lang.c that it's not a good habit.
for: *&$K them too :-)

Which side of the fence are you guys on? :-)

For those on the 'against' side: you may want to take a look
at your implementation's standard library functions, you may
have to rewrite some of them if it bothers you that much.

I look forward to reading all responses... on Monday!
Have a nice weekend guys :-)
Mark
Nov 15 '05 #13

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

Similar topics

4
by: Stub | last post by:
I read the code in a book and it seems not quite right to me. The possible mistake seems in the dummy::getData(). It returns a local pointer, void* data, back to its caller. Am I right about...
11
by: Sims | last post by:
Hi, I have a function that looks something like .... void Function( std::string &oldval ) { int iSome_size = xxx; // get some size char *tmp = NULL; tmp = new char;
5
by: Daniel | last post by:
I need to reverse the doubly linked list with dummy node. I think the solution is to exchange each node pointers' next and previous address. But what's wrong in my function? Thanks void...
11
by: Tommy Martin | last post by:
I am trying to use a bitmap in my asp.net page and I am running into a problem because the name of the path has a space in it. Is this a limitation of the function or am I just overlooking...
12
by: questions? | last post by:
I am testing a problem with linked list. I just do a lot of times: create a list, then free it. ############################################# # include <stdio.h> # include <stdlib.h> struct...
3
by: smorrey | last post by:
Howdy folks, I recently purchased a book on C++ MUD creation and it features alot of nifty tidbits. The book is MUD GAME PROGRAMMING by Ron Penton Publisher: Premier Press Anyways of...
83
by: Anonymous | last post by:
Came across some code summarized as follows: char const* MyClass::errToText(int err) const { switch (err) { case 0: return "No error"; case 1: return "Not enough"; case 2: return "Too...
1
by: ahammad | last post by:
Hello, I have written a fairly complex parsing tool that is used to parse information from company documents. The program works very well, but in order to insure that all the data is copied...
35
by: erik gartz | last post by:
Hi. I'd like to be able to write a loop such as: for i in range(10): pass but without the i variable. The reason for this is I'm using pylint and it complains about the unused variable i. I can...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
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.