【问题标题】:Passing array as argument and swapping the data - unexpected data将数组作为参数传递并交换数据 - 意外数据
【发布时间】:2015-05-10 16:19:35
【问题描述】:

当我在交换函数中传递参数时,我期待索引a[0]a[1] 的输出。但输出分别显示来自a[1]a[2] 的数据。

但是为什么呢?以及如何改进?

下面是我的代码。

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

int add(int *array,int max );
void swap(int *array,int max,int *x,int *y);

int  main()
{
    int sum,i,j=0,a[10],max;

    scanf("%d",&max);

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

    for(i=0;i<max;i++)
    {
        printf(" %d ",a[i]);
    }

    printf("\n");

    //for(i=0;i<max;i++)
    {
        swap(a,max,&a[0],&a[1]);
    }
    sum=add(a,max);  // array name is same as address of array//or sum=add(&a[0],max);
    printf("%d",sum);
}

int add(int *array,int max)
{
    int sum=0,i=0;
    while(i<max)
    {
        sum=sum +array[i];
        i++;
    }
    return sum;
}

void swap(int *a,int max, int *x,int *y)
{
    int i;
    /* int temp=0;

    temp=a[*x];
    a[*x]=a[*y];
    a[*y]=temp;*/
    printf("\n%d %d",a[*x],a[*y]);
    for(i=0;i<max;i++)
    {
        printf("\n %d",a[i]);
    }
    printf("\n");
}

【问题讨论】:

    标签: c arrays function pointers swap


    【解决方案1】:

    你的问题在下面一行

    printf("\n%d %d",a[*x],a[*y]);
    

    打电话给swap()点赞

    swap(a,max,&a[0],&a[1]);
    

    如果a[0]a[1] 的值大于10,则您访问的数组越界。请检查并更正您的逻辑。

    FWIW,

    但输出分别显示来自 a[1] 和 a[2] 的数据

    这是因为很可能a[0] 包含1a[1] 包含`2

    【讨论】:

    • 它没有越界,因为我总是访问 i
    • @deepakpandey 在询问用户输入时,如何保证a[0]==1 始终如一?
    • @deepakpandey 没什么好抱歉的朋友,只要逻辑思考。 :-)
    • 我基本上想使用指针发送 2 个数组索引到交换函数。怎么做 。并且不使用 scanf 获取数据保证用户输入的数据位于该索引处。
    • @deepakpandey 我说的是用户将输入的 。你不能确定这一点。至少,您不应该编写程序假设。 :-)
    【解决方案2】:

    我试过了,现在可以正常使用了。谢谢

    int add(int *array,int max );
    void swap(int *array,int max,int *x,int *y);
    
      int  main()
      {
        int sum,i,j=0,a[10],max;
    
         printf("enter the size \n");
    
          scanf(" %d",&max);
    
          for(i=0;i<max;i++)
        {
            scanf("%d",&a[i]);
            }
    
              for(i=0;i<max;i++)
        {
            printf(" data entered a[%d] = %d \n ",i,a[i]);
            }
    
    printf("\n");
    
    
    printf("%d",&a[1]);
    
             //for(i=0;i<max;i++)
        {
    
          swap(a,max,&a[0],&a[1]);
    
          }
          sum=add(a,max);  // array name is same as address of array//or sum=add(&a[0],max);
           printf("%d",sum);
    }
    
      int add(int *array,int max)
      {
          int sum=0,i=0;
          while(i<max)
          {
    
    
         sum=sum +array[i];
      i++;}
      return sum;
      }
    
    void swap(int *a,int max, int *x,int *y)
    {
     int i,temp;
      temp=*y;
       *y=*x;
        *x=temp;
    
        printf("\n%d %d",*x,y);
         for(i=0;i<max;i++)
        {
            printf("\n %d",a[i]);
        }
    
    printf("\n");
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-12
      • 2020-05-28
      • 2017-12-22
      • 2011-08-12
      • 2016-09-26
      • 1970-01-01
      相关资源
      最近更新 更多