比较两个整数数组a和b的元素,并将a或b中但不同时在a和b中的元素存储在数组c中的程序
我正在从事的项目是编写一个程序,比较两个整数数组a和b的元素,并将a或b中的元素存储在数组c中,但不同时存储在a和b中。 例如,数组a包含元素{1,2,3},数组b包含元素{3,2,6,7}。数组c应该包含{1,6,7} 函数应该使用指针算法(而不是下标)来访问数组元素。换句话说,消除循环索引变量和函数中所有[]运算符的使用 现在我被用户输入卡住了,但我也不知道如何做逻辑比较两个整数数组a和b的元素,并将a或b中但不同时在a和b中的元素存储在数组c中的程序,c,C,我正在从事的项目是编写一个程序,比较两个整数数组a和b的元素,并将a或b中的元素存储在数组c中,但不同时存储在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 gu
/* 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;
}
在回答您的问题之前,欢迎来到SO。您的代码非常不完整,问题太广泛,好像您希望我们编写代码,而且该语言有许多灾难性的用法,这表明您缺少许多有关该语言的关键概念。我建议您在开始使用C语言之前,先将这些关键概念和C语言的基础知识结合起来。无论如何,我会尽力帮助你解决代码中的一些问题 你做错的第一件事就是在这里
int *a;
int *p;
p = a;
在spesific程序中,没有必要将未初始化的指针指定给另一个指针
第二件事是,您并没有为这些指针a和p分配任何内存,但您正在尝试将用户输入存储到这些指针中
printf("Enter %d numbers: ", n1);
for(p = a; p < a + n1; p++){
scanf("%d", p);
}
现在,您有了一个名为a的指针,并分配了一个大小为n1的内存块,它能够存储整数值。您可以使用它初始化另一个指针或在其中存储新的整数值,除非它超过了您以前分配的内存块大小。正如备注中所述,另一个回答两组问题
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);
}
在valgrind下:
您没有为阵列a和b分配任何内存。您可能需要类似于a=mallocn1*sizeof*a的内容。除此之外:到底什么不起作用,或者从哪里得到与预期不同的结果?程序在printfasdf之前停止;并且不执行所有的主功能。这是我目前的问题。除此之外,我不知道find_elements函数从何处开始。你应该1打开编译器警告,2找到一本关于C的好书。你应该对问题进行澄清,而不是写评论。您的程序可能会崩溃,因为您没有为a和b分配内存。你应该在调试器中运行你的程序,看看发生了什么。我正在写一个类似的答案,并开始定义一个实现方案,我让你做。。。如果你同意-这个问题非常广泛,因此还有很多问题需要回答,但我认为鼓励新用户提出广泛的问题是不好的。这就是为什么我只回答代码中有问题的部分。无论如何,请随意提出你自己的答案:谢谢大家的反馈。我承认我对Stack Overflow和C语言还比较陌生。我一直在阅读和观看指针上的视频,但它们仍然让我有点困惑。如果我的问题太宽泛,我很抱歉,下次我会尽量把范围缩小到一个特定的点。不客气。如果你喜欢我的答案,你可以把它标记为这个问题的答案。。祝你好运:
int *a;
int *p;
p = a;
int *b;
int *pb;
pb = b;
#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
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)