Thread: Passing values/by reference to C functions from main() and receiving them?

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    4

    Passing values/by reference to C functions from main() and receiving them?

    I have trouble in declaring variables in main() so that it can be passed to a C function - also declaring variables in C functions so that it can be returned to the main() without using the return statment. I know pointers are ofter used. Then arrays could also be used. The dimensions of the arrays do not always needs to be declared in the C function - but it seems the main() always want it be declared if it is an array - there are times, we do not the array size - I had a working code where one of the dimension of a 2D array was blank, var[][10], in a C function.

    In this statement
    int *j = 0
    is j = 0 or address of j = 0? Why in main it has the received with &varm to correctly pass the value?

    The help I am seeking is an illustration of these possible scenarios and preferably an explanation - could someone help?

    If a C function mallocs a variable and does some computation - then you would want to free this variable. If you free, and the function intend passes the variable through the function call, will the variable would have become NULL before main() receives the benefits of computation? What do you do?

    Thanks if you could help.
    Last edited by uws; 04-17-2015 at 07:04 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Something some libraries do is to allocate and set the memory, while explicitly asking in their documentation from the user to free it when done with it. Many others provide add/remove or grab/release interfaces, stating that in order to avoid memory leaks and/or unwanted library behavior, for example an "add" should be followed by a "remove".
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Answering your first question, partially: you are giving the pointer j the value 0 or NULL. This means j will initially point to nothing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-03-2015, 07:06 PM
  2. Passing in values to functions
    By Mentallic in forum C Programming
    Replies: 7
    Last Post: 10-11-2012, 01:34 AM
  3. Problems passing values from function to main variables
    By ERJuanca in forum C Programming
    Replies: 18
    Last Post: 06-12-2009, 07:13 PM
  4. Passing argc and argv from main to other functions
    By Degenko in forum C Programming
    Replies: 1
    Last Post: 06-06-2007, 01:57 PM
  5. functions passing values
    By srinurocks in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2002, 11:49 PM

Tags for this Thread