Passing Variable-Length Array to a Function

This is a discussion on Passing Variable-Length Array to a Function within the C Programming forums, part of the General Programming Boards category; Code: int GetChar (int NumChar, int MaxNumChar [NumChar]) { printf("Please enter desired characters.\n"); for (int i = 0; i < ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    15

    Passing Variable-Length Array to a Function

    Code:
    int GetChar (int NumChar, int MaxNumChar [NumChar])
    {
    
    	printf("Please enter desired characters.\n");
    	for (int i = 0; i < NumChar; i++)
    		scanf("%c", &MaxNumChar[i]);
    
    	return i;
    }
    I think I'm passing the array to the function wrong. What's the right way to do this and how do I return the characters read in this function back to main?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Are you really passing in a VLA to your function?

    And even if you are, you don't have to specify the size of an array that is passed into a function, VLA or no VLA. So don't.

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    int GetChar (int NumChar, int MaxNumChar [NumChar])
    An entire array is never passed into a function, only its address. So you can either pass that as a pointer or declare the function like this -

    Code:
    int GetChar (int NumChar, int MaxNumChar [])
    which is pretty much the same as passing in an int *.

    To pass the array, all you have to do is -

    Code:
    int myArray[4];
    int GetChar(4,myArray);
    referring to the name of an array without using square brackets returns the address of the array or better the address of the first element, which is equivalent to &myArry[0].
    how do I return the characters read in this function back to main?
    You cant return an array from a function either. Only its pointer. However, since arrays are passed using a pointer whatever changes you make inside the function will be made to the original array as well.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    15
    Quote Originally Posted by Spidey View Post
    An entire array is never passed into a function, only its address. So you can either pass that as a pointer or declare the function like this -

    Code:
    int GetChar (int NumChar, int MaxNumChar [])
    which is pretty much the same as passing in an int *.

    To pass the array, all you have to do is -

    Code:
    int myArray[4];
    int GetChar(4,myArray);
    referring to the name of an array without using square brackets returns the address of the array or better the address of the first element, which is equivalent to &myArry[0].


    You cant return an array from a function either. Only its pointer. However, since arrays are passed using a pointer whatever changes you make inside the function will be made to the original array as well.
    Ok, that makes sense. I'm getting a syntax error where I declare the function though.

    Code:
    Array = GetChar (NumChar, MaxNumChar []);

  5. #5
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Ok, that makes sense. I'm getting a syntax error where I declare the function though.

    Code:
    Array = GetChar (NumChar, MaxNumChar []);
    What exactly are you trying to do here ? Thats not how you declare a function, its done like this -
    Code:
    int GetChar (int NumChar, int MaxNumChar []);
    However, If your trying to assign the value to Array and if Array is an array then you cant assign it the value returned from GetChar in its current form as it returns an int.
    Also, you call the function like this -

    Code:
    GetChar(4,Array);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Passing a byte and its bits to a function
    By rtarbell in forum C Programming
    Replies: 9
    Last Post: 12-04-2008, 09:24 AM
  3. Problems passing an array to a function
    By ndenklau in forum C Programming
    Replies: 5
    Last Post: 09-20-2006, 09:14 AM
  4. Passing a character array to a function
    By Qchem in forum C Programming
    Replies: 3
    Last Post: 03-07-2005, 07:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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