【问题标题】:I have a function that give numbers in 2 arrays我有一个函数可以在 2 个数组中给出数字
【发布时间】:2023-02-25 05:12:50
【问题描述】:

我有一个函数,它在 2 个数组中给出数字,但第二个数组不接受所有给定的数字。任何想法发生了什么?

我试试这个

struct array
{
    int (*list)[N];
    int (*mlist)[N];
};

int input(struct array x){
    int i, j;
    printf("Give the intiger numbrs for the first array: ");
    for(i = 0; i< N; i++){
        scanf("%d", *(x.list+i));
    }

    printf("Give the intiger numbrs for the second array: ");
    for(j = 0; j< N; j++){
        scanf("%d", *(x.mlist+j));
    }
   }

int main(){
    struct array x;

    input(x);
    return 0;
}

【问题讨论】:

  • 您的结构包含两个指向数组的指针,但您从未为它们分配任何内存。
  • 我想知道是什么触发了blackgreen 删除给定的答案?
  • @chux-ReinstateMonica 啊...答案可能被确定为 ChatGPT 生成的答案。
  • @chux-ReinstateMonica 我也是。我发现有时很难发现,而且我也不会猜到这是其中之一 - 但我读到@meta,mods 已经变得非常擅长发现这些答案。我认为这需要一些练习。 :)
  • @TedLyngmo 评论清洁器通过:哔 - 哔。

标签: c


【解决方案1】:

有什么想法吗?

缺少作业

int main(){
  struct array x; // At this point x and its members are uninitialized.

之后

int input(struct array x){
  // x here is a copy of the junk passed to it from main().

  int i, j;
  printf("Give the intiger numbrs for the first array: ");
  for(i = 0; i< N; i++) {

    // x.list still has not been yet assigned.
    // So `x.list+i` is a junk calculation. 
    // Undefined behavior (UB) 
    scanf("%d", *(x.list+i));  
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    相关资源
    最近更新 更多