LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2002, 09:40 PM   #1
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Rep: Reputation: 30
need help with the following C function ...


Hi. Can someone help me with the following code? I know something is wrong but not what it is.

gcc gives me these errors>
reverse_str.c: In function `reverse_string':
reverse_str.c:31: warning: return makes integer from pointer without a cast
reverse_str.c:31: warning: function returns address of local variable

--------------------------------------------------------
#include <stdio.h>

char reverse_string(char *str);

int main(void)
{

char *string = "This will print backwards";

printf("%s\n", reverse_string(string));

return 0;

}
char reverse_string(char *str)
{

char str2[40];
int x = 0;
int y = 0;

while (str[x] != '\0')
x++;

while ( x >= 0)
str2[y++] = str[x--];

/* append NULL character to string */
str2[y] = '\0';

return str2;

}

thanks
 
Old 11-13-2002, 07:49 AM   #2
niac
LQ Newbie
 
Registered: Oct 2002
Location: France
Distribution: RH 8
Posts: 14

Rep: Reputation: 0
Your algo is not correct x get the value where str[x]='\0'
then you make str2[0]=str[x++]
str2[0]='\0' not what you want. Thus printf() will stop at the first cell.

Moreover you return a pointer which is local : try to avoid this. Local varilable are deleted after exiting the function.

Here will be the correct way :

#include <stdio.h>

void reverse_string(char *str, char *str2);

void reverse_string(char *str, char *str2) {
int x = 0;
int y = 0;
while (str[x] != '\0')
x++;
while ( x >= 0)
str2[y++] = str[--x];
/* append NULL character to string */
str2[y] = '\0';
}

int main(void) {
char *string = "This will print backwards";
char str2[40];
reverse_string(string,str2);
printf("%s\n", str2);
return 0;
}
 
Old 11-13-2002, 07:54 AM   #3
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
Well there are many things which are wrong with your program.
- You should be returning a char * not just a character in the reverse_string function.
- You shouldn't return an address to a local variable. You could copy str2 into str and then return str instead of str2.
- You shouldn't assign a literal string to a string pointer. Use strcpy for that.
- The second while loop starts with copying the \0 into the string so the result string will always be empty. You could use --x instead of x-- to solve this.
- The null character is appended one place to far so you should use y - 1 for that.

Well after you change all that it should work. It did here at least.


Hmm looks like I was too slow again.

Last edited by Mik; 11-13-2002 at 07:58 AM.
 
Old 11-13-2002, 07:59 PM   #4
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *  func (char *, int);

int main()
{

  char str [] = "Here is a string";
  printf("%s\n", func ( str, strlen(str) - 1) ) ;

  return 0;
}

char *  func (char  * s, int size)
{
  char *ptr = s;
  char * rStr = malloc (256);
  int index = 0;

  while( size >= 0)
    {
      rStr[index] = *( ptr + size);
      ++index;
      --size;
      ptr = s;
    }
  rStr[index] = '\0';
  return rStr;
}
Here is one version. Geez, Gnome terminal is a piece of junk. I had to use XTerm to get this program to display properly. Anyway, the main point is that you have to declare the return string as static storage space on the free store. If you just make a local variable than the pointer that you are returning to it will point to unallocated memory as soon as the function returns, the stack is released.

BTW I'm sure there are nicer examples, but this one does work.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling another function from a function using GTK geminigal Programming 4 07-11-2005 03:15 PM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Is the wait function is the same as the sleep function ? Linh Programming 3 04-28-2004 12:39 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration