Thread: How to pass array to function by reference

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

    How to pass array to function by reference

    I have one array with value I want to pass the content of array to function by reference (pointer). I do not understand how to do that

    Code:
    #include <stdio.h>
    
    #define  Y    '0' 
    #define  N    '1'
    
    
    #define  A    '0' 
    
    
    char MyFunction(char text, int i, char *buffer);  /* Prototype */
    
    
    char MyFunction( char text )  /* Called function definition */
    {
      if (text == A)
      {
        //store content here//
        printf("success \n");  
        return Y;
      }
      else
      {
          printf("Failure \n");  
        return N;
      }
    }
    
    
    int main( void )  /* Calling function */
    {
      int size = 10;
      
      char name[size] = {'L','e','s','s','o','n','s','\0'};
      
      char function =     MyFunction(A, size, name);
      
      printf("%c\n", function);  
      
      return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    char MyFunction(char text, int i, char *buffer);  /* Prototype */
     
     
    char MyFunction( char text )  /* Called function definition */
    Lying to the compiler ends badly!

    You said MyFunction has 3 parameters and then then you used only one in the function body.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    81
    Quote Originally Posted by stahta01 View Post
    Code:
    char MyFunction(char text, int i, char *buffer);  /* Prototype */
     
     
    char MyFunction( char text )  /* Called function definition */
    Lying to the compiler ends badly!

    You said MyFunction has 3 parameters and then then you used only one in the function body.

    Tim S.
    my typing mistake

    Code:
    #include <stdio.h> 
    #define  Y    '0' 
    #define  N    '1'
     
     
    #define  A    '0' 
     
     
    char MyFunction(char text, int i, char *buffer);  /* Prototype */
     
     
    char MyFunction(char text, int i, char *buffer);
    {
      if (text == A)
      {
        //store content here//
        printf("success \n");  
        return Y;
      }
      else
      {
          printf("Failure \n");  
        return N;
      }
    }
     
     
    int main( void )  /* Calling function */
    {
      int size = 10;
       
      char name[size] = {'L','e','s','s','o','n','s','\0'};
       
      char function =     MyFunction(A, size, name);
       
      printf("%c\n", function);  
       
      return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest you try to compile your code.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass array by reference help
    By Trey Brumley in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2014, 11:47 PM
  2. pass by reference by inner function in C
    By shanwu in forum C Programming
    Replies: 6
    Last Post: 06-05-2012, 10:02 PM
  3. how can i pass 2d and 3d array by reference?
    By johnny8888 in forum C++ Programming
    Replies: 9
    Last Post: 10-23-2011, 10:04 AM
  4. how to pass array by reference
    By dezz101 in forum C Programming
    Replies: 13
    Last Post: 09-19-2010, 09:28 AM
  5. Pass an array to a function by reference?
    By Loic in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2007, 11:44 PM

Tags for this Thread