Как также сказано в комментариях и другом ответе, 2 группы
int *a;
int *p;
p = a;
int *b;
int *pb;
pb = b;
являются проблемными , потому что вы просто копируете не инициализированные значения, в то время как вам нужно выделить память, и scanf запись в неизвестные адреса
Предложение:
#include <stdio.h>
#include <stdlib.h>
/* fill an array and return it or NULL on error, update sz if ok */
int * init(const char * nth, size_t * sz)
{
printf("Enter the length of the %s array: ", nth);
if ((scanf("%d", sz) != 1) || ((*sz) < 1)) {
puts("invalid size");
return NULL;
}
int * r = malloc((*sz) * sizeof(int));
if (r == 0) {
puts("not enough memory");
return NULL;
}
printf("Enter %d numbers: ", *sz);
int * p;
for (p = r; p != r + (*sz); ++p) {
if (scanf("%d", p) != 1) {
puts("invalid value");
free(r);
return NULL;
}
}
return r;
}
/* return 1 if v not in p, else 0 */
int absent(int v, int * p, size_t sz)
{
int * sup = p + sz;
while (p != sup)
if (*p++ == v)
return 0;
return 1;
}
/* fill c and return number of values */
size_t find_elements(int *a, int sza, int *b, int szb, int * c)
{
size_t szc = 0;
int * pc = c;
int * p, * sup;
for (p = a, sup = a + sza; p != sup; ++p) {
if (absent(*p, b, szb) && absent(*p, c, szc)) {
*pc++ = *p;
szc += 1;
}
}
for (p = b, sup = b + szb; p != sup; ++p) {
if (absent(*p, a, szb) && absent(*p, c, szc)) {
*pc++ = *p;
szc += 1;
}
}
return szc;
}
int main()
{
size_t sza;
int * a = init("first", &sza);
if (a == NULL)
return -1;
size_t szb;
int * b = init("second", &szb);
if (b == NULL)
return -1;
/* allocate with the worst case */
int * c = malloc((sza + szb) * sizeof(int));
if (c == 0) {
puts("not enough memory");
return -1;
}
size_t szc = find_elements(a, sza, b, szb, c);
/* it is possible to use realloc to decrease the allocation size of c */
for (int * p = c; p != c + szc; ++p)
printf("%d ", *p);
putchar('\n');
free(a);
free(b);
free(c);
}
Компиляция и выполнение
pi@raspberrypi:/tmp $ gcc -pedantic -Wextra a.c
pi@raspberrypi:/tmp $ ./a.out
Enter the length of the first array: 3
Enter 3 numbers: 1 2 3
Enter the length of the second array: 4
Enter 4 numbers: 3 2 6 7
1 6 7
Под valgrind :
pi@raspberrypi:/tmp $ valgrind ./a.out
==5346== Memcheck, a memory error detector
==5346== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5346== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==5346== Command: ./a.out
==5346==
Enter the length of the first array: 3
Enter 3 numbers: 1 2 3
Enter the length of the second array: 4
Enter 4 numbers: 3 2 6 7
==5346== Invalid read of size 4
==5346== at 0x106C8: absent (in /tmp/a.out)
==5346== Address 0x49d1894 is 0 bytes after a block of size 12 alloc'd
==5346== at 0x4847568: malloc (vg_replace_malloc.c:299)
==5346== by 0x105C3: init (in /tmp/a.out)
==5346==
1 6 7
==5346==
==5346== HEAP SUMMARY:
==5346== in use at exit: 0 bytes in 0 blocks
==5346== total heap usage: 5 allocs, 5 frees, 2,104 bytes allocated
==5346==
==5346== All heap blocks were freed -- no leaks are possible
==5346==
==5346== For counts of detected and suppressed errors, rerun with: -v
==5346== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 6 from 3)