英文:
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 <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");
}
答案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("The values of Array is: \n");
for (i = 0; i < n; i++)
{
printf("%d\t", 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论