Thread: what type of parameters can be pass to function

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    68

    what type of parameters can be pass to function

    What type of parameters can be pass to function

    I wrote following function. I think it can pass the given parameters to function.

    Code:
    void function (char name[10], int age, char grade, int *intPointer, char *charPointer, struct record){
    	
    }
    Does it a valid function ? if not then what's wrong in the function ?
    can we pass function to function as parameter ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,686
    > Does it a valid function ? if not then what's wrong in the function ?
    The 'struct record' is incomplete.
    Is record the name of the struct, and the variable name unknown?
    Or is record the name of the variable of an unknown struct type.

    > can we pass function to function as parameter ?
    Yes.
    Look at qsort() in stdlib.h for an example.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    Quote Originally Posted by Salem View Post
    > Does it a valid function ? if not then what's wrong in the function ?
    The 'struct record' is incomplete.
    Is record the name of the struct, and the variable name unknown?
    Or is record the name of the variable of an unknown struct type.
    .
    record is name of structure and id and height is structure variable and name is structure member
    Code:
    struct record { int id; float height; } name;
    does it valid function that pass the structure ?
    Code:
    void function ( struct record){
        
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,686
    > does it valid function that pass the structure ?
    No, because it's basically the same as your first attempt.

    Code:
    void function ( struct record myvar ) {
       // do something with myvar.id and myvar.height
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    Quote Originally Posted by Salem View Post
    > does it valid function that pass the structure ?
    No, because it's basically the same as your first attempt.
    I understand now what types of parameter can be pass to function. I can pass integer to function

    I am struggling to pass array

    Code:
    #include <stdio.h>
    
    int main()
    {
       int array [5];  // array declration 
       
       function (value) // call function 
    	
        return 0;
    }
    
    
    void function( int value[5])
    {
    	int i;
        for( i = 0; i < 5; i++)
            value[i] = value[i] + 10;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,686
    > function (value)
    Try
    function (array) ;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    Quote Originally Posted by Salem View Post
    > function (value)
    Try
    function (array) ;
    Code execute without any error.

    I don't think its useful example. I want to understand how to pass array with useful example

    Code:
    #include <stdio.h>
    
    void function( int value[5])
    {
    	int i;
        for( i = 0; i < 5; i++)
    	{
            value[i] = value[i] + 10;
    	    printf("\n value %d ", value[i]);
    	}
    } 
    
    
    int main()
    {
       int array [5];  // array declration 
       
       function (array); // call function 
    	
        return 0;
    }
    value 4200965
    value 4200874
    value 10
    value 3133450
    value 4194442

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,686
    So initialise the array properly in main then
    Code:
    int main()
    {
       int array [5] = { 1, 10, 100, 1000, 10000 };  // array declration 
        
       function (array); // call function 
         
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    Quote Originally Posted by Salem View Post
    So initialise the array properly in main then
    Thank you salem , How argument pass by value or reference?

    Passing a pointer to function

    Code:
    #include <stdio.h>   
    void fun(int *pointer) 
    { 
       int i; 
       for ( i = 0; i < 8; i++) 
         printf("%d  ", pointer[i]); 
    } 
       
    int main() 
    { 
       int array[] = {1, 2, 3, 4, 5, 6, 7, 8}; 
    
    
       fun(array); 
       
       return 0; 
    }
    I do not understand what happen when we call the function ?

  10. #10
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    240
    C++ Pointers and Arrays

    Read more about Arrays and Pointers and you'll easily understand what's happening.

    When you declare "array", you create a pointer called 'array' which know it's size is 8 because you provide it with 8 elements in initialisation. So, you're indexes from 0-7 are initialised from 1-8. When you call fun(array), you need to know two things.
    First, what does 'fun' expect? It expects a pointer to an integer.
    Second, 'array' is a pointer to the first element of your array (which is an integer), i.e., to your element at index [0] which hold the number 1. This array has a size of 8 integers as mentioned above.

    So, you basically passed a pointer to a function expecting a pointer.

    > How argument pass by value or reference?

    Google.
    Passing by Value vs. by Reference Visual Explanation with animations and diagrams
    Passing by pointer Vs Passing by Reference in C++ - GeeksforGeeks

  11. #11
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    I need your help
    Code:
    #include <stdio.h>
    void fun(int *pointer)
    {
       int i;
       for ( i = 0; i < 5; i++)
         printf("%d  ", pointer[i]);
    }
     
    int main()
    {
       int array[] = {1, 2, 3, 4, 5}; 
    
       fun(array);  // I don't understand this line. there is no & operator 
     
       return 0; }
    What happen when we call function fun(array) ?

    Why we can't write fun(&array) instead of
    fun(array) ?

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,686
    > // I don't understand this line. there is no & operator
    Well fun(array); and fun(&array[0]); are the same thing.

    Arrays, when passed as a function parameter decay to being a pointer to the first element of the array.
    Question 6.3
    A reference to an object of type array-of-T which appears in an expression decays (with three exceptions) into a pointer to its first element; the type of the resultant pointer is pointer-to-T.
    Because all you get is a pointer, it's usual to also supply the size as an additional parameter.
    Code:
    void fun(int *pointer, size_t size)
    {
       for ( size_t i = 0; i < size; i++)
         printf("%d  ", pointer[i]);
    }
     
    int main()
    {
       int array[] = {1, 2, 3, 4, 5}; 
       fun(array,5);
       return 0;
    }

    > Why we can't write fun(&array) instead of fun(array)
    You can!
    But it's a different kind of pointer.
    It's a pointer to the WHOLE array, not just one element of the array.
    It looks like this.
    Code:
    // pointer is a pointer to an array of 5 ints
    void fun(int (*pointer)[5], size_t size)
    {
       for ( size_t i = 0; i < size; i++)
         printf("%d  ", (*pointer)[i]);
    }
     
    int main()
    {
       int array[] = {1, 2, 3, 4, 5}; 
       fun(&array,5);
       return 0;
    }
    You have to be careful with the positioning of the parentheses.
    int (*pointer)[5] is NOT the same as int *pointer[5].
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-08-2017, 04:42 PM
  2. how to pass ./configure parameters to header files
    By fadey in forum C Programming
    Replies: 1
    Last Post: 08-06-2007, 11:17 AM
  3. Can you pass a dll url parameters?
    By jamez05 in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2006, 01:59 PM
  4. Need to pass parameters to system()
    By roginmn in forum C Programming
    Replies: 13
    Last Post: 04-27-2004, 04:30 PM
  5. Ask about function parameters and pass a reference.
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 11-04-2002, 12:14 PM

Tags for this Thread