Printing Arrays in separate Function in C

35,271

Solution 1

Pass a second argument to your function that takes the length of the array. For example:

print_array(int *array, int length)
{
    for (int i = 0; i < length; i++) { /* whatever */ }
}

Solution 2

The function has no way of knowing when the array ends. This piece of data simply does not exist unless you pass it manually. The array is just a sequence of bytes in the memory, it has no end delimiter. So you should add a parameter to the function telling it the length of the array.

Yep, this is how it works in C.

Solution 3

Change the function to:

void print_array(int a[], size_t a_size) {
    int i;
    for(i=0; i< a_size;i++)
    // ...

And change the calling of the function to pass in the size:

    print_array(second, sizeof(second)/sizeof(second[0]));

Which will calculate the memory size of the array (for a 4 int array on a 32 bit system it'll be 16) and divide it by the size of an int (on a 32 bit system, it's 4 bytes).

Solution 4

in C you can make it with a function and macro:

void printArray_(int *a, int len) {
    for (int i = 0; i < len; i++) printf("%d ", a[i]);
}

#define printArray(arr) printArray_((arr), sizeof(arr)/sizeof(arr[0]))

int main(int argc, _TCHAR* argv[])
{   
    int data[] = { 1,2,3,4 };
    printArray(data);
    return 0;
}

output:

1 2 3 4
Share:
35,271
Mike
Author by

Mike

Updated on July 20, 2022

Comments

  • Mike
    Mike almost 2 years

    I'm trying to print all of the values in the four arrays by sending them to a separate function. But, the problem is I can't get the function to print the all of the integers in the array because I'm not sure what I could set the condition statement in the for loop to, that would be universal to any array of any size.

    Right now the function only prints the first 11 numbers. I assume that's because the first number in that array is 11.

        #include <stdio.h>
    
        void print_array(int a[]);
    
        void find_max(int b[]);
    
        void find_min(int c[]);
    
        void search(int d[]);
    
        void SORT(int e[]);
    
        int main(void)
        {
            int first[11] = {7,7,7,7,7,7,7,7,7,7,7};
    
            int second[14] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2};
    
            int third[16] = {-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
    
            int fourth[23] = {-3, 4, 33, 22, 9, -100, 2, 56, 57, 55, 2, 90, 2234, 32, 8, 123, 2, 33, 22, 22, 33, -1, -3}; 
    
    
            print_array(&second[0]);
    
    
            return(0);
        }
    
        void print_array(int a[])
        {
    
            int i;
            for(i=0;i<*a;i++)
            {
                printf("%d ",a[i]);
    
            }
        }
    
  • Mike
    Mike about 13 years
    anyway i can assign the value in the brackets[] of the array to a variable?
  • Ilya Kogan
    Ilya Kogan about 13 years
    @Mike Exactly, you should make your program aware of this value somehow, for example by assigning it to a variable.
  • Cloud
    Cloud over 6 years
    If you call this within a function that receives a pointer-to-char argument, it will fail, as the sizeof(arr) call will return sizeof(char *) instead, due to "array decay".