Thread: segmentation: function called with multiple outputs?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    110

    segmentation: function called with multiple outputs?

    I have a function of the form:

    Code:
    float function(string[],i) {
    /*multiple operations, each with some output...and I know this works, as I have already ran it outside of this function and debugged it by printing each output*/
    
    switch(i) {                /* i equals 1,2, or 3, and selects the output of the function*/
      case 1: return (x); /* x is a float */
      break;
      case 2: return (n); /* n is an integer*/
      break;
      case 3: return (c); /*c is a char
      break;
      default: printf("Inavlid request\n");
    }
    } /*end function*/
    
    int main () {
    
    /*input string and i*/
    
    float answer=function(string,i);
    printf("Answer=%f\n",answer);
    
    return 0;
    }
    I'm guessing the fault is because I have declared function as a float, but in certain cases, the function is trying to return an int or char. (mind you, I still get this error when I select i=1, returning the float variable). I have tried declaring function as void, but got some compile-time errors.

    Is this problem likely to be causing my segmentation error or will it be something else?

    How can I ammend this?

    thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I find it very unlikely that the posted code would cause a segmentation fault. Generally, that means that some part of your code is accessing invalid memory. None of your code that you've posted does any memory access.

    So I expect the problem is in parts of the code that you haven't posted...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Ok, thats what I thought...I must have tried to access a string[n] with an invalid string number,n, I'll have to double check everything. On another note, is this a valid method of returning different values from a function? If the values are different types, as in mine, what do I declare the function as? void?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bertazoid View Post
    Ok, thats what I thought...I must have tried to access a string[n] with an invalid string number,n, I'll have to double check everything. On another note, is this a valid method of returning different values from a function? If the values are different types, as in mine, what do I declare the function as? void?
    In this case, all the values are converted to float.

    You can't return "different types" from a a single function - the compiler will convert it to whatever return type you have, and if that's not possible according to standard C type conversion, you will get an error (and even when it is possible, you sometimes get warnings if the compiler deems it "unlikely that you meant to do that").

    Declaring the function as a void will just disable the possibility of a return value at all.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Use a union.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MeTh0Dz View Post
    Use a union.
    That is a possibility, but not the only solution. Actually, I'd say the most likely scenario is that there should be more than one function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Depends on exactly how much is happening in the different cases, if it's only a couple lines of code. Somewhat pointless to make functions for all of them (unless you plan to do a lot of expansion later on).

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MeTh0Dz View Post
    Depends on exactly how much is happening in the different cases, if it's only a couple lines of code. Somewhat pointless to make functions for all of them (unless you plan to do a lot of expansion later on).
    But then you put the COMMON code in a function that is called from multiple places. There must clearly be a good reason for using a union to return things, and "I want to sometimes return an integer, sometimes a char and sometimes a float" is not a good answer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    I thought about making different functions, and using a function pointer, but the 'operations' I have used in this code are quite long (There is about 100 lines of code in place of that first comment), and so it would be nice to just be able to have the one, and select the output. I suppose actually for my purpose I could convert the char and int to float values, and keep track of this when calling the function, so that is one possibility. I will also look at unions, and also try it with different functions just to see if it works. Thanks.

  10. #10
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    If each one of those case statements is going to have 100 lines of code, you need to write separate functions for increased readibility and maintainability. Otherwise you will just have one extremely bloated function.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The question still is:
    1. Why do you want different types to be returned?
    2. How do you know WHICH type of result to expect?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    HI both, I've solved both of these problems now....after debugging for ages, I noticed I hadn't used '&' on one of the scanf functions lol, and as for the function problem, I ended up using two seperate functions and calling them as appropriate...it turned out after some thought, that I didn't have to include all 100 lines of code in each function after all, since there were bits that were and weren't needed for each one. It is still pretty lengthy, and I might make them into a header file, but at least I'm confident that it is optimized well (I'm sure there is no shorter way to do what I wanted...well, with my limited knowledge of c at least ). Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM