Home > Enterprise >  Issue with using array pointers (C)
Issue with using array pointers (C)

Time:10-08

I am writing a program that is meant to read in two arrays and find the difference between the two (elements found in set A but not in set B).

The sets are stored using arrays of 1s and 0s (1s for elements that exist and 0s for elements that don't). I have the following code written and can't seem to understand why I am getting these warnings

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;

I have the following code written. It will not return a value, either.

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

Any assistance with formatting and using pointers would be helpful, as I am still new to coding and am having difficulty understanding the concepts.

CodePudding user response:

The following checks the value of the pointers:

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

You want to check the pointed values.

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

The following sets the value of the pointer:

set_difference = 1;

You want to set the pointed variable.

*set_difference = 1;

This answer only addresses the warnings you asked about. There are a number of other major problems, but I don't want to do your homework for you. Think about how many different variables do you want to set.

  • You are currently setting one.
  • You are currently setting it n*n times.

CodePudding user response:

you some problems in your code:

  1. the value of variable k isn't being initialized in the loop as it's used being as an iterator, also a[k] = 1 doesn't make any since as this is an array not hash table, assume if input1 = 1, 2, 3, 4, 5 and input2 = 6, 7, 8, 9, 10, the way you write that line makes that input1 is same as input2 :

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

so you should do :

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

  1. using the same variable n for 2 different arrays can result in some errors if the user entered different sizes for the 2 different arrays

so use another variable to get the size of the second array instead of n that's being used for the first array


  1. the size of the set_dif in line :

    int set_dif[N];
    

is better to be the size of the smallest array of them, but it wouldn't make any difference if its size is greater than that.


  1. in this line :

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

you are comparing address which is pointer p with a value which is 1

so instead you should compare the value in that address with value 1, so you should do: if(*p==1 && *q==0)


  1. in this line:

    set_difference = 1;
    

set_difference is a pointer to the array which means it's an address, so you can't do address = Value, instead you should do the:

     set_difference[i] = 1;

where i is an iterator


  1. also in the line:

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

you should compare if(*p!=*q) as not to make the problem discussed in point 1 and the array called set_dif should be initialized with ones.


  1. instead of:

      printf("%d ",i); 
    

write: printf("%d ",a[i]); , as to achieve what are seeking with this line, you should look for something called hash table


with all that being said, this is the full edited code:

#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: \n");
    scanf(" %d", &n);

    int a[n];

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

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

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

    int b[m];

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

    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: \n");
    for(i=0; i< n ;i  ){
        if(set_dif[i] != 0)
             printf("%d \t",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;
        }
    }
}

and this is some example output:

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
  •  Tags:  
  • c
  • Related