【发布时间】:2021-05-21 09:14:55
【问题描述】:
我正在处理开关盒的问题。 程序说明: 主要(argc,argv)。
- argv 导致 switch 语句中的情况。根据输入,将输入相应的案例并执行相应的功能。 -> 输出始终是一个结构,具有不同的内容。允许多个输入(即 main.c case1 case3)-> 两种情况都执行。
- 我的问题是处理这些数据的传递并将其保存在全局变量中,以便打印集合。在案例内部,我将本地结果传递给全局变量,但在案例的 break 语句之后,全局再次以 NULL 开头,并且不包含已执行案例的信息。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "m.h"
output* print;
int main(int argc, char* argv[])
{
output_g* global; // global_struct
if(argc > 1)
{
global= create_global(argc-1); // allocation for global struct
for(int j = 0; j < argc; j++)
{
switch (atoi(argv[i]))
{
case 123:
{
output* print= 123();
if(print== NULL)
{
return 0;
}
global = fill_global(global ,print); // hands over the current struct to the global struct
delete(print); // free function
}
break;
case 456:
{
output* print= 456();
if(print== NULL)
{
return 0;
}
global = fill_global(global ,print); // hands over the current struct to the global struct
delete(print); // free function
}
break;
case 789:
{
lfnr++;
output_1* print_liste = 789();
if(print== NULL)
{
return 0;
}
global = fill_global(global ,print); // hands over the current struct to the global struct
delete(print); // free function
}
break;
default:
break;
}
print_global_struct(file,globale_liste);
delete_global(globale_liste);
}//End for-Schleife
}// End If
return 0;
}
【问题讨论】:
-
print_global_struct、delete_global 等函数运行良好!问题主要是将每种情况下的内容交给全局并像这样经历。
-
不要评论澄清,而是edit问题
-
output* print= 123();- 在这里没有任何意义。 -
fill_global() 是做什么的?但是你可以在每个 case 赋值之后 printf() 全局值,你会发现哪里出了问题。它在 fill_global() 的某处变为 null 或 fill_global() 返回 null。传递数据与它无关。
-
@RohanBari 输出* print = 123();是进入case 123时会执行的第一个case的函数。AndreiNikolaenko只指向局部结构(global struct, local struct)
标签: c struct switch-statement global-variables