使用malloc为数组插入一个值。

时间:2021-02-01 01:39:30

I used this code to insert values for array data, but when I tried inserting the values 8 1 2 3 4 5 6 7 8(the first number 8 is the size of the array), the output was 00000000 instead of the input values 1 2 3 4 5 6 7 8. Any idea how I can make the program work?

我用这段代码插入值数组数据,但是当我试着插入的值8 1 2 3 4 5 6 7 8(第一个数字8是数组的大小),输出是00000000而不是输入值1 2 3 4 5 6 7 8。你知道我怎样才能让这个程序正常工作吗?

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

int main()
{
  int n,i,*data;

  scanf("%d", &n);

  data=(int *)malloc(sizeof(int)*n);//data[size]

  for(i=0;i<n;i++)
  {
     scanf("%d", &data[i]);
  }

  for(i=0;i<=n;i++)
     printf("%d",data[n]);
  printf("\n");

  return 0;
}

2 个解决方案

#1


2  

  1. The print loop should use i as index and not n as yours
  2. 打印循环应该使用i作为索引,而不是n作为您的索引
  3. The loop have to go up to n-1, so correct condition have to be i<n. Your code access the "array" out of bounds which invokes Undefined Behavior
  4. 循环必须向上到n-1,所以正确的条件是i 。您的代码访问“数组”超出了调用未定义行为的边界。
  5. You always have to check function return values.
  6. 你总是要检查函数的返回值。
  7. Side note: with you shouldn't cast malloc return.
  8. 附注:使用c时,不应使用malloc返回。

Code

代码

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

int main()
{
    size_t n,i;
    int *data;

    printf("Insert number of items: ");
    scanf("%zu", &n);

    data=malloc(sizeof(int)*n);

    if (data != NULL)
    {
        for(i=0;i<n;i++)
        {
            printf("Insert value for item %zu: ", i+1);
            scanf("%d", &data[i]);
        }

        printf("You inserted: ");

        for(i=0;i<n;i++)
            printf("%d ",data[i]);
    }
    else
    {
        fprintf(stderr, "Failed allocating memory\n");
    }

    printf("\n");

    return 0;
}

#2


2  

You need to change this part :

你需要改变这部分:

for(i=0;i<=n;i++)
    printf("%d",data[n]);
    printf("\n");
    return 0;
}

to :

:

for(i = 0; i < n; i++)
    printf("%d",data[i]);
    printf("\n");
    return 0;
}

What you do now is iterating but not using the variable i as index. Instead, you constantly try to print only data[n]. This is accessing index out of bounds as in , an array's indexes start from 0 and reach n-1, where n is the array's size. This can cause [tag: undefined-behavior] of your program.

现在要做的是迭代,而不是使用变量i作为索引。相反,您总是尝试只打印数据[n]。这是访问超出范围的索引,如在c中,数组的索引从0开始到n-1,其中n是数组的大小。这会导致程序的[tag: undefined-behavior]。

So your for loop will either have to be :

所以你的for循环必须是:

for(i = 0; i < n; i++)

or :

或者:

for(i = 0; i <= n-1; i++)

Also, take a look on this link on why you should not cast the result of malloc. Moreover, make sure that you always check the result of malloc, like this :

另外,请查看这个链接,看看为什么不应该使用malloc的结果。此外,请确保始终检查malloc的结果,如下所示:

data=malloc(sizeof(int)*n);
if (data == NULL)
    printf("Error in dynamically allocating memory.\n");

#1


2  

  1. The print loop should use i as index and not n as yours
  2. 打印循环应该使用i作为索引,而不是n作为您的索引
  3. The loop have to go up to n-1, so correct condition have to be i<n. Your code access the "array" out of bounds which invokes Undefined Behavior
  4. 循环必须向上到n-1,所以正确的条件是i 。您的代码访问“数组”超出了调用未定义行为的边界。
  5. You always have to check function return values.
  6. 你总是要检查函数的返回值。
  7. Side note: with you shouldn't cast malloc return.
  8. 附注:使用c时,不应使用malloc返回。

Code

代码

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

int main()
{
    size_t n,i;
    int *data;

    printf("Insert number of items: ");
    scanf("%zu", &n);

    data=malloc(sizeof(int)*n);

    if (data != NULL)
    {
        for(i=0;i<n;i++)
        {
            printf("Insert value for item %zu: ", i+1);
            scanf("%d", &data[i]);
        }

        printf("You inserted: ");

        for(i=0;i<n;i++)
            printf("%d ",data[i]);
    }
    else
    {
        fprintf(stderr, "Failed allocating memory\n");
    }

    printf("\n");

    return 0;
}

#2


2  

You need to change this part :

你需要改变这部分:

for(i=0;i<=n;i++)
    printf("%d",data[n]);
    printf("\n");
    return 0;
}

to :

:

for(i = 0; i < n; i++)
    printf("%d",data[i]);
    printf("\n");
    return 0;
}

What you do now is iterating but not using the variable i as index. Instead, you constantly try to print only data[n]. This is accessing index out of bounds as in , an array's indexes start from 0 and reach n-1, where n is the array's size. This can cause [tag: undefined-behavior] of your program.

现在要做的是迭代,而不是使用变量i作为索引。相反,您总是尝试只打印数据[n]。这是访问超出范围的索引,如在c中,数组的索引从0开始到n-1,其中n是数组的大小。这会导致程序的[tag: undefined-behavior]。

So your for loop will either have to be :

所以你的for循环必须是:

for(i = 0; i < n; i++)

or :

或者:

for(i = 0; i <= n-1; i++)

Also, take a look on this link on why you should not cast the result of malloc. Moreover, make sure that you always check the result of malloc, like this :

另外,请查看这个链接,看看为什么不应该使用malloc的结果。此外,请确保始终检查malloc的结果,如下所示:

data=malloc(sizeof(int)*n);
if (data == NULL)
    printf("Error in dynamically allocating memory.\n");