使用C语言上的数组函数

huangapple go评论76阅读模式
英文:

Array using functions on C

问题

以下是翻译好的部分:

一个接受数组并在控制台上显示它的程序,使用函数。
程序应包含3个函数,包括主函数(main())。
接下来的两个函数用于接受数组的值,以及在控制台上显示数组的值。

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

void getarray(int a[], int size);
void displayarray(int a[], int size);

int main(void) {
    int a[20], size;
    getarray(a, size);
    displayarray(a, size);
    return EXIT_SUCCESS;
}

void getarray(int a[], int size) {
    int i;

    printf("输入数组的大小:");
    scanf("%d", &size);

    for (i = 0; i < size; i++) {
        scanf("%d", &a[i]);
    }
}

void displayarray(int a[], int size) {
    int i;

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

这是我尝试的。我没有得到正确的输出。

我的输出:

输入数组的大小:3
1
2
3

然后停在这里。

这里有什么错误吗?或者有没有其他方法来得到结果?

英文:

A program to accept an array and diplay it on the console using functions.
Program should contain 3 functions including main() function.
Next two functions for accepting values to the array, and display array values on the console respectively.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

void getarray(int a[],int size);
void displayarray(int a[],int size);

int main(void) {
	int a[20],size;
	getarray(a,size);
	displayarray(a,size);
	return EXIT_SUCCESS;
}

void getarray(int a[],int size){
	int i;

	printf(&quot;enter the size of the array&quot;);
	scanf(&quot;%d&quot;,&amp;size);

	for(i=0;i&lt;size;i++){
		scanf(&quot;%d&quot;,&amp;a[i]);
	}
}

void displayarray(int a[],int size){
	int i;

	for(i=0;i&lt;size;i++){
		printf(&quot;%d\t&quot;,a[i]);
	}
}

This is what I tried. I did'nt get proper output.

My output:

Enter the size of the array3
1
2
3

And stopped here.

What are the mistake in it? Or is there another way to get result?

答案1

得分: 4

你需要将一个 int* 传递给 getarray,否则 size 变量将局限于函数内部,你填写的值将无法在调用点使用:

void getarray(int a[], int *size) { // 注意:int*
    int i;

    printf("输入数组的大小");

    if (scanf("%d", size) != 1)    // 它已经是一个指针,所以不需要&
        *size = 0;

    for (i = 0; i < *size; i++) {  // 解引用以获取值
        scanf("%d", &a[i]);
    }
}

然后这样调用它:

getarray(a, &size);
英文:

You need to send in an int* to getarray otherwise the size variable will be local to the function and the value you fill in will not be available at the callsite:

void getarray(int a[], int *size) { // note: int*
    int i;

    printf(&quot;enter the size of the array&quot;);

    if (scanf(&quot;%d&quot;, size) != 1)    // it&#39;s already a pointer so no &amp;
        *size = 0;

    for (i = 0; i &lt; *size; i++) {  // dereference to get the value
        scanf(&quot;%d&quot;, &amp;a[i]);
    }
}

Then call it like so:

getarray(a, &amp;size);

答案2

得分: 2

如评论中所述,问题在于main中的sizegetarray中的size是不同的变量,即在函数内部更改size不会在main中更改它。

@TedLyngmo的答案展示了使用指针解决问题的方法。

另一种解决方法是让getarray 返回 大小。也许可以使用无符号类型表示大小。

unsigned getarray(int a[]) {
    unsigned i, size;

    printf("请输入数组的大小");

    if (scanf("%u", &size) != 1) return 0;

    for (i = 0; i < size; i++) {
        scanf("%d", &a[i]);
    }
    return size;
}

然后像这样调用它:

size = getarray(a);

顺便说一句:对于一个真正的程序,你还应该添加处理用户输入的大小超出数组范围的情况的代码。

英文:

As stated in comments, the problem is that size in main and size in getarray are different variables, i.e. changing size inside the function doesn't change it in main.

The answer from @TedLyngmo shows a way to solve the problem using pointers.

Another solution is to let getarray return the size. And perhaps use an unsigned type for size.

unsigned getarray(int a[]) {
    unsigned i, size;

    printf(&quot;enter the size of the array&quot;);

    if (scanf(&quot;%u&quot;, &amp;size) != 1) return 0;

    for (i = 0; i &lt; size; i++) {
        scanf(&quot;%d&quot;, &amp;a[i]);
    }
    return size;
}

and call it like

size = getarray(a);

BTW: For a real program you should also add code to handle cases where a user inputs a size that are too big for the array.

答案3

得分: 1

已经定义了一个包含20个元素的数组

int a[20], size;

所以这个提示

printf("输入数组的大小");

是没有意义的。而且您正在将未初始化的变量size传递给函数。结果调用第二个函数会导致未定义的行为。

在您的任务描述中写道

接下来的两个函数用于接受数组的值,并在控制台上显示数组的值。

这意味着数组应该已经被定义。所以这个提示

printf("输入数组的大小");

至少应该在main函数中。

程序可以如下所示:

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

void getarray(int a[], size_t size);
void displayarray(const int a[], size_t size);

int main(void)
{
    size_t size;

    printf("输入数组的大小:");

    if (scanf("%zu", &size) != 1)
        return EXIT_FAILURE;

    int a[size];

    getarray(a, size);
    displayarray(a, size);

    return EXIT_SUCCESS;
}

void getarray(int a[], size_t size)
{
    puts("输入数组的元素:");

    for (size_t i = 0; i < size; i++)
    {
        scanf("%d", &a[i]);
    }
}

void displayarray(const int a[], size_t size)
{
    for (size_t i = 0; i < size; i++)
    {
        printf("%d\t", a[i]);
    }
    putchar('\n');
}

或者您可以使用malloc动态分配数组,例如:

int main(void)
{
    size_t size;

    printf("输入数组的大小:");

    if (scanf("%zu", &size) != 1)
        return EXIT_FAILURE;

    int *a = malloc(size * sizeof(int));

    if (a == NULL)
        return EXIT_FAILURE;

    getarray(a, size);
    displayarray(a, size);

    free(a);

    return EXIT_SUCCESS;
}

其他两个函数保持不变。

英文:

You already defined an array with 20 elements

int a[20],size;

So this prompt

printf(&quot;enter the size of the array&quot;);

does not make sense. Also you are passing the uninitialized variable size to the functions. As a result calling the second function invokes undefined behavior.

In your description of the assignment there is written

> Next two functions for accepting values to the array, and display
> array values on the console respectively.

It means that the array should be already defined. So this prompt

printf(&quot;enter the size of the array&quot;);

should be at least in main.

The program can look the following way

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

void getarray( int a[], size_t size );
void displayarray( const int a[], size_t size );

int main(void) 
{
    size_t size;

    printf( &quot;enter the size of the array: &quot; );
    
    if ( scanf( &quot;%zu&quot;, &amp;size ) != 1 ) return EXIT_FAILURE;
    
    int a[size];

    getarray( a, size );
    displayarray( a, size );

    return EXIT_SUCCESS;
}

void getarray( int a[], size_t size )
{
    puts( &quot;enter elements of the array:&quot; );

    for ( size_t i = 0; i &lt; size; i++ )
    {
        scanf( &quot;%d&quot;, &amp;a[i] );
    }
}

void displayarray( const int a[], size_t size )
{
    for ( size_t i = 0; i &lt; size; i++ )
    {
        printf( &quot;%d\t&quot;, a[i] );
    }
    putchar( &#39;\n&#39; );
}

A;ternatively you can dynamically allocate an array using malloc as for example

int main(void) 
{
    size_t size;

    printf( &quot;enter the size of the array: &quot; );
    
    if ( scanf( &quot;%zu&quot;, &amp;size ) != 1 ) return EXIT_FAILURE;
    
    int *a = malloc( size * sizeof( int ) );

    if ( a == NULL ) return EXIT_FAILURE;

    getarray( a, size );
    displayarray( a, size );

    free( a );

    return EXIT_SUCCESS;
}

The other two funcions stay unchanged.

huangapple
  • 本文由 发表于 2023年7月20日 16:27:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定