Question

I'm trying to pass an entire array to the function but the last value i get is always a garbage value and not 3. Please point out where I've made a mistake.

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

main() {
    int array[3] = {0, 1, 2, 3};
    display(&array[0], 3);
}
display(int *j, int n) {
    int i;

    for(i=0; i<=n; i++) {
        printf("\n%d", *j);
        j++;
    }
}
Was it helpful?

Solution

You cannot pass "an entire array to a function" -- C does not do this. Instead, you are passing a pointer to the first element of the array.

main() {
    int array[3] = {0, 1, 2, 3};

main should be declared int main(int argc, char* argv[]). It's not that horrible to type, and you can even get your text editor to do it for you, if you care enough.

You have declared array to contain only three items, but store four items into the array. This should have thrown a compile warning at the least. Pay attention to those warnings. Don't specify the size of your array unless that size is vital (Say, if you wanted to keep track of US states, then int states[50] = { ... }; might make sense. For now. Maybe this is a bad example.)

for(i=0; i<=n; i++) {
    printf("\n%d", *j);
    j++;

for(i=0; i<=n; i++) is very often a bug -- C array indexes run from 0 to n-1, not 0 to n. You're reading one-past-the-array, and that value is not surprisingly garbage.

Further this code is awkward; you should just use printf("\n%d", j[i]) and not increment j each time. Incrementing two variables as "loop variables" this way is a recipe for writing bugs in the future. (If you need to update two variables with every loop iteration, place both in the last section of the for(;;i++, j++) loop.)

OTHER TIPS

You have an off-by-one error in your for loop. Your array has only three elements. You try to read four elements instead of three.

Change your code to this (note the < instead of <=):

for (i = 0; i < n; i++) {

You also try to initialize the array with four elements when it only has room for three:

int array[3] = {0, 1, 2, 3};

Maybe you can try to change your compiler settings to flag this as a warning/error. Then you might get something like this that would notify you of the problem:

prog.c: In function ‘main’:
prog.c:13: error: excess elements in array initializer
prog.c:13: error: (near initialization for ‘array’)

The array variable represent an address of first element so no need to use & address operator with the function call and another issue is with size of an array.

main() {
    int array[4] = {0, 1, 2, 3};
    display(array, 3);
}
display(int *j, int n) {
    int i;

    for(i=0; i<=n; i++) {
        printf("\n%d", *j);
        j++;
    }
}

There a few observations in the code:
1. main() should be int main(void) or int main(int argc, char **argv). Also you need to return an int value from main when to change it to int main(void) or int main(int argc, char **argv)
2. You are initializing an array of 3 elements with 4 values.
Either declare as int array[3] = {0, 1, 2}; or as int array[] = {0, 1, 2, 3}; (this will create an array with 4 elements)
3. display(int *j, int n) should have a return type say void display(int *j, int n) (otherwise it default to int in which case the function is not returning any value). In this case you will have to define the function before using.
4. In current case, to the function display you are passing an array of 3 elements and accessing element at index 3 (when i equals n) in the for loop which is the 4th element (i.e. out of bounds which is undefined behavior). Remember that array in C is zero indexed. So for(i=0; i<=n; i++) should be for(i=0; i<n; i++)
Hope this helps!

Your array is declared to have 3 elements yet you initialize it with 4:

int array[3] = {0, 1, 2, 3};

You create an array for only 3 integers, but put 4 in it. But the error is when you print the array, you print four entries, though only three exist.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top