Author |
Message |
tack #1 / 13
|
 Help - reverse string
Hi, i can't get my the correct result of the return of the fuction. actually i need the function passing with str[] and reverse to tempArr[] and return to main, but garbage return. this is my code: #include <stdio.h> #include <conio.h> char str[] = {'A','B','C'}; char *ptr(char strArr[3]); void main() { clrscr(); *ptr(str); printf("%s",*ptr); getch(); } char *ptr(char strArr[3]) { char tempArr[3]; for(int x=2; x>=0; x--) { for(int y=0;y<=2;y++) tempArr[y]=strArr[x]; } return tempArr; } pls help... thankyou.
|
Wed, 30 Mar 2005 20:02:32 GMT |
|
 |
Martin Renni #2 / 13
|
 Help - reverse string
Quote:
> Hi, > i can't get my the correct result of the return of the fuction. > actually i need the function passing with str[] and reverse to > tempArr[] and return to main, but garbage return. > this is my code: > #include <stdio.h> > #include <conio.h> > char str[] = {'A','B','C'}; > char *ptr(char strArr[3]); > void main() > { > clrscr(); > *ptr(str);
With the above statement you are discarding the return value. Also it should be ptr(str) - drop the asterisk. Quote: > printf("%s",*ptr);
This line is just crazy - if you want to print the result of the function use printf("%s", ptr(str)); Quote: > char *ptr(char strArr[3]) > { > char tempArr[3]; > for(int x=2; x>=0; x--) > { > for(int y=0;y<=2;y++) > tempArr[y]=strArr[x]; > } > return tempArr; > }
This is a big no-no. You've declared an automatic array (tempArr), local to the function. You then return a pointer to it when the function ends. However automatic variables are destroyed when they go out of scope (in this case when the function ends). So your returned pointer will point to invalid data. A quick fix is to make tempArr a global variable. Another is to make tempArr static within the function. Yet another is to use malloc() to create storage that can be accessed globally, but then you have to remember to free() it later. -- Martin
|
Wed, 30 Mar 2005 20:56:06 GMT |
|
 |
Richard Heathfiel #3 / 13
|
 Help - reverse string
Quote:
> Hi, > i can't get my the correct result of the return of the fuction. > actually i need the function passing with str[] and reverse to > tempArr[] and return to main, but garbage return. > this is my code: > #include <stdio.h> > #include <conio.h> > char str[] = {'A','B','C'}; > char *ptr(char strArr[3]); > void main() > { > clrscr(); > *ptr(str); > printf("%s",*ptr); > getch(); > } > char *ptr(char strArr[3]) > { > char tempArr[3]; > for(int x=2; x>=0; x--) > { > for(int y=0;y<=2;y++) > tempArr[y]=strArr[x]; > } > return tempArr; > } > pls help... > thankyou.
Here's what my compiler said: rev.c:2: conio.h: No such file or directory So I fixed that by removing the line altogether. Then the compiler said: rev.c:7: warning: return type of `main' is not `int' rev.c: In function `main': rev.c:8: warning: implicit declaration of function `clrscr' rev.c:9: warning: value computed is not used rev.c:10: warning: char format, different type arg (arg 2) rev.c:11: warning: implicit declaration of function `getch' rev.c: In function `ptr': rev.c:17: parse error before `int' rev.c:17: `x' undeclared (first use in this function) rev.c:17: (Each undeclared identifier is reported only once rev.c:17: for each function it appears in.) rev.c:17: warning: statement with no effect rev.c:17: parse error before `)' rev.c:19: `y' undeclared (first use in this function) rev.c:19: warning: statement with no effect rev.c:19: parse error before `)' rev.c:16: warning: unused variable `tempArr' rev.c:14: warning: unused parameter `strArr' rev.c:21: warning: control reaches end of non-void function So I fixed all those, plus one or two bugs that I came across: #include <stdio.h> char *ptr(char strArr[3]); int main() { char str[4] = {'A','B','C'}; char *rev = ptr(str); printf("%s\n", rev); puts("Press ENTER to continue."); getchar(); return 0; } char *ptr(char strArr[3]) { static char tempArr[3]; int x; int y; for(x=2; x>=0; x--) { for(y=0;y<=2;y++) tempArr[y]=strArr[x]; } return tempArr; } All that's wrong now is: 1) your algorithm doesn't work; 2) your function only handles strings with three bytes in them. To fix your algorithm is easy: char *ptr(char strArr[3]) { static char tempArr[4] = {0}; int x; for(x = 2; x >= 0; x--) { tempArr[2 - x] = strArr[x]; } return tempArr; Quote: }
To fix your function so that it handles any length string is a bit harder, but not much. A Google search through the comp.lang.c archives with the keywords "reverse string" should give you lots of hints. --
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999. C FAQ: http://www.eskimo.com/~scs/C-faq/top.html K&R answers, C books, etc: http://users.powernet.co.uk/eton
|
Wed, 30 Mar 2005 21:18:35 GMT |
|
 |
Joe Wrigh #4 / 13
|
 Help - reverse string
Quote:
> Hi, > i can't get my the correct result of the return of the fuction. > actually i need the function passing with str[] and reverse to > tempArr[] and return to main, but garbage return. > this is my code: > #include <stdio.h> > #include <conio.h>
conio.h is not Standard. Quote: > char str[] = {'A','B','C'}; > char *ptr(char strArr[3]); > void main()
You want 'int main(void)' here. Quote: > { > clrscr();
clrscr() is not Standard. Quote: > *ptr(str);
What is the '*' for? Quote: > printf("%s",*ptr); And here? > getch();
getch() is not Standard. Quote: > } > char *ptr(char strArr[3]) > { > char tempArr[3]; > for(int x=2; x>=0; x--)
You can't declare 'int x' in the for statement in Standard C. Quote: > { > for(int y=0;y<=2;y++) > tempArr[y]=strArr[x]; > } > return tempArr;
You return the address of an automatic variable which disappears as the return completes. Quote: > } > pls help... > thankyou.
I think you could use a good tutorial text and a language reference. #include <stdio.h> char str[] = "ABCDE"; void rev(char *); int main(void) { printf("Before: %s\n", str); rev(str); printf(" After: %s\n", str); return 0; Quote: }
void rev(char *s) { char *d = s; char c; while (*d) ++d; --d; while (d > s) { c = *s; *s++ = *d; *d-- = c; } Quote: }
--
"Everything should be made as simple as possible, but not simpler." --- Albert Einstein ---
|
Wed, 30 Mar 2005 21:46:17 GMT |
|
 |
Richard Heathfiel #5 / 13
|
 Help - reverse string
<snip> Quote: > void rev(char *s) { > char *d = s; > char c; > while (*d) ++d; > --d;
Undefined behaviour if *s is '\0'. <snip> --
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999. C FAQ: http://www.eskimo.com/~scs/C-faq/top.html K&R answers, C books, etc: http://users.powernet.co.uk/eton
|
Wed, 30 Mar 2005 22:09:04 GMT |
|
 |
Joe Wrigh #6 / 13
|
 Help - reverse string
Quote:
> <snip> > > void rev(char *s) { > > char *d = s; > > char c; > > while (*d) ++d; > > --d; > Undefined behaviour if *s is '\0'. > <snip>
Good catch. --
"Everything should be made as simple as possible, but not simpler." --- Albert Einstein ---
|
Wed, 30 Mar 2005 23:14:41 GMT |
|
 |
Mark McIntyr #7 / 13
|
 Help - reverse string
Quote:
>Hi, >i can't get my the correct result of the return of the fuction. >actually i need the function passing with str[] and reverse to >tempArr[] and return to main, but garbage return. >this is my code: > #include <stdio.h> > #include <conio.h> > char str[] = {'A','B','C'}; > char *ptr(char strArr[3]); > void main()
Either "reverse a string" is part of a standard course, or you've posted before. Either way, anyone posting here should already know that void main() is not legal C. In C, main returns an int. Quote: > { > clrscr();
there's a school of thought that says that its rude to clear the screen. How do you know that the user doesn't want to see what was there before? Quote: > *ptr(str);
This has no effect. You pass str into the function ,but then don't put the returned value anywhere. Try strcpy(somestring. ptr(str)); but see the comment about ptr later. By the way, ptr is a very bad name for a function. Most people would think it was a variable of type pointer to something. Quote: > printf("%s",*ptr);
ptr is a function, you can't print it out. You presumably want to print out what ptr returned. In that case, once you've assigned it to a variable, you can print it. Quote: > getch();
getch is nonstandard. Try using getchar() instead. Quote: > } > char *ptr(char strArr[3]) > { > char tempArr[3]; > for(int x=2; x>=0; x--) > { > for(int y=0;y<=2;y++) > tempArr[y]=strArr[x]; > } > return tempArr;
1) you can't return an array from a function. This statement returns a pointer to the array. 2) tempArr is a pointer to a local variable which is destroyed when the function ends. Therefore the returned value points to nothing valid and contains garbage. Quote: > }
-- Mark McIntyre CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html> CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
|
Thu, 31 Mar 2005 03:37:40 GMT |
|
 |
Peter Nilsso #8 / 13
|
 Help - reverse string
Quote:
... > > for(int x=2; x>=0; x--) > You can't declare 'int x' in the for statement in Standard C.
You can in C99. -- Peter
|
Thu, 31 Mar 2005 10:47:02 GMT |
|
 |
Mark McIntyr #9 / 13
|
 Help - reverse string
On Sun, 13 Oct 2002 12:47:02 +1000, in comp.lang.c , "Peter Nilsson" Quote:
>... >> > for(int x=2; x>=0; x--) >> You can't declare 'int x' in the for statement in Standard C. >You can in C99.
And ISTR that the scope of x is another of the "insignificant" differences between C and C++, although ICBW. -- Mark McIntyre CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html> CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
|
Fri, 01 Apr 2005 03:25:26 GMT |
|
 |
Ben Pfaf #10 / 13
|
 Help - reverse string
Quote:
> On Sun, 13 Oct 2002 12:47:02 +1000, in comp.lang.c , "Peter Nilsson"
> >> > for(int x=2; x>=0; x--) > >> You can't declare 'int x' in the for statement in Standard C. > >You can in C99. > And ISTR that the scope of x is another of the "insignificant" > differences between C and C++, although ICBW.
In both C++98 and C99, the scope of `x' is the `for' statement only.
|
Fri, 01 Apr 2005 04:03:33 GMT |
|
 |
Thomas Stege #11 / 13
|
 Help - reverse string
Quote:
>>On Sun, 13 Oct 2002 12:47:02 +1000, in comp.lang.c , "Peter Nilsson"
>>>>> for(int x=2; x>=0; x--) >>>>You can't declare 'int x' in the for statement in Standard C. >>>You can in C99. >>And ISTR that the scope of x is another of the "insignificant" >>differences between C and C++, although ICBW. > In both C++98 and C99, the scope of `x' is the `for' statement > only.
As it is in Java as well. IOW, C is a subset of Java. -- Thomas.
|
Fri, 01 Apr 2005 03:17:21 GMT |
|
 |
Ben Pfaf #12 / 13
|
 Help - reverse string
Quote:
> > In both C++98 and C99, the scope of `x' is the `for' statement > > only. > As it is in Java as well.
Yes. Quote: > IOW, C is a subset of Java.
That does not follow and it's incorrect anyway.
|
Fri, 01 Apr 2005 06:07:12 GMT |
|
 |
Thomas Stege #13 / 13
|
 Help - reverse string
Quote:
>>> In both C++98 and C99, the scope of `x' is the `for' statement >>>only. >>As it is in Java as well. > Yes. >>IOW, C is a subset of Java. > That does not follow and it's incorrect anyway.
I was trying to make the point that C is not a subset of C++. In a rather roundabout way I must admit, but sometimes making an outrageous claim might shed some light on a similar but not as outrageus claim. Just in case any newbies were reading. -- Thomas.
|
Fri, 01 Apr 2005 05:47:12 GMT |
|
|