【问题标题】:Issue with using array pointers (C)使用数组指针的问题 (C)
【发布时间】:2022-10-08 01:52:07
【问题描述】:

我正在编写一个程序,该程序旨在读取两个数组并找到两者之间的差异(在集合 A 中找到但不在集合 B 中的元素)。

这些集合使用 1 和 0 的数组存储(1 表示存在的元素,0 表示不存在的元素)。我编写了以下代码,但似乎无法理解为什么会收到这些警告

warning: comparison between pointer and integer [enabled by default]
             if(p==1 && q==0)
                 ^
 warning: assignment makes pointer from integer without a cast [enabled by default]
                 set_difference = 1;

我编写了以下代码。它也不会返回值。

#define N 10

void find_set_difference(int *set_a, int *set_b, int n, int *set_difference);  

int main(void)
{
    int i, k;
    int n;

    printf("Enter the number of elements in set A: \n");
    scanf("%d", &n);
    
    int a[n];

    printf("Enter the elements in set A: \n"); 

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

    printf("Enter the number of elements in set B: \n");
    scanf("%d", &n);

    int b[n];

    printf("Enter the elements in set B: \n"); 

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

    int set_dif[N];

    find_set_difference(a, b, N, set_dif);

    printf("The difference of set A and set B is: \n");
    for(i=0;i<10;i++){
        if(set_dif[i]==1)
        printf("%d ",i);
    }

    return 0;
}

void find_set_difference(int *set_a, int *set_b, int n, int *set_difference){

    int *p, *q;

    for(p=set_a; p<set_a+n; p++){
        for(q=set_b; q<set_b+n; q++){
            if(p==1 && q==0)
                set_difference = 1;
            else 
                set_difference = 0;
        }
    }
}

任何有关格式化和使用指针的帮助都会有所帮助,因为我还是编码新手,并且难以理解这些概念。

【问题讨论】:

  • b[k]=1 将设置值 1 无论您之前输入什么。
  • set_difference 是指向数组的指针。你期待set_difference = 1; 做什么?我认为您想分配给数组的一个元素,而不是数组本身。
  • 你永远不会在&amp;a[k] 之前设置k。我想你的意思是&amp;k
  • 如果n 大于10 怎么办?为什么不对set_dif 使用与a 相同的大小?
  • 如果集合 A 中的元素数量与集合 B 不同怎么办?您只是将 b 的大小传递给函数。您需要将其置于不同的变量中。

标签: c


【解决方案1】:

下面检查指针的值:

if(p==1 && q==0)

您想检查指出的值。

if(*p==1 && *q==0)

下面设置指针的值:

set_difference = 1;

您想设置指向的变量。

*set_difference = 1;

此答案仅解决您询问的警告。还有许多其他重大问题,但我不想为你做功课。想一想您要设置多少个不同的变量。

  • 您当前正在设置一个。
  • 您当前正在设置 n*n 次。

【讨论】:

    【解决方案2】:

    你的代码中有一些问题:

    1. 变量k 的值没有在循环中被初始化,因为它被用作迭代器, a[k] = 1 也没有做任何事情,因为这是一个数组而不是哈希表,假设input1 = 1, 2, 3, 4, 5 和@987654325 @,你写那行的方式使输入1与输入2相同

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

      所以你应该这样做:

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

      1. 如果用户为 2 个不同的数组输入不同的大小,则对 2 个不同的数组使用相同的变量 n 可能会导致一些错误

      所以使用另一个变量来获取第二个数组的大小,而不是用于第一个数组的n


      1. 行中set_dif 的大小:

        int set_dif[N];
        

      最好是它们中最小数组的大小,但如果它的大小大于那个大小,它不会有任何区别。


      1. 在这一行:

        if(p==1 && q==0)
        

      您正在将指针 p 的地址与 1 的值进行比较

      因此,您应该将该地址中的值与值1 进行比较,所以您应该这样做:if(*p==1 &amp;&amp; *q==0)


      1. 在这一行:

        set_difference = 1;
        

      set_difference 是指向数组的指针,这意味着它是一个地址,所以你不能这样做地址 = 值,相反,您应该执行以下操作:

           set_difference[i] = 1;
      

      其中i 是一个迭代器


      1. 也在这一行:

        if(*p==1 && *q==0)
        

      您应该比较if(*p!=*q),以免出现第 1 点中讨论的问题 并且名为 set_dif 的数组应该用 1 初始化。


      1. 代替:

          printf("%d ",i); 
        

      写:printf("%d ",a[i]);,为了实现这一行所寻求的,你应该寻找一个叫做hash table的东西


      话虽如此,这是完整的编辑代码:

      #include <stdio.h>
      
      
      void find_set_difference(int *set_a, int *set_b, int n, int m, int *set_difference);
      
      int main(void)
      {
          int i, k = 0;
          int n, m;
      
          printf("Enter the number of elements in set A: 
      ");
          scanf(" %d", &n);
      
          int a[n];
      
          printf("Enter the elements in set A: 
      ");
      
          for(i=0, k = 0; i<n; i++, k++){
              scanf(" %d", &a[k]);
          }
      
          printf("Enter the number of elements in set B: 
      ");
          scanf("%d", &m);
      
          int b[m];
      
          printf("Enter the elements in set B: 
      ");
      
          for(i=0, k = 0; i<n; i++, k++){
              scanf("%d", &b[k]);
          }
      
          int set_dif[n];
      
          for (int j = 0; j < n; ++j) {
              set_dif[j] = 1;
          }
      
          find_set_difference(a, b, n, m, set_dif);
      
          printf("The difference of set A and set B is: 
      ");
          for(i=0; i< n ;i++){
              if(set_dif[i] != 0)
                   printf("%d 	",a[i]);
          }
      
          return 0;
      }
      
      void find_set_difference(int *set_a, int *set_b, int n, int m, int *set_difference){
      
          int *p, *q;
          int i = 0;
          for(p = set_a; p < set_a + n; p++, i++){
              for(q = set_b; q< set_b + m ; q++){
                  if(*p==*q)
                      set_difference[i] = 0;
              }
          }
      }
      

      这是一些示例输出:

      Enter the number of elements in set A:
      5
      Enter the elements in set A:
      1
      3
      4
      5
      6
      Enter the number of elements in set B:
      5
      Enter the elements in set B:
      2
      3
      4
      6
      8
      The difference of set A and set B is:
      1       5
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多