简体   繁体   English

值未初始化为 c 中的变量

[英]Value is not initialized into the variable in c

Here is the program I have made:这是我制作的程序:

#include <stdio.h>

int main(void)
{
    //Put variables here
    int space = 0;
    int num_of_rows = 0;
    int p = 1;
    int t = 0;
    char alphabet[100] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

    get_and_validate_user_input(num_of_rows);
    printf(" The number of rows are : %d", num_of_rows);
}

void get_and_validate_user_input(int num_of_rows)
{
    //Input validation
    while (1)
    {
        printf("Enter the number of rows : ");
        //Getting the input from the user
        scanf("%d", &num_of_rows);

        if (num_of_rows <= 0)
        {
            printf("Negative numbers are not allowed here \n");
        }
        else
        {
            break;
        }
    }
}

Expected Input: Enter the number of rows: 5预期输入:输入行数:5

Expected Ouput: The number of rows is: 5预期输出:行数为:5

But from my code output shows wrong.但是从我的代码输出显示错误。 It shows: The number of rows is: 0它显示:行数为:0

Now my question is: What's wrong I have been made in my program?现在我的问题是:我的程序出了什么问题? How can I solve this problem?我怎么解决这个问题?

your problem is get_and_validate_user_input modifies the local variable num_of_rows => the value stay 0 in main你的问题是get_and_validate_user_input修改局部变量num_of_rows => 值在main 中保持 0

if you want to have num_of_rows modified also out of get_and_validate_user_input give it by address rather than by value, example (unused variable commented, modify check in get_and_validate_user_input to be compatible with message) :如果您还想在get_and_validate_user_input 中修改num_of_rows ,请按地址而不是按值给出它,例如(注释未使用的变量,修改get_and_validate_user_input 中的检查以与消息兼容):

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

void get_and_validate_user_input(int * num_of_rows);

int 
main(void){
  //Put variables here
  //int space=0;
  int num_of_rows=0;
  //int p=1;
  //int t=0;
  //char alphabet[100]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  
  get_and_validate_user_input(&num_of_rows);
  printf(" The number of rows is : %d\n",num_of_rows);
  return 0;
 }

void get_and_validate_user_input(int * num_of_rows){
  //Input validation
  while(1){
    printf("Enter the number of rows : ");
    //Getting the input from the user
    if (scanf("%d",num_of_rows) != 1) {
      int c;

      puts("Invalid input");
      // invalid input, bypass all the line
      while ((c = getchar()) != '\n') {
        if (c == EOF) {
          puts("EOF, abort");
          exit(-1);
        }
      }
    }
    else if(*num_of_rows < 0){ // 0 seems allowed, else update message
      printf("Negative numbers are not allowed here \n");
    }
    else {
      break;
    }
  }
}

Compilation and execution:编译和执行:

/tmp % gcc -Wall c.c
/tmp % ./a.out
Enter the number of rows : 12
 The number of rows is : 12
/tmp % ./a.out
Enter the number of rows : aze
Invalid input
Enter the number of rows : -2
Negative numbers are not allowed here 
Enter the number of rows : 12
 The number of rows is : 12
/tmp % 

The compiler does not see in the point of the call编译器没有看到调用的点

get_and_validate_user_input(num_of_rows);

how the function get_and_validate_user_input is declared.函数get_and_validate_user_input是如何声明的。 So the compiler issue a message.所以编译器发出一条消息。 It expects that the function by default has the return type int but when it encountered the function definition it sees that the return type is void .它期望函数默认具有返回类型int但当它遇到函数定义时它看到返回类型是void

Place the function declaration before main.将函数声明放在 main 之前。

The value of the passed parameter is not used in the function传递参数的值未在函数中使用

void get_and_validate_user_input(int num_of_rows){
    //...
    //Getting the input from the user
    scanf("%d",&num_of_rows);
    //...

The function parameter num_of_rows is a local variable of the function that is initialized by the value of the passed argument.函数参数 num_of_rows 是函数的局部变量,由传递的参数值初始化。 So changing the local variable does not influence on the original argument.所以改变局部变量不会影响原始参数。

You could declare and define the function like您可以像这样声明和定义函数

int get_and_validate_user_input( void )
{
    int num_of_rows = 0;

    //Input validation
    while (1)
    {
        printf("Enter the number of rows : ");
        //Getting the input from the user
        scanf("%d", &num_of_rows);

        if (num_of_rows <= 0)
        {
            printf("Negative numbers are not allowed here \n");
        }
        else
        {
            break;
        }
    }

    return num_of_rows;
}

And call it in main like并在 main 中调用它

num_of_rows = get_and_validate_user_input();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM