【问题标题】:Passing the value of pointer传递指针的值
【发布时间】:2019-12-20 13:48:31
【问题描述】:

我想创建一个简单的程序来更好地理解指针的工作原理,但遇到了一个问题。我想处理 3 个文件 main.c modul.c 和 modul.h。

modul.h

typedef struct                                                                               
{                                                                                          
   int data;                                                                                 
}w_options;                                                                                
int show(); //prototype of function

modul.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "modul.h"

int show()
{

  w_options *color;
  color = (w_options *)malloc(sizeof(w_options));
  printf("%d\n", color->data);

  if (color->data == 1)
  {
    printf("Good\n");
  }
  else
  {
    printf("Bad\n");
  }
}

ma​​in.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "modul.h"

int main()
{
   w_options *color;
   color = (w_options *)malloc(sizeof(w_options));
   color->data=1;
   printf("%d\n", color->data);
   show(); 
}

ma​​in.c 中,我将 color-&gt;data 的值设置为 1,它正在工作,正在打印 1。但我想将此设置值传递给 modul.c。这就是我创建简单的if 指令来检查值是否通过的方式。不幸的是,该值没有通过,我不知道如何修复它。我的大型程序需要这种解决方案。

输出:

1
0
Bad

【问题讨论】:

  • 你没有向show()传递任何东西?
  • 您是否知道show 中的(本地)color 变量与main 中的color 变量不同?您需要修改该函数的签名并实际传递该变量。

标签: c pointers structure


【解决方案1】:

您只需将它作为参数传递给您的函数。而且,由于您的函数不返回任何内容,因此将其声明为 void。

modul.h

typedef struct                                                                               
{
   int data;
}w_options;
void show(w_options *color); //prototype of function

modul.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "modul.h"

void show(w_options *color)
{


  if (color->data == 1)
  {
    printf("Good\n");
  }
  else
  {
    printf("Bad\n");
  }
}

ma​​in.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "modul.h"

int main()
{
  w_options *color;
  color = (w_options *)malloc(sizeof(w_options));
  color->data=1;
  printf("%d\n", color->data);
  show(color);

  return EXIT_SUCCESS;
}

【讨论】:

    【解决方案2】:

    目前您没有传递任何值。

    modul.c 中,您正在创建一个名为color 的指针,它没有初始化值,您只是在分配内存。

    color = (w_options *)malloc(sizeof(w_options));
    printf("%d\n", color->data);
    

    碰巧将0打印为当前值。这根本无法保证。

    main.c 中,您正在创建另一个不同的指针,也称为color,但在不同的范围内,其自己的color-&gt;data 设置为1

    color = (w_options *)malloc(sizeof(w_options));
    color->data=1;
    printf("%d\n", color->data);
    

    这会正确地将1 打印为当前值,因为您已正确初始化它。

    如果您希望show 使用指针,请将指针作为参数传递给它并在另一端使用它。

    main.c

    ...
    show(color);
    ...
    

    模块.c

    ...
    int show(w_options *color)
    {
    
      // this is a parameter now, receiving the value from its caller
      //w_options *color;
      //color = (w_options *)malloc(sizeof(w_options));
      printf("%d\n", color->data);
    
      if (color->data == 1)
      {
        printf("Good\n");
      }
      else
      {
        printf("Bad\n");
      }
    }
    ...
    

    【讨论】:

      猜你喜欢
      • 2014-04-07
      • 1970-01-01
      • 2016-12-25
      • 2011-07-28
      • 1970-01-01
      • 2016-12-05
      • 2020-08-16
      • 2017-04-26
      相关资源
      最近更新 更多