Reputation:
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
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:
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
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