Completely baffled

This is a discussion on Completely baffled within the C Programming forums, part of the General Programming Boards category; So I thought a while back I had away of returning an array from a function by returning a pointer ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    8

    Completely baffled

    So I thought a while back I had away of returning an array from a function by returning a pointer and then using *(ptr + n) where n is 1, 2, 3 etc. to get the elements out. I seem to recall this working but at the moment it isn't, and I can't figure out why it does this. Here is the code I have:
    Code:
    double *final_ptr = box(data, width, range);    
    printf("final pointer = %f\n", *final_ptr);  
    double final_array[5] = { 0 };
    final_array[0] = *final_ptr;
    printf("final array : %f\n", final_array[0]);
    Which was mostly some test code I put in to try to pin down why this wasn't working. The weird thing is it prints:

    final pointer = 1.5 (what I wanted)
    final array : 0

    I do not understand why. But this doesn't happen if you write a = 6; b = a, and then print a and b. So I imagine that it is either something to do with using a pointer or else something to do with the function "box" I have written. But the "box" function returns the result I want, and that seems to carry over to the main function. It is just when I want to put it into an array it does not seem to work. Is there some rule that I am unaware of here?

    Any help is much appreciated, thanks.

    Edit: I have also just tried putting just a standard double and making it equal to *final_ptr. This also then prints 0. So it seems that, apart from the actual pointer, everything thinks that it is pointing to 0. As you can tell I'm not exactly an expert in C but this seems really weird to me.
    Last edited by Fyzix; 12-31-2012 at 01:11 PM.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    I seem to recall this working but at the moment it isn't
    Returning an array from a function is the same as returning a pointer to a local variable from a function which is undefined behavior. If it does work, you got lucky. It isn't, wasn't, and can't be made guaranteed to work.

    What you are to do instead in C is use function parameters to edit your arrays. Pass in your arrays as a pointer to the first element. Having the arrays come from the calling code through parameters means that they will be in scope for the duration of the function's execution and after the function ends.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    8
    Ah, thank you very much for clearing that up. With regard to using function parameters to edit arrays, could you give me a quick example of how that is used? For example if I wanted to do a really simple function that just added 1 to each element of the array, how would I go about doing that? Is it as simple as just:
    Code:
    int *function(int *number)
    {
      //loop through and add one to all the elements
      return(number)
    }
    Thanks for all your help.
    Last edited by Fyzix; 12-31-2012 at 01:28 PM.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Quote Originally Posted by Fyzix View Post
    Ah, thank you very much for clearing that up. With regard to using function parameters to edit arrays, could you give me a quick example of how that is used? For example if I wanted to do a really simple function that just added 1 to each element of the array, how would I go about doing that?
    Yes. You can return the parameter if you want to, but I usually don't end up using it, anyway:
    Code:
    void function ( int *number, int numberofelements )
    {
       int i;
       for ( i = 0; i < numberofelements; i++ ) {
          number[i] += 1;
       }
    }
    After this finishes, print the array in the calling code and you'll see the changes.

    Thanks for all your help.
    No problem

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Quote Originally Posted by whiteflags View Post
    Returning an array from a function is the same as returning a pointer to a local variable from a function which is undefined behavior.
    A minor correction: the act of returning a pointer to a local variable does not cause undefined behaviour. Undefined behaviour occurs if the caller subsequently dereferences that pointer.

    It is the impact on the caller (after all, given a pointer, a likely use case is to dereference it) that makes returning the address of a local/auto variable a bad idea.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Good point. If one was simply to compare it to NULL only, then that should be fine.
    Of course nobody is saying that it is the least bit good to do.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    I don't think there is a situation where properly written comparisons are broken, anyway, but that would be interesting. And scary. But interesting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. factoring - baffled
    By strokebow in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2012, 11:41 AM
  2. Replies: 10
    Last Post: 12-08-2011, 11:27 AM
  3. Baffled By Question
    By ASCII in forum C Programming
    Replies: 9
    Last Post: 08-28-2011, 05:41 PM
  4. Newbie baffled
    By Steve MacD in forum C Programming
    Replies: 8
    Last Post: 02-17-2005, 10:44 AM
  5. baffled!
    By The Dog in forum C Programming
    Replies: 3
    Last Post: 07-03-2002, 06:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21