C segmentation fault with 2d array

Can someone explain to me why this code is not working?

#include <stdio.h>
#include <stdlib.h>

void findAndPrint(char *arr[], int year, int month, int day);

int main()
{
    char *dayTab[] = {
        {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
        {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    };
    findAndPrint(dayTab, 3, 3, 3);
    getchar();
    return 0;
}

void findAndPrint(char *arr[], int year, int month, int day ){
    int d = 0;
    if(month > 12 || month < 1 || day > 31 || day<1)
        return;
    int leap = ((year%4==0 && year%100!=0) || year%400 == 0)?1:0;
    int i;
    for(i=0; i<month-1; i++){
        d += arr[leap][i];
    }
    d+= day;
    printf("Day = %d", d);
}

      

The IDE (Code :: Blocks) writes "Software received signal SIGSEGV. Segmentation fault".

0


source to share


2 answers


First you need a 2d character array, not an array of character pointers.

char dayTab[][12] = {
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

      

Then you need to change the function to accept this type.

void findAndPrint(char arr[][12], int year, int month, int day ) ;

      

The rest looks fine.



Trying with parameters:

findAndPrint(dayTab, 2014, 10, 12);

      

Gives us a day: 285

What's right, yaaay!

+4


source


If I understood your intent correctly, you wanted these nested sequences to {31, 28, ... }

serve as arrays char[]

to be pointed to by the pointers in the top-level array.

Despite what the other answer (s) says, it is wrong to say that you absolutely need a literal 2D array (although in this case, a 2D array may be better than what you were trying to do). The original attempt will work too if you use the correct syntax.

Now, you can't just set a sequence {31, 28, ... }

in the middle of your code and expect the compiler to interpret it as an array. The language does not have this functionality, but it has a similar language with slightly different syntax. The only way to get your array initialized correctly is char *dayTab[]

that the "built-in" way is to use a composite literal function. Initialization will look like this

char *dayTab[] = {
    (char []) { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
    (char []) { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};

      



Pay attention to the additional syntax (char [])

. This is absolutely essential. This is the only thing you need to change in the source code so that it compiles as you see fit.

What you currently have in your source code is not valid. C. If some compiler accepted this code (GCC in CodeBlocks?), It is only because of some compiler extension. In this particular case, this compiler extension played a cruel trick on you. I don't even know how it was interpreted by the compiler, but definitely not the way you expected it to be interpreted.

PS In my experiments, GCC provided a wall of diagnostic messages in response to our original code. Have you received these messages from your compiler? Did you just ignore them?

+1


source







All Articles