我正在研究的项目说要编写一个程序,该程序比较两个整数数组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;
}
答案 0 :(得分:0)
在回答您的问题之前,欢迎您来SO。您的代码非常不完整,问题过于广泛(闻起来像是您希望我们编写代码),并且该语言有许多灾难性用法,这表明您缺少有关该语言的许多关键概念。我建议您在进行相关工作之前先了解这些关键概念和C语言基础知识。无论如何,我将尝试帮助您解决代码中的一些问题...
您做错的第一件事是在这里;
int *a;
int *p;
p = a;
在特殊程序中没有必要将未初始化的指针分配给另一个指针。
第二件事是您没有为这些指针a
和p
分配任何内存,但是您试图在此处将用户输入存储到这些指针中;
printf("Enter %d numbers: ", n1);
for(p = a; p < a + n1; p++){
scanf("%d", p);
}
您在此处应用了相同的错误逻辑;
int *b;
int *pb;
pb = b;
printf("Enter %d numbers: ", n2);
for(pb = b; pb < b + n2; pb++){
scanf("%d", pb);
}
我不想为您编写代码,但是有一个改进建议可以向您提出。像这样使用它们之前,必须为指针分配内存;
int *a = (int *) malloc(n1 * sizeof(int));
现在,您有了一个名为a
的指针,并分配了一个大小为n1
的存储块,它可以存储整数值。您可以使用它来初始化另一个指针或在其中存储新的整数值,除非它超出了先前分配的内存块大小。
答案 1 :(得分:0)
在发言中也提到了,另一组回答了两组
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)