功能在不传递参数给另一个函数的情况下正常工作吗?

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

Function works properly without passing parameter to another function?

问题

The provided code appears to be written in C/C++. It consists of two functions: getArray and DisplayArray. The getArray function prompts the user for input and stores it in an array, while the DisplayArray function prints the values of an array.

The reason it works properly even though you didn't pass the array to the DisplayArray function is that both functions are operating on a global array named array. In C/C++, functions have access to global variables, so both functions can access and manipulate the array variable without needing to pass it as an argument.

Please note that there is a slight issue in the code: the variable n in the DisplayArray function is not defined, which may lead to undefined behavior. You should define n and ensure it contains the correct number of elements to display from the array variable.

If you have any specific questions about the code or need further assistance, feel free to ask.

英文:

How does this code work properly? I use a function to insert an array and another function to print that array in the console but I didn't passed the array to the function to print the array but it works fine. What is the reason for that?

program

void getArray();
void DisplayArray();

int main(void)
{
	getArray();
	DisplayArray();
	return EXIT_SUCCESS;
}

void getArray()
{
	int limit, b[10];
	printf("Enter the limit:");
	scanf("%d", &limit);
	printf("Enter the values of the array:");

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

void DisplayArray()
{
	int i, n, array[10];
	printf("The values of Array is: \n");
	for (i = 0; i < n; i++)
	{
	    printf("%d\t", array[i]);
	}
}

Output

Enter the limit:4
Enter the values of the array2
4
6
8
The values of Array is:
2
6
8

答案1

得分: 3

The code does not work, it has undefined behavior. The function getArray reads the numbers into a local array and the function DisplayArray prints the contents of a local array that is uninitialized, with index value running from 0 up to n, which is also uninitialized (!). It seems to behave as expected because both arrays happen to be allocated at the same address at runtime, but you cannot rely on this. The behavior is just undefined.

Indeed, you'll see that not both outputs are not exactly the same.

Here is a modified version with defined behavior:

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

void getArray(int *array, int n);
void DisplayArray(const int *array, int n);

int main(void) {
    int limit, b[10];

    printf("Enter the limit: ");
    if (scanf("%d", &limit) != 1) {
        fprintf(stderr, "invalid input\n");
        return EXIT_FAILURE;
    }
    if (limit < 1 || limit > 10) {
        fprintf(stderr, "invalid limit: %d\n", limit);
        return EXIT_FAILURE;
    }
    getArray(b, limit);
    DisplayArray(b, limit);
    return EXIT_SUCCESS;
}

void getArray(int *array, int n) {
    printf("Enter the %d values of the array:\n", n);
    for (int i = 0; i < n; i++) {
        if (scanf("%d", &array[i]) != 1) {
            fprintf(stderr, "invalid input\n");
            exit(EXIT_FAILURE);
        }
    }
}

void DisplayArray(const int *array, int n) {
    printf("The values of Array are: ");
    for (int i = 0; i < n; i++) {
        printf(" %d", array[i]);
    }
    printf("\n");
}
英文:

The code does not work, it has undefined behavior. The function getArray reads the numbers into a local array and the function DisplayArray prints the contents of a local array that is uninitialized, with index value running from 0 up to n, which is also uninitialized (!). It seems to behave as expected because both arrays happen to be allocated at the same address at runtime, but you cannot rely on this. The behavior is just undefined.

Indeed, you'll see that not both outputs are not exactly the same.

Here is a modified version with defined behavior:

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

void getArray(int *array, int n);
void DisplayArray(const int *array, int n);

int main(void) {
    int limit, b[10];

    printf(&quot;Enter the limit: &quot;);
    if (scanf(&quot;%d&quot;, &amp;limit) != 1) {
        fprintf(stderr, &quot;invalid input\n&quot;);
        return EXIT_FAILURE;
    }
    if (limit &lt; 1 || limit &gt; 10) {
        fprintf(stderr, &quot;invalid limit: %d\n&quot;, limit);
        return EXIT_FAILURE;
    }
    getArray(b, limit);
    DisplayArray(b, limit);
    return EXIT_SUCCESS;
}

void getArray(int *array, int n) {
    printf(&quot;Enter the %d values of the array:\n&quot;, n);
    for (int i = 0; i &lt; n; i++) {
        if (scanf(&quot;%d&quot;, &amp;array[i]) != 1) {
            fprintf(stderr, &quot;invalid input\n&quot;);
            exit(EXIT_FAILURE);
        }
    }
}

void DisplayArray(const int *array, int n) {
    printf(&quot;The values of Array are: &quot;);
    for (int i = 0; i &lt; n; i++) {
        printf(&quot; %d&quot;, array[i]);
    }
    printf(&quot;\n&quot;);
}

答案2

得分: 0

在这个函数中,

void DisplayArray()
{
    int i, n, array[10];
    printf("Array的值是:\n");
    for (i = 0; i < n; i++)
    {
        printf("%d\t", array[i]);
    }
}

变量 array 没有初始化。当你读取一个未初始化的变量时,程序的行为是未定义的。这意味着它可以做任何事情。如果在这两者之间调用另一个函数,例如,输出会发生变化。

始终要初始化你的变量。

英文:

In this function

void DisplayArray()
{
    int i, n, array[10];
    printf(&quot;The values of Array is: \n&quot;);
    for (i = 0; i &lt; n; i++)
    {
        printf(&quot;%d\t&quot;, array[i]);
    }
}

the variable array is unitialized. When you read an uninitialized variable, the behavior of your program is undefined. That means it can do absolutely anything. If you call another function between these two, for example, the output changes.

Always initialize your variables.

答案3

得分: 0

不工作。 在函数“DisplayArray”中使用未初始化的数组“array”被称为未定义行为。

由于新分配的堆栈中的“array”与数组“b”位于内存中的相同位置,因此您会看到数字发生冲突。

但这并不意味着您的程序正在运行。 这只是一个巧合。

英文:

It does not work. It is called undefined behaviour as you use uninitialized array array in the function DisplayArray.

You see the numbers be incident because the new stack allocated array was located at the same place in memory as the array b.

But it doesn't mean that your program is working. It is just a coincidence.

huangapple
  • 本文由 发表于 2023年5月21日 02:07:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296697.html
匿名

发表评论

匿名网友

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

确定