【问题标题】:Simple C program error简单的 C 程序错误
【发布时间】:2013-11-13 10:55:21
【问题描述】:

我在这里找不到任何错误。该程序的目的是计算数组元素及其总和的所有可能组合。我正在尝试编写一个程序,它将返回一个元素数组,其中下一个元素不等于任何前一个元素或任何先前元素组合的总和。我是这样开始的,遇到了一个错误:它说程序已经停止工作......

#include <stdio.h>
int m[20];

void initm(int x[]) {
    for(int i=0; i<20; i++) {
        m[i]=i;
    }
}

void sorter(int x[]) {
    for(int i=0; i<20; i++) {
        for(int j=0; j<20; j++) {
            /* nested for loop to get all possible combinations */
            printf("%d===%d===%d", x[i], x[j], x[i]+x[j]);  
        }
    }  
}

int main() {
    initm(m[20]);
    sorter(m[20]);
    return 0;
}

【问题讨论】:

  • "I can't find any errors here" 你看过编译器的输出信息了吗?

标签: c arrays element combinations


【解决方案1】:

m[20] 读取了一个int 超出数组末尾的一个元素,所以

initm(m[20]);
sorter(m[20]);

应该是

initm(m);
sorter(m);

【讨论】:

  • 这意味着可能存在许多被 OP 忽略的编译器警告。
猜你喜欢
  • 2012-04-18
  • 1970-01-01
  • 2012-06-09
  • 2015-05-24
  • 1970-01-01
  • 1970-01-01
  • 2013-11-12
  • 2014-08-15
  • 2023-03-23
相关资源
最近更新 更多