C语言 我有一个函数可以给予两个数组中的数字

0x6upsns  于 2023-03-17  发布在  其他
关注(0)|答案(1)|浏览(132)

我有一个函数,它给予了两个数组中的数字,但是第二个数组没有包含所有给定的数字,你知道这是怎么回事吗?
我试试这个

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

ttygqcqt1#

你知道发生了什么吗?

缺少分配

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

相关问题