【问题标题】:Call and Return function using pointers locally in c在c中使用本地指针调用和返回函数
【发布时间】:2018-03-12 11:19:14
【问题描述】:

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

#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;
}

【问题讨论】:

  • 请对您的代码应用适当的缩进以使其更具可读性。
  • 并详细说明您的问题。有点不清楚。
  • 我在您的代码中没有看到任何需要使用全局变量的内容。是什么让您认为您需要使用它们?很明显,您已经知道如何声明和使用局部(非全局)变量。
  • 我在全球范围内使用它们,因为我更容易打电话和阅读它们。这就是为什么我想学习如何在本地声明它们。
  • 这里的第一步是烧掉你的 C 书/老师,然后重新学习函数是如何工作的。在接受指针之前这样做。

标签: c pointers global-variables local-variables


【解决方案1】:

1) 刷新指针知识,如果你愿意,可以通过pointers传递参数。

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

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

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

【讨论】:

  • 感谢您的代码。它真的帮助我理解了如何从函数中调用和读取指针和变量。我还在学习指针的过程中,我看到很多人侮辱和指出我。不过,我不在乎!
  • @KlaydPro 很高兴能为您提供帮助。保持学习。永不放弃。
【解决方案2】:

我在全球范围内使用它们,因为我更容易打电话和阅读它们。这就是为什么我想学习如何在本地声明它们。

非常简单,只需在本地声明它们,如图所示:(无需您的用户输入代码即可专注于您的主要问题)

int main(void)
{   // locally declared
   double a = 4.5;
   double b = 10.8;

   double result_1, result_2, result_3, result_4;

   result_1 = add(a, b);       
   result_2 = sub(a, b);       
   result_3 = mul(a, b);       
   result_4 = div(a, b);


   return 0;
}

// example function (the others will be of similar form,
// a single return line with the appropriate math operator.

double add(double a, double b)//no pointers necessary
{
    return a + b;
}

如果你真的想要使用指针,那么你可以实现函数以通过参数返回结果:

void add(double a, double b, double *result)
{
    *result = a + b;
}

用法:(例如从 main 调用)

   add(a, b, &result_1);// passing the address of result_1      
                        // to allow the value at that address
                        // to be modified.

【讨论】:

    【解决方案3】:

    好吧,不确定这是否是您要求的,但让我建议您使用以下代码:

    #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 main()
    {
        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);
    
            if  (g==1)
            {
                s=Addition(r,b);
                printf("The addition result is %d+%d=%d",*r, *b, s);
            }
    
            return 0;
        }
    }
    

    如您所见,我们使用指针来存储用户写入的数字的值,因此您可以在调用函数后保留它们。

    【讨论】:

    • 并非如此,因为您仍然可以获得全局变量。我的意图是通过在本地也使用指针在本地声明它们
    • 哦,抱歉,我进行了更改,因此它适用于局部变量,但没有移动它们。我已经编辑了这篇文章,所以它们现在是局部变量(在你的主函数中)
    猜你喜欢
    • 2014-10-27
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    • 2010-10-12
    • 2012-03-31
    相关资源
    最近更新 更多