C - 使用动态数组将数组添加到自身

发布于 2024-12-25 02:31:07 字数 2640 浏览 0 评论 0原文

我为此启动了一个不同的线程,我尝试使用他们给我的帮助来解决它,但我无法运行该程序。谁能告诉我程序出了什么问题以及它应该如何?谢谢。

该程序应该向自身添加一个数组,并用总和替换原始数组,因此当打印初始数组时,它会打印总和。这就是我到目前为止所做的。

请注意必须使用 ADDER(a,a) 作为函数调用。我不被允许改变这一点。两个参数都将通过引用传递。

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int size; //global variable

void ADDER(int *a, int *b) {
    int i;
    for (i = 0; i < size; i++) {
        b[i] += a[i];
    }     
}

int main() {
    int n, i;
    printf("Enter the number of elements: ");
    scanf("%d", &n);
    int *a = (int *)malloc(n*sizeof(int));
    int *b;
    for (i=0; i<n; i++) {
        printf("Enter element number %d: ", i);
        scanf("%d", &a[i]);
    }
    ADDER(a,a);
    for (i=0; i<n; i++) {
        printf("%d", a[i]);
    }
}

错误:

1>-------- 构建已开始:项目:adderTest,配置:调试 Win32 ------

1> adder.c

1>e:\my Documents\Visual Studio 2010\projects\addertest\addertest\adder.c(17): 警告 C4996: 'scanf': 此函数或变量可能不安全。考虑使用 scanf_s 代替。要禁用弃用,请使用 _CRT_SECURE_NO_WARNINGS。有关详细信息,请参阅联机帮助。

1> e:\program files\microsoft Visual Studio 10.0\vc\include\stdio.h(304) : 请参阅“scanf”的声明

1>e:\my Documents\visual studio 2010\projects\addertest\addertest\adder.c( 18): 错误 C2143: 语法错误: 缺少 ';'在“类型”

1>e:\my Documents\Visual Studio 2010\projects\addertest\addertest\adder.c(19) 之前:错误 C2143:语法错误:缺少“;”在“类型”

1>e:\我的文档\ Visual Studio 2010 \项目\ addertest \ addertest \ adder.c(22)之前:错误C2065:'a':未声明的标识符

1> e:\我的文档\ Visual Studio 2010 \ items\addertest\addertest\adder.c(22): 错误 C2109: 下标需要数组或指针类型

1>e:\my Documents\Visual Studio 2010\projects\addertest\addertest\adder.c(24): 错误 C2065: 'a' : 未声明的标识符 1>e:\my Documents\visual studio 2010\projects\addertest\addertest\adder.c(24): 警告 C4047: 'function' : 'int *' 与 'int' 的间接级别不同

1>e:\我的文档\ Visual Studio 2010 \项目\ addertest \ addertest \ adder.c(24):警告C4024: 'ADDER' :形式参数和实际参数的不同类型 1

1>e:\my Documents\visual studio 2010\projects\addertest\addertest\adder.c(24): 错误 C2065: 'a' : 未声明的标识符

1>e: \我的文档\ Visual Studio 2010 \项目\ addertest \ addertest \ adder.c(24):警告C4047: 'function' : 'int *' 的间接级别与 'int'

1>e:\my Documents\visual studio 2010\projects\addertest\addertest\adder.c(24): 警告 C4024: 'ADDER' : 形参和实参的不同类型 2

1>e:\my Documents\Visual Studio 2010\projects\addertest\addertest\adder.c(26): 错误 C2065: 'a': 未声明的标识符

1>e:\my Documents\visual studio 2010\projects\addertest\addertest\adder.c(26): 错误C2109:下标需要数组或指针类型

==========构建:0成功,1失败,0最新,0 已跳过 ==========

I started a different thread for this, I tried solving it using the help they gave me but I am not able to run the program. Can anyone tell me what is wrong in the program and how is it supposed to be? Thanks.

The program is supposed to add an array to itself and replace the original with the sum, so when the initial array is printed, it prints the sum. This is what I have done so far.

Please note it is a must to use ADDER(a,a) as a function call. I am not allowed to change that. Both parameters are to be passed by reference.

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int size; //global variable

void ADDER(int *a, int *b) {
    int i;
    for (i = 0; i < size; i++) {
        b[i] += a[i];
    }     
}

int main() {
    int n, i;
    printf("Enter the number of elements: ");
    scanf("%d", &n);
    int *a = (int *)malloc(n*sizeof(int));
    int *b;
    for (i=0; i<n; i++) {
        printf("Enter element number %d: ", i);
        scanf("%d", &a[i]);
    }
    ADDER(a,a);
    for (i=0; i<n; i++) {
        printf("%d", a[i]);
    }
}

Errors:

1>------ Build started: Project: adderTest, Configuration: Debug Win32 ------

1> adder.c

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(17): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

1> e:\program files\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(18): error C2143: syntax error : missing ';' before 'type'

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(19): error C2143: syntax error : missing ';' before 'type'

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(22): error C2065: 'a' : undeclared identifier

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(22): error C2109: subscript requires array or pointer type

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(24): error C2065: 'a' : undeclared identifier
1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(24): warning C4047: 'function' : 'int *' differs in levels of indirection from 'int'

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(24): warning C4024: 'ADDER' : different types for formal and actual parameter 1

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(24): error C2065: 'a' : undeclared identifier

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(24): warning C4047: 'function' : 'int *' differs in levels of indirection from 'int'

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(24): warning C4024: 'ADDER' : different types for formal and actual parameter 2

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(26): error C2065: 'a' : undeclared identifier

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(26): error C2109: subscript requires array or pointer type

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丶情人眼里出诗心の 2025-01-01 02:31:07

您在文件作用域和块作用域(在 main 函数中)都有一个 size 对象。

要修复您的程序,您可以删除 main 函数中 size 对象的定义,或者(首选解决方案)删除文件范围中的对象并将其作为新参数传递给您的ADDER 函数。

如果使用 gcc 进行编译,则可以使用 -Wshadow 警告,该警告会在变量遮盖另一个变量时向您发出警报。

You have a size object in both file scope and block scope (in main function).

To fix your program you can remove the definition of the size object in the main function or (preferred solution) remove the one in file scope and pass it as a new argument to your ADDER function.

If you compile with gcc you can use the -Wshadow warning that will alert you when a variable shadows another one.

橙味迷妹 2025-01-01 02:31:07

我看到的最大问题是你有 2 个 size 变量

  • 全局 size
  • 中声明

局部 sizemain ADDER 函数使用 size 的全局版本,它从未被分配,因此它的功能不正确。最简单的解决方法是避免全局,只需将大小传递给 ADDER

void ADDER(int *a, int *b, int size) {
 ...
}

The biggest problem I see is that you have 2 size variables

  • The global size
  • The local size declared in main

The ADDER function uses the global version of size which is never assigned to hence it functions incorrectly. The easiest fix is to avoid the global and just pass along the size to ADDER.

void ADDER(int *a, int *b, int size) {
 ...
}
淑女气质 2025-01-01 02:31:07
#include <stdlib.h>
#include <stdio.h>

/* global variable to store the size */
int g_size;

void ADDER(int *a, int *b) 
{
    int i;
    for (i=0 ; i<g_size ; i++) {
        b[i] += a[i];
    }

    return;     
}

int main(void) 
{
    int n, i;

    printf("Enter the number of elements: ");
    scanf("%d", &n);

    int *a = NULL;
    if ((a = (int *)malloc(n * sizeof(int))) == NULL) {
        printf("unable to allocate memory \n");
        return -1;
    }

    for (i=0; i<n; i++) {
        printf("Enter element number %d: ", i);
        scanf("%d", &a[i]);
    }

    g_size = n;
    ADDER(a, a);

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

    free(a);
    return 0;
}

我进行了以下修改:

  • malloc() 进行错误检查
  • 完成后
  • free()'ed 将内存格式化输出
  • 删除 conio.h 作为不需要它
  • ,终于让它工作了! :)

编辑:根据OP作者的请求更新代码。

#include <stdlib.h>
#include <stdio.h>

/* global variable to store the size */
int g_size;

void ADDER(int *a, int *b) 
{
    int i;
    for (i=0 ; i<g_size ; i++) {
        b[i] += a[i];
    }

    return;     
}

int main(void) 
{
    int n, i;

    printf("Enter the number of elements: ");
    scanf("%d", &n);

    int *a = NULL;
    if ((a = (int *)malloc(n * sizeof(int))) == NULL) {
        printf("unable to allocate memory \n");
        return -1;
    }

    for (i=0; i<n; i++) {
        printf("Enter element number %d: ", i);
        scanf("%d", &a[i]);
    }

    g_size = n;
    ADDER(a, a);

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

    free(a);
    return 0;
}

I made the following modifications:

  • error checking for malloc()
  • once done, free()'ed the memory
  • formatted output
  • removed conio.h as it is not needed
  • and, finally made it work! :)

EDIT: Update the code as per the request of the OP author.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文