c - 程序比较两个整数数组 a 和 b 的元素,并将元素存储在数组 c 中,这些元素在 a 或 b 中,但不在 a 和 b 中
问题描述
我正在做的项目说要编写一个程序来比较两个整数数组 a 和 b 的元素,并将元素存储在数组 c 中,这些元素要么在 a 要么在 b 中,但不在 a 和 b 中。例如,数组 a 包含元素 {1, 2, 3},数组 b 包含元素 {3, 2, 6, 7}。数组 c 应包含 {1, 6, 7}。
该函数应该使用指针算法——而不是下标——来访问数组元素。换句话说,消除循环索引变量和函数中所有 [] 运算符的使用。
现在我被困在用户输入上,但我也不知道如何做逻辑。
/* This is what I have so far I'm very stuck and would love guidance.
*/
#include <stdio.h>
#include <stdlib.h>
void find_elements(int *a, int n1, int *b, int n2, int *c, int*size);
int main()
{
//User inputs length of arrays
int n1 = 0;
printf("Enter the length of the first array: ");
scanf("%d", &n1);
int *a;
int *p;
p = a;
printf("Enter %d numbers: ", n1);
for(p = a; p < a + n1; p++){
scanf("%d", p);
}
printf("asdf");
int n2 = 0;
printf("Enter the length of the second array: ");
scanf("%d", &n2);
int *b;
int *pb;
pb = b;
printf("Enter %d numbers: ", n2);
for(pb = b; pb < b + n2; pb++){
scanf("%d", pb);
}
printf("Output: \n");
}
void find_elements(int *a, int n1, int *b, int n2, int *c, int*size)
{
return;
}
解决方案
正如在评论和其他答案中所说的那样,两组
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)
推荐阅读
- c++ - 为什么 UINT32_MAX + 1 = 0?
- java - 将字符串从 onLoadFinished 方法传递到另一个活动
- mysql - 获取返回行值的百分比
- c++ - Qt 和 C++:将 QLineEdit 添加到 QTabWidget
- android-studio - Android Studio 在 Ubuntu 中启动视图解析问题
- amazon-web-services - 如何在 CloudFormation 脚本中将 AmazonEC2RoleforSSM 附加到 ec2 实例
- sqlite - 从 SQLite 表中检索所有行需要多长时间?
- azure - Kubernetes:连接到 Azure SQL
- android - 当列表的各个项目使用Retrofit和Gson的格式不同时,如何解析json列表?
- python - 如何在 SQLAlchemy 和 MySQL 中舍入一个数字?