psrag anvesh
psrag anvesh

Reputation: 1275

The correct way of passing an array into a function?

I am relatively new to C and can't figure out what is wrong with the code?

I am getting 2 warnings during compile time and Segmentation fault core dump error during run time. Can anyone explain why? I am running Ubuntu as a virtual machine. And is this the correct way to declare/pass an array into a function?

#include <stdio.h>

//Loop handlers 
int i, j, m, n;
int c;
int cap[26];


//Funtions prototype
void countingChars(void);
void vertcalHistogram(int [], int size);    //Warning: expected ‘int *’ but argument is of type ‘int’ (helloworld)
void dashes(void);

int main (void)
{
    countingChars();
    vertcalHistogram( cap[26], 26); //Warning: passing argument 1 of ‘vertcalHistogram’ makes pointer from integer without a cast [enabled by default] (helloworld)
    //dashes();
    getchar();
    return 0;
}

void countingChars(void)
{
    while((c = getchar()) != EOF)
    {
        if(c >= 65 && c <= 90)
            ++cap[c - 65];

        if(c >= 97 && c <= 122)
            ++cap[c - 97];

        for(i = 0; i < 26; i++)
            printf("%d", cap[i]);
        printf("\n");

    }   
}
void dashes(void)
{
    printf("\n");
    printf("\n");

    for(i = 0; i < 40; i++)
        printf("_");

    printf("\n");

    for(i = 0; i < 40; i++)
        printf("_");

}

void vertcalHistogram(int cap[], int size)
{
    for(i = 0; i < size; i++)
    {   
        printf("||");
        for(j = 0; j < cap[i]; j++)
            printf("*");
        printf(" ~~ %d", cap[i]);
        printf("\n");
    }
}

Upvotes: 0

Views: 112

Answers (4)

chux
chux

Reputation: 153358

cap[26] is 1 element passed the last element of char array cap[]. Original code is passing a char and not an array. Certainly not what is intended.

void vertcalHistogram(int [], int size); 
int cap[26];
...
vertcalHistogram( cap[26], 26);  // bad
...
void vertcalHistogram(int cap2[], int size)  // Changed name for clarity

In C, arrays are not truly passed to functions.

Detail: Instead use the following. The array cap is a formal parameter to vertcalHistogram(). C does not pass arrays, instead it converts the array to the address and type of the first element. This actual parameter is passed to the function. The function receives the address as cap2[].

vertcalHistogram( cap, 26);  // good

Upvotes: 0

Mike Nakis
Mike Nakis

Reputation: 61989

cap[26] is the 27th element of cap, and since cap[] is an array of int, the 27th element is of type int. You need to pass cap, not cap[26].


Also, you would do yourself a great favour to enable the "treat all warnings as errors" option of your compiler, so as to not even try running your program if you get warnings.

Also, try this: #define COUNTOF(x) (sizeof(x)/sizeof((x)[0])) so then you can call your function like this: vertcalHistogram( cap, COUNTOF(cap) );

Upvotes: 2

Emil Laine
Emil Laine

Reputation: 42828

cap[26] means the int at index 26 in the cap array.

If you want to pass cap, write cap:

verticalHistogram(cap, 26);

Upvotes: 1

P.P
P.P

Reputation: 121387

The correct way is to pass the address of array itself or the address of the first element:

vertcalHistogram( cap, 26);

or

vertcalHistogram( &cap[0], 26);

But it doesn't seem to be necessary since cap is a global variable in your code.

cap[26] is outside the bounds of the array. Remember C indexing starts from 0. So for an array of size 26, 0 to 25 is the valid index range.

Upvotes: 1

Related Questions