Thread: passing array to function

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    90

    passing array to function

    I want to understand how does array get pass by its value

    If I know the length of the array, can I pass its content to the function as I do in my program


    Code:
    #include<stdio.h>
    
    void Fun (int content[], int n );
    
    
    int main ()
    {
    	int array[6] = {6, 5, 4, 3, 2, 1};
    	
    	Fun (array, 6);
    	
    }
    
    
    void Fun (int content[], int n );
    
    
    {
    	
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, but you should still be aware that it is not the array itself that is passed, but rather the array is converted to a pointer to its first element, hence for that function call, the parameter named content is a pointer to the first element of the array named array in the main function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    90
    Quote Originally Posted by laserlight View Post
    Yes, but you should still be aware that it is not the array itself that is passed, but rather the array is converted to a pointer to its first element, hence for that function call, the parameter named content is a pointer to the first element of the array named array in the main function.
    The program is not working as it should supposed to do

    Code:
    #include<stdio.h> 
    void Fun (int content[], int n );
     
    int main ()
    {
    	int n = 6;
        int array[6] = {6, 5, 4, 3, 2, 1};
         
        Fun (array, n);
         
    }
     
    void Fun (int content[], int n )
     
    {
         for (int  i = 0; i < n; i++)
    	 {
    		 printf(" % d ", content);
    	 }
    }
    output : 6422292 6422292 6422292 6422292 6422292 6422292

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Try printf(" % d ", content[i]);

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > The program is not working as it should supposed to do
    Code:
    $ cat foo.c
    #include<stdio.h>
    void Fun (int content[], int n );
    
    int main ()
    {
    	int n = 6;
        int array[6] = {6, 5, 4, 3, 2, 1};
    
        Fun (array, n);
    
    }
    
    void Fun (int content[], int n )
    
    {
         for (int  i = 0; i < n; i++)
    	 {
    		 printf(" % d ", content);
    	 }
    }
    $ gcc foo.c
    foo.c: In function ‘Fun’:
    foo.c:18:11: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
        printf(" % d ", content);
               ^
    If you're not seeing warnings like this, then get a better compiler.

    If you are seeing warnings like this, then pay attention to them.

    If you don't understand a warning, then post the code and the warning.
    Don't just run the code anyway and complain that it doesn't work.
    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.

  6. #6
    Registered User
    Join Date
    Nov 2019
    Posts
    90
    Hodor
    Try printf(" % d ", content[i]);

    It's work



    Quote Originally Posted by Salem View Post
    If you're not seeing warnings like this, then get a better compiler.

    If you are seeing warnings like this, then pay attention to them.

    If you don't understand a warning, then post the code and the warning.
    Don't just run the code anyway and complain that it doesn't work.
    I use MinGW is a port of the free GCC compiler to Windows My compiler is not giving any warning.

    There are so many compiler available on internet so will you suggest any good compiler

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Player777 View Post
    Hodor
    Try printf(" % d ", content[i]);

    It's work



    I use MinGW is a port of the free GCC compiler to Windows My compiler is not giving any warning.

    There are so many compiler available on internet so will you suggest any good compiler
    It should have been given a warning. How are you compiling your programs (e.g. from the command line or using and IDE)?

  8. #8
    Registered User
    Join Date
    Nov 2019
    Posts
    90
    Quote Originally Posted by Hodor View Post
    It should have been given a warning. How are you compiling your programs (e.g. from the command line or using and IDE)?
    command gcc -o test test.c
    test

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    command gcc -Wall -Wextra -o test test.c
    would be a good place to start.


    command gcc --version
    would tell us just how old your compiler is.
    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.

  10. #10
    Registered User
    Join Date
    Nov 2019
    Posts
    90
    Quote Originally Posted by Salem View Post
    command gcc -Wall -Wextra -o test test.c
    would be a good place to start.


    command gcc --version
    would tell us just how old your compiler is.
    gcc --version
    gcc (MinGW.org GCC-6.3.0-1) 6.3.0
    Copyright (C) 2016 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    >gcc -Wall -Wextra -o test test.c
    warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
    printf(" % d ", content);

  11. #11
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Player777 View Post
    gcc --version
    gcc (MinGW.org GCC-6.3.0-1) 6.3.0
    Copyright (C) 2016 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    >gcc -Wall -Wextra -o test test.c
    warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
    printf(" % d ", content);
    Ok, well now you know how to turn warnings on This might be controversial but when I was at uni if we submitted a program that had a single warning we'd get a zero. Harsh? Not really in my opinion. You should be able to get your program to compile without warnings. So turn the warnings into errors

    gcc -Wall -Wextra -Werror -o test test.c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing array to another function
    By RyanC in forum C Programming
    Replies: 2
    Last Post: 12-28-2018, 10:36 AM
  2. Passing 2-D array to a Function
    By Shankar k in forum C Programming
    Replies: 6
    Last Post: 04-25-2015, 11:25 AM
  3. Replies: 4
    Last Post: 09-25-2012, 01:31 AM
  4. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 01:32 PM
  5. passing array to function
    By chr1zis in forum C++ Programming
    Replies: 2
    Last Post: 02-20-2006, 10:39 PM

Tags for this Thread