'450DSA: Union of two arrays by pointer

/**

  • @brief Union of two arrays
  • EX: Given two arrays a[] and b[] of size n and m respectively. The task is to find union between these two arrays. Union of the two arrays can be defined as the set containing distinct elements from both the arrays. If there are repetitions, then only one occurrence of element should be printed in the union.

Example 1:

Input: 5 3 1 2 3 4 5 1 2 3 Output: 5 Explanation: 1, 2, 3, 4 and 5 are the elements which comes in the union set of both arrays. So count is 5. */

#include<stdio.h>

int CountUnion(int a[], int n, int b[], int m){
    int CountDif = 0;       
    int* pDif= &CountDif;
    int Count = 0;
    int* pCount = &Count;

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

        for(int j=0; j<m; j++){
            printf("b[%d] = %d\n",j, b[j]);
            if (a[i] == b[j]){
                *pDif = *pDif + 1;
            }            
        }

        printf("countdif = %d\n", *pDif);
        if(*pDif == 0){
            *pCount++;
        }
        printf("count = %d\n", *pCount);
        *pDif = 0;
        printf("countdif = %d\n", *pDif);
        putchar('\n');
    }

    int sum = m + *pCount;
    printf("sum = %d\n", sum);
    return sum;
}

int main(){

    putchar('\n');
    int a[]={1, 2, 3, 4, 5};
    int n = sizeof(a)/sizeof(a[0]);
    int b[]={1, 6};
    int m = sizeof(b)/sizeof(b[0]);
    printf("union = %d", CountUnion(a, n, b, m)); 

    return 0;
}

but my output has error so i use some printf function to check the code, and it is:

a[0] = 1
b[0] = 1
b[1] = 6
countdif = 1
count = 0
countdif = 0

a[1] = 2
b[0] = 1
b[1] = 6
countdif = 0
count = 1
countdif = 0

a[2] = 3
b[0] = 1
b[1] = 6
countdif = 0
count = 2
countdif = 0

a[3] = 4
b[0] = 1
b[1] = 6
countdif = 0
count = 21862
countdif = 0

a[4] = 5
b[0] = 1
b[1] = 6
countdif = 0
count = 1355498792
countdif = 0

sum = 1355498794
union = 1355498794

in the a[0], a[1], a[2] the code work right but in the a[3] the 'count' has dump value. i dont know why it do this. I has use some pointer to change the value of CountDif and Count. Can you give me some reason for this code work. Thank you!. ps: sorry for my bad english



Solution 1:[1]

You initialize pCount to be this:

int *pCount = &Count;

Later in-code you do this:

*pCount++;

That isn't doing what you think it is. That reaps the current value of *pCount, then increments the pointer, not the dereferenced content. If you turn up your warning levels your compiler should warn you that the expression result of *pCount++ is unused, and it would be accurate. It is similar, but not entirely identical, to doing this:

*pCount;
++pCount;

This not only does not do what you want, it advances the pointer pCount outside of its addressable object memory. Any dereference thereafter invokes undefined behavior.

To fix this, ensure the increment operator sticks to the value; not the pointer, that you want to increment. In your specific context there are a couple of ways you can do that. The easiest to understand would be

(*pCount)++;

Alternatively, though it does something different, you could also pre-increment directly without the parens.

++*pCount;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 WhozCraig