Thread: function return problem

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    function return problem

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char* reverse(char* a)
    {
      int temp, x, y = strlen(a) - 1;
    
      for ( x = 0; x < y; x++, y-- )
        temp = a[x], a[x] = a[y], a[y] = temp;
    
      return a;
    }
    
    int main()
    {
        char *text = "hello world";
    How do I reverse variable text using reverse() and assign the result to a new variable char*? Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    temp = a[x], a[x] = a[y], a[y] = temp;
    These are separate statements, need to separated by ;

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    When you post a program like this, it's always a good idea to state what specifically is the problem. For example, any errors messages or unexpected behavior.

    I would imagine any problems are coming from your use of compound assignment statements. Try and separate them out a bit to make them easier for you to read.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Everything works fine. You don't need to worry about function reverse(). My problem is that I don't know how to assign what reverse() returns to a new variable.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Code:
    y = f(x);
    Where y is the variable you want the return value assigned to, f is your function, and x is your parameter.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    OK, but what type should y be?

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Whatever type you're returning. In this case, a pointer to char (char *).

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    It doesn't work
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char* reverse(char* a)
    {
      int temp, x, y = strlen(a) - 1;
    
      for ( x = 0; x < y; x++, y-- )
        temp = a[x], a[x] = a[y], a[y] = temp;
    
      return a;
    }
    
    int main()
    {
    	char *text = "hello world";
    	char *reversed = reverse(text);
    
    	return 0;
    }
    Try to run that.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Everything works fine
    Apparently not. Try making the changes already suggested to you. On second inspection, your return function is in serious need of a complete re-write. You take a pointer to an array as a parameter, but then treat that pointer like the array itself.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You are trying to modify a string literal. String literals are allowed to be placed in read only memory.

    Try changing
    Code:
    char *text
    to
    Code:
    char text[]

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Worked
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Function return value conflict
    By cashmerelc in forum C Programming
    Replies: 6
    Last Post: 11-12-2007, 02:05 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM