user9953286
user9953286

Reputation:

I passed the wrong length of an array to a function. Why do I not get an error?

I am a beginner and I am learning c for around 20 days. I had been doing this using Youtube. I came across a video in which I was told that if an array is passed to a function then the second variable should be the length of the array. I didn't feel that was right and I tried the code given below.

#include <stdio.h>
#include <conio.h>

void name(int[], int);

int main() {
    int arr[] = {1,2,3};
    name(arr, 5);
    getch();
    return 0;
}

void name(int a[], int i) {
    printf("%d", i);
}

It didn't give me any error. So, is this correct? I mean, is the second variable really necessary for length of array?

Upvotes: 2

Views: 402

Answers (2)

chqrlie
chqrlie

Reputation: 144695

There is some confusion here: In C arrays are passed to functions as pointers to their first element. As a consequence, the function does not automatically receive any information about the number of elements of the array. If this information is required, it must be passed some other way:

  • the array size may be fixed in the specification of the function. In this case, it is up to the programmer to ensure that arrays passed to this function have the expected size at every calling point so the function can assume its array argument to have the correct size.
  • the array size may be inferred from its contents: for example C strings are arrays of char terminated by a null byte, a char with a zero value usually written '\0' (which is actually an int in C). A pointer to the first char is enough information to handle the complete string.
  • the array size may be passed as an extra argument to the function. Again, it is the programmer's responsibility to pass the actual size of the array, in accordance with the function specification. For example fgets() reads the contents of a stream into an array of char. It must be passed the maximum number of array elements it can change, which is at most the array size:

    char buf[128];
    int lineno = 1;
    while (fgets(buf, sizeof buf, stdin)) {
        printf("%d\t%s", buf);
    }
    

    Note that the array size can be computed as sizeof(array) / sizeof(*array) and sizeof(char) is 1 by definition.

Here is a modified example:

#include <stdio.h>
#include <conio.h>

void output(int *a, int length) {
    int i;
    for (i = 0; i < length; i++) {
        printf("%d\n", a[i]);
    }
}

int main() {
    int arr[] = { 1, 2, 3 };
    output(arr, sizeof(arr) / sizeof(*arr));
    getch();
    return 0;
}

Upvotes: 2

Achal
Achal

Reputation: 11921

I didn't give me any error ? No, compiler won't produce any error in this case since you are just passing the no of element value which is more than array element, But if name() function tries to access out of bound elements then it cause undefined behavior. For e.g

void name(int[], int);

int main() {
    int arr[] = {1,2,3};
    name(arr, 5);
    getch();
    return 0;
}

void name(int a[], int i) {
    printf("%d", i); /* printing variable i value is fine */
    for(int row = 0; row < i; row++) {
         printf("%d\n",a[row]);/* it cause UB when access a[3],a[4].. */

}

The correct procedure is find the no of element in array, store into one variable & pass that variable to a function, not some random number. For e.g

#include <stdio.h>
#include <stdio.h>
#include <conio.h>
void name(int[], int);
int main() {
    int arr[] = {1,2,3},ele;
    ele = sizeof(arr)/sizeof(arr[0]);
    name(arr, ele);
    getch();
    return 0;
}

void name(int a[], int ele) {
    printf("no of element in array : %d\n", ele);
    for(int row =0; row<ele; row++) {
      printf("%d\n",a[row]);
    }
}

Upvotes: 2

Related Questions