简体   繁体   English

二维阵列的“ C”分割错误

[英]'C' Segmentation fault with 2d array

Can anybody explain me why this code doesn't work? 谁能解释这个代码为什么不起作用?

#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);
}

IDE(Code::Blocks) writes "Program received signal SIGSEGV. Segmentation fault." IDE(Code :: Blocks)写入“程序接收到的信号SIGSEGV。分段错误。”

First you need a 2d array of characters, not an array of pointers to characters. 首先,您需要一个二维字符数组, 而不是一个指向字符的指针数组。

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 have to change the function to accept that type. 然后,您必须更改函数以接受该类型。

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

The rest looks ok. 其余的看起来还可以。

Trying with parameters: 尝试使用参数:

findAndPrint(dayTab, 2014, 10, 12);

Gives us day: 285 给我们一天: 285

Which is correct, yaaay! 没错,是的!

If I properly understood your intent, you wanted those nested {31, 28, ... } sequences to serve as char[] arrays, to which the pointers in the upper level array would point to. 如果我正确理解了您的意图,则希望将嵌套的{31, 28, ... }序列用作char[]数组,上​​层数组中的指针将指向这些数组。

Despite what the other answer(s) states, it is not correct to say that you necessarily need a literal 2D array (even though in this case a 2D array might be a better idea than what you attempted to do). 尽管有其他答案,但不能肯定地说您确实需要一个2D数组(即使在这种情况下2D数组可能比您尝试做的更好)。 You original attempt will work too, as long as you use the proper syntax. 只要使用正确的语法,您的原始尝试也将起作用。

Now, you can't just plant a {31, 28, ... } sequence in the middle of the code and expect the compiler to interpret it as an array. 现在,您不能只在代码中间植入{31, 28, ... }序列,并期望编译器将其解释为数组。 The language does not have such feature, but it has a similar one with slightly different syntax. 该语言没有这种功能,但是具有相似的语法,但语法略有不同。 The only way achieve the proper initialization of your char *dayTab[] array in that "inline" fashion is to use the compound literal feature. 以“内联”方式实现char *dayTab[]数组正确初始化的唯一方法是使用复合文字功能。 The initialization will look as follows 初始化如下

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 }
};

Note the extra (char []) syntax. 注意额外的(char [])语法。 This is absolutely necessary. 这是绝对必要的。 This is the only thing that you have to change in your original code to make it compile as intended. 这是您唯一需要更改的原始代码,以使其按预期进行编译。

What you currently have in your original code is not valid C. If some compiler accepted this code (GCC in CodeBlocks?), then only due to some compiler extension. 您当前在原始代码中拥有的内容无效C。如果某些编译器接受了此代码(CodeBlocks中的GCC?),则仅是由于某些编译器扩展。 That compiler extension happened to play a cruel joke on you in this particular case. 在这种特殊情况下,该编译器扩展碰巧对您造成了残酷的笑话。 I don't even know how it was interpreted by the compiler, but definitely not how you intended it to be interpreted. 我什至不知道编译器如何解释它,但是绝对不知道您打算如何解释它。

PS In my experiments, GCC gave a wall of diagnostic messages in response to our original code. PS在我的实验中,GCC响应了我们的原始代码,给出了一堆诊断消息。 Did you get those messages from your compiler? 您是否从编译器获得了这些消息? Did you just ignore them? 您只是忽略了他们吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM