#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Get >1 variables from a function without using Global variables


    Hello,
    I always got more variables from function by using Global variables, but what if i cant use global variables? is there any chance t oget 3-4 variables from a function? and if yes by what way?

    Also how it works with arrays? hot to get on and more arrays from a function.

    Thanks
    C Beginner
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    Pass pointers to arguments (or references in C++). Alternatively, return a structure and decompose the bits of the structure.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    Yes i know i have t do it somehow with passing the pointers, but it never works for me the good way. so would be great if you can help me with an example to understand it the proper way. so the example... can be:

    I have three functions, main(), read(),write()
    In read i read something from file to array,
    and in write i printf the array,
    in main if c = getchar() is r i call function read and if c is w i call function write

    Thansk a lot!
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    This is why you go to school, to learn how to do things. Just asking for help won't teach you how to learn. There are _tons_ of resources available now that weren't when I was learning, take the time to earn your degree!

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    I understand what you mean, and i cant do nothing else just agree, so the i change my question :-) if you can please help me how to look after the information in english, is it passing pointer to function?

    because my biggest problem is my english, im hungarian living in slovakia, theres almost nothing about c in slovak language, and our teacher teaches us C in slovak so im unfamiliar with English names, she even pointers calls Ukazovatele or smerniky, so its really confusing.
  10. #6
  11. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    OK, here is an example:

    Code:
    #include <stdio.h>
    
    #define ARRLEN 25
    
    void funct(int *intval, char *chrArray, int arrlen){
            *intval = 12345;
            strncpy(chrArray, "here be tygers", arrlen-1);
    }
    
    int main(){
            int ival = 0;
            char carr[ARRLEN];
            strcpy(carr, "I am blank");
            printf("Before: ival: %d, carr: %s\n", ival, carr);
            funct(&ival, carr, ARRLEN);//note that carr is _already_ a pointer. could also use (in this case, but not in all cases!) sizeof(carr) for length
            printf("After: ival: %d, carr: %s\n", ival, carr);
            return 0;
    }

    Comments on this post

    • ptr2void agrees

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    Thanks a lot, this helped me a lot, really thanks

    have one question left, can you plese tell me if theres a solution, that we can transfer arrays from void functions? like
    void function(void);
    there will be array declared

    to void function2(void)
    there will use for example array[87]

    Dont need to tell me how to do it, just if its possible to do it in some not hard way, :-)

    Thanks for helping me,
  14. #8
  15. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    There are three basic areas of memory for a program. The heap is where you malloc memory from, the stack which 'magically' is created as you enter a routine (and 'magically' cleaned up when you leave) and static memory that was 'allocated' by the compiler when the program was built. Of course, there is memory that can be created as files, shared objects, etc., but those are not what I think you are after. In order to 'share' memory with a subroutine either the memory has to be common to the scope of the two functions ('global'), it must be passed in, so that the calling function 'owns' the memory, or it must be allocated on the heap inside the called function and then it is the callee's responsibility to deallocate the memory. Having said that, by using structures it is possible to 'allocate' memory on the stack through a return value (as I briefly alluded to originally). As a side note, use of the key word 'static' inside a function will result in memory that is no longer on the stack, so retains its value between function calls (note that this type of memory is very problematic when using threads).

    So, in your example, if you simply declare a local variable of array inside the function then it exists on the stack and hence goes out of scope when the function returns. While you might be able to access that memory outside the function call, it will be coincidental and is a bug waiting to manifest. You could alternatively malloc some memory and then return a pointer to that memory, but you have to then remember to free it when you are done.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    Thanks a lot for helping me to understand this part of C, i did it somehow similar to your example, and also spent whole day reading about strings and arrays and pointers, and hopefully my knowledge of C is a little bit deeper :-) Thanks

IMN logo majestic logo threadwatch logo seochat tools logo