C 传递指针的值
我想创建一个简单的程序来更好地理解指针是如何工作的,我遇到了一个问题。我想处理3个文件main.c modul.c和modul.h 模数hC 传递指针的值,c,pointers,structure,C,Pointers,Structure,我想创建一个简单的程序来更好地理解指针是如何工作的,我遇到了一个问题。我想处理3个文件main.c modul.c和modul.h 模数h typedef struct {
typedef struct
{
int data;
}w_options;
int show(); //prototype of function
typedef struct
{
int data;
}w_options;
void show(w_options *color); //prototype of function
模数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");
}
}
#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();
}
#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");
}
}
#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;
}
只需将其作为参数传递给函数。并且,由于您的函数不返回任何内容,请将其声明为void 模数h
typedef struct
{
int data;
}w_options;
int show(); //prototype of function
typedef struct
{
int data;
}w_options;
void show(w_options *color); //prototype of function
模数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");
}
}
#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();
}
#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");
}
}
#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;
}
#包括
#包括
#包括
#包括“模数h”
无效显示(w_选项*颜色)
{
如果(颜色->数据==1)
{
printf(“良好”);
}
其他的
{
printf(“坏的”\n);
}
}
main.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");
}
}
#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();
}
#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");
}
}
#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;
}
#包括
#包括
#包括
#包括“模数h”
int main()
{
w_选项*颜色;
颜色=(w_选项*)malloc(sizeof(w_选项));
颜色->数据=1;
printf(“%d\n”,颜色->数据);
显示(颜色);
返回退出成功;
}
当前您没有传递任何值
在modul.c
中,您正在创建一个名为color
的指针,该指针没有初始化值,您只是在分配内存
color = (w_options *)malloc(sizeof(w_options));
printf("%d\n", color->data);
它碰巧将0
打印为当前值,这是一种意外。这根本不能保证
在main.c
中,您正在创建另一个不同的指针,也称为color
,但位于不同的范围内,其自身的color->data
设置为1
color = (w_options *)malloc(sizeof(w_options));
color->data=1;
printf("%d\n", color->data);
这将正确地将1
打印为当前值,因为您已正确初始化它
如果希望show
使用指针,请将指针作为参数传递给它,并在另一端使用它
main.c
模数c
您没有将任何内容传递给
show()
?您是否知道show
中的(本地)color
变量与main
中的color
变量不同?您需要修改该函数的签名并实际传递该变量。