在c中本地使用指针调用和返回函数

标签 c pointers global-variables local-variables

你好,我正在学习指针,所以我创建了一个计算器。 我设法从函数返回值和指针,但通过全局声明它们。我如何在本地声明它们?

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

所有函数的声明

int Addition();
int Subtraction();
int Devision();
int Multiplication();

全局变量的声明,我想在本地声明它们

int p;
int n;
int *r=&n;
int *b=&p;

主函数开始

int main()
{
    int g,s;
    while (1)
    {
        printf("Please choose the Arithmetic operation: \n");
        printf("Addition-> 1 \nSubtraction-> 2 \nDevision-> 3 \nMultiplication-> 4 \nExit-> 0\n ");
        scanf("%d",&g);

用户通过输入一个数字(1、2、3、4或0退出)来选择算术运算(函数)

        if  (g==1)
        {
            s=Addition(r,b);
            printf("The addition result is %d+%d=%d",*r, *b, s);
        }
        else if  (g==2)
       {
          s=Subtraction(r,b);
          printf("The Subtraction result is %d-%d=%d",*r, *b, s);
       }

       else if  (g==3)
       {
         s=Devision(r,b);
         printf("The Devision result is %d/%d=%d",*r, *b, s);
       }

       else if  (g==4)
      {
        Multiplication(r,b);
        printf("The Multiplication result is %d/%d=%d",*r, *b, s);
      }
       else
      {
       break;
      }

     return 0;
   }

}

主函数结束。 以下是所有其他功能

Addition()
{
    int x;

    printf("Ennter first nr: ");
    scanf("%d",&n);
    printf("Ennter second nr: ");
    scanf("%d",&p);
    x=n+p;

    return x;
}

Subtraction()
{
    int x;
    printf("Ennter first nr: ");
    scanf("%d",&n);
    printf("Ennter second nr: ");
    scanf("%d",&p);
    x=n-p;
   return x;
}

Devision()
{
    int x;
    printf("Ennter first nr: ");
    scanf("%d",&n);
    printf("Ennter second nr: ");
    scanf("%d",&p);
    x=n / p;
    return x;
}

 Multiplication()
 {
     int x;
     printf("Ennter first nr: ");
     scanf("%d",&n);
     printf("Ennter second nr: ");
     scanf("%d",&p);
     x=n * p;
     return x;
}

最佳答案

1) 刷新有关指针的知识,如果需要,可以通过指针传递参数。

间接或取消引用运算符*给出指针所指向的对象的内容。

一元或一元运算符&给出变量的地址。

2) 避免全局变量在 main 之后在本地声明。

3) 使用switch代替if-else

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

int Addition(int* n, int* p)
{
    int x;

    printf("Ennter first nr: ");
    scanf("%d",n);
    printf("Ennter second nr: ");
    scanf("%d",p);        
    x = *n + *p;    
    return x;
}

int Subtraction(int* n, int* p)
{
    int x;

    printf("Ennter first nr: ");
    scanf("%d",n);
    printf("Ennter second nr: ");
    scanf("%d",p);
    x = *n  - *p;  
    return x;
}

int main(void)
{  
    int n1 = 0;
    int n2 = 0;
    int *r = &n1;
    int *b = &n2;   
    int g,s;

    while (1)
    {
        printf("Please choose the Arithmetic operation: \n");
        printf("Addition-> 1 \nSubtraction-> 2 \nDevision-> 3 \nMultiplication-> 4 \nExit-> 0\n ");
        scanf("%d",&g);

        switch(g)
        {
            case 0:
               printf("END\n");
               return 0;
            break;                
            case 1:
                s = Addition(r, b);
                printf("The addition result is: %d+%d=%d\n\n",n1, n2, s);
                break;
            case 2:
                s = Subtraction(r ,b);
                printf("The Subtraction result is: %d-%d=%d\n\n",*r, *b, s);
            break;
        }
    }

    return 0;
}

测试:

Please choose the Arithmetic operation:                                                                                                         
Addition-> 1                                                                                                                                    
Subtraction-> 2                                                                                                                                 
Devision-> 3                                                                                                                                    
Multiplication-> 4                                                                                                                              
Exit-> 0                                                                                                                                        
 2                                                                                                                                              
Ennter first nr: 5                                                                                                                              
Ennter second nr: 3                                                                                                                             
The Subtraction result is: 5-3=2                                                                                                                

Please choose the Arithmetic operation:                                                                                                         
Addition-> 1                                                                                                                                    
Subtraction-> 2                                                                                                                                 
Devision-> 3                                                                                                                                    
Multiplication-> 4                                                                                                                              
Exit-> 0                                                                                                                                        
 0                                                                                                                                              
END

关于在c中本地使用指针调用和返回函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49233929/

相关文章:

Android,获取指向 ParseObject 的指针

c - 指向结构的指针声明会为其成员分配内存吗?

matlab - 使用 Globals 而不是在 Matlab 中传递大型数组

c - 收到的 IM-MSG 信号消息

编译器错误为 'request for member ' 价格',不是结构或 union '

c - 这个模块有过时的符号

c - 为什么 'z'中存储的值为35?难道不应该是20吗,因为在函数 'c=*b'中(c等于*b指向的值)?

C - 代码表示头未声明 - 尝试将节点插入链表

javascript - 为什么我的变量在 javascript 中返回未定义?

C++ 输入全局变量可跨多个类访问