c - C 中的数组包含不正确的值

标签 c arrays pointers

该程序的目标是将数组的第一个和最后一个元素相加,并将该值设置为输出数组的第一个元素,然后继续向内移动。所有的总和都将存储在输出数组中。对于这个程序,规则规定我只能使用指针和指针算术(即没有下标,没有'[]'等)我已经让这个程序适用于长度为 2 和长度为 4 的数组(因为我只为偶数长度的数组实现了功能)但是当我尝试任何长度为 6 或以上的数组时,程序会将第一个数组中不存在的错误值加在一起。

我已经尝试使用两个不同的调试器来隔离问题的来源,但我终究无法弄清楚。我花了几个小时查看我的 C 语言笔记并检查代码,尽我所能对其进行修改。我觉得数组和指针变量之间的交互方式好像有问题,但我不确定。我似乎在 Stack Overflow 上找不到任何与这个太相似的问题(是的,我看过)。

void add(int *a1, int array_size, int *a2) {

    int * p;
    int * temp = (a1+(array_size-1));

    if (array_size % 2 == 0) {
        array_size = array_size/2;
        for (p = a2; p < (a2+array_size); p++) {
            *p = *a1 + *temp;
            printf("%d", *a1);
            printf(" + %d", *temp);
            a1++;
            temp--;
            printf(" = %d\n", *p);
        }
    }

}

对于长度为 2 和 4 的数组(同样,我现在只测试偶数),代码工作正常。

示例输出:

Enter the length of the array: 2                                                                                               

Enter the elements of the array:  1 2                                                                                          
1 + 2 = 3   
The output array is: 3

Enter the length of the array: 4                                                                                               

Enter the elements of the array:  1 2 3 4                                                                                      
1 + 4 = 5                                                                                                                      
2 + 3 = 5  
The output array is: 5 5

现在这就是问题所在。

当我这样做时:

Enter the length of the array: 6                                                                                                 

Enter the elements of the array:  1 2 3 4 5 6 

我希望:

1 + 6 = 7                                                                                                                        
2 + 5 = 7                                                                                                                        
3 + 4 = 7 

The output array is: 7 7 7

但是相反,输出是:

1 + 0 = 1                                                                                                                        
2 + 3 = 5                                                                                                                        
3 + 4 = 7          

The output array is: 1 5 7  

我最好的猜测是我对指针或指针语法的使用出了问题。我将不胜感激任何帮助,无论是积极的还是消极的。

这是 main() 函数:

int main() {

  int size = 0;
  int out_size = 0;
  int arr[size];

  printf("Enter the length of the array: ");
  scanf("%d", & size);
  printf("\nEnter the elements of the array:  ");
  for (int i = 0; i < size; i++) {
    scanf("%d", & arr[i]);
  }

  if (size % 2 == 0) {
      out_size = size/2;
  }
  else{
      out_size = ((size-1)/2) + 1;
  }

  int out[out_size];

  //calling the add function and using the addresses of arrays + array size
  add(arr, size, out);

  //iterating through and printing output array which has now been
  //altered by the move function
  printf("\nThe output array is: ");
  for (int i = 0; i < out_size; i++) {
    printf("%d ", out[i]);
  }
  return 0;
}

最佳答案

您正在使用大小为 0 的数组:

int main() {
    int size = 0;
    int out_size = 0;
    int arr[size]; // <- Here is your problem

您可以在 size 阅读之后移动数组声明:

int main() {
    int size = 0;
    int out_size = 0;

    printf("Enter the length of the array: ");
    scanf("%d", & size);
    int arr[size];

    printf("\nEnter the elements of the array:  ");
    for (int i = 0; i < size; i++) {
        scanf("%d", & arr[i]);
    }

关于c - C 中的数组包含不正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55329307/

相关文章:

c - K&R C 书关于 scanf 如何处理格式字符串中的空格和制表符的问题?

c - 在 c 中按 enter 后退出循环

c - 使用 putchar() 打印段落

android - 更改字符串数组中的项目时应用程序强制关闭,Android?

c - 使用指针从 C 中的函数调用数组元素

更改内核函数指针的地址

objective-c - OpenSSL x509 证书 : Add Extension with X509_add1_ext_i2d()

python - NumPy:一次对许多小矩阵进行点积

java - 使用 Jackson 将 Json 数组拆分为单个 Json 元素

list - 在 ocaml 中定义一棵树 + 指向其子树的指针