Passing arrays to functions

This is a discussion on Passing arrays to functions within the C Programming forums, part of the General Programming Boards category; This is a simple question from a C beginner. When passing arrays as parameters to functions, do I always need ...

  1. #1
    Registered User engine's Avatar
    Join Date
    Mar 2011
    Location
    Portugal
    Posts
    3

    Passing arrays to functions

    This is a simple question from a C beginner.

    When passing arrays as parameters to functions, do I always need to pass the array's length? Or is there a way to compute the arrays length inside the function?

    Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    You have to pass the length as another parameter.
    Strictly speaking, a string is also an array. Through convention it is terminated by a '\0' character, so many of the library functions such as strlen() determine the array length by simply searching for this special terminator. I suppose you could do similar... if you had an array of ints, the terminating int might be a -1. Then inside the function you'd have a loop to search for a -1. As you can probably see, this would add computing time.

  3. #3
    Registered User engine's Avatar
    Join Date
    Mar 2011
    Location
    Portugal
    Posts
    3
    The book I am following only refers those two methods you specified. So I will take your word for it. And I will stop searching for other methods.

    Thanks.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    Quote Originally Posted by engine View Post
    This is a simple question from a C beginner.

    When passing arrays as parameters to functions, do I always need to pass the array's length? Or is there a way to compute the arrays length inside the function?

    Thanks.
    You can use "sizeof" as long as you've declared the size of the array you are passing into the function.

    Here's a very simple example. In your prototype you leave the size of the array empty, when you are in the function you create an int variable and set it equal to the sizeof the array you just passed in + 1.


    Code:
    #include <stdio.h>
    
    void input(int[]);
    void print(int[]);
    
    void main()
    {
    	int x[5];
    		
    	input(x);
    	print(x);	
    }
    
    void input(int a[])
    {
    	int length = sizeof(a) + 1;
    	int i;
    	
    	printf("Please enter 5 digits\n");
    	for(i=0; i < length; i++)
    		scanf("%d", &a[i]);
    }
    
    void print(int a[])
    {
    	int length = sizeof(a) + 1;
    	int i;
    		
    	printf("Your 5 digits are\n");
    	for(i=0; i < length; i++)
    		printf("%d\n", a[i]);
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by FairyGuy View Post
    You can use "sizeof" as long as you've declared the size of the array you are passing into the function.
    No you can't, your example only works because you lucked out.
    Quote Originally Posted by FairyGuy View Post
    Code:
    void main()
    {
    	int x[5];
    		
    	input(x);
    	print(x);	
    }
    
    void input(int a[])
    {
    	int length = sizeof(a) + 1;
    	int i;
    	
    	printf("Please enter 5 digits\n");
    	for(i=0; i < length; i++)
    You luck out here, that's the only reason it works. Change the int x[5] to int x[50] and see what's wrong.

    sizeof int = 4 in your case + 1 = 5 ... and why is it you think you need to add 1 anyway?

    You can use sizeof on an array to get its size, but only if the array is declared in the same scope that you use it:
    Code:
    int array[ 50 ];
    int x = sizeof( array ) / sizeof( int );
    printf( "array has %d members\n", x );
    You both need to read this.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Quote Originally Posted by quzah View Post
    No you can't, your example only works because you lucked out.You luck out here, that's the only reason it works. Change the int x[5] to int x[50] and see what's wrong.

    sizeof int = 4 in your case + 1 = 5 ... and why is it you think you need to add 1 anyway?

    You can use sizeof on an array to get its size, but only if the array is declared in the same scope that you use it:
    Code:
    int array[ 50 ];
    int x = sizeof( array ) / sizeof( int );
    printf( "array has %d members\n", x );
    You both need to read this.


    Quzah.
    Actually, sizeof(a) is equal to sizeof(int *) not sizeof(int). Arrays passed to functions decay to pointers.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    Quote Originally Posted by quzah View Post
    No you can't, your example only works because you lucked out.
    I see your point, tried it with a larger number.
    Quote Originally Posted by quzah View Post
    You can use sizeof on an array to get its size, but only if the array is declared in the same scope that you use it:
    Code:
    int array[ 50 ];
    int x = sizeof( array ) / sizeof( int );
    printf( "array has %d members\n", x );
    You both need to read this.
    Quzah.
    So in order to use sizeof in my example it would have to be in the main function, and like nonoob said passed as another parameter, which would be redundent if you already know the size of array you're declaring which leads me to ask is there any use in doing so?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by FairyGuy View Post
    So in order to use sizeof in my example it would have to be in the main function
    It doesn't have to be in main. It has to be declared wherever you are using sizeof on it.,
    Quote Originally Posted by FairyGuy View Post
    and like nonoob said passed as another parameter, which would be redundent if you already know the size of array you're declaring which leads me to ask is there any use in doing so?
    Good programming practices. Functions shouldn't need to rely on secret unknowns that are scattered throughout your code. They should only need to know about their parameters to do whatever they are supposed to do. Writing in the middle of your function the number 50, because you the programmer knew at the time of writing it that the array's size somewhere else was 50, is bad. If someone comes along and changes that array's 50 to 500, your function is now broken. Because it doesn't know you changed the array's 50, because its 50 is still 50.

    "But I could just search-and-replace 50 with 500!"

    Sure, if you want to do that every time you change anything. Oh, and if you want to assume that every 50 you talk about just happens to be the array size. But what if 50 is for something else? Now you've broken something else by trying to fix your array functions.

    That's why.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing 2d arrays to functions
    By Tool in forum C++ Programming
    Replies: 1
    Last Post: 07-21-2010, 10:26 PM
  2. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  4. passing arrays through functions
    By Ecko in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2003, 08:21 PM
  5. Passing multidimensional arrays to functions
    By maxthecat in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21