如何为c中的结构赋予价值?

问题描述

我正在使用const中的结构,但是无法赋予结构属性任何价值。

import PySimpleGUI as sg

# List for Radiobuttons
radio_choices = ["Method 1","Method 2","Method 3","Method 4"]

layout = [[sg.Text("Choose Method")],*[[sg.Radio(text,1),sg.Text('this is ' + text,key=text,visible=False) ] for text in radio_choices],[sg.Button("Start"),sg.Button("Stop"),sg.Button("Exit")]


          ]

window = sg.Window("Choose Method",layout,size=(350,350))

while True:
    event,values = window.Read()

    print(event,values)
    if event == "Exit" or event == sg.WIN_CLOSED:
        break

    if event == 'Start':
        for k,value in values.items():
            if value == True:
                window[radio_choices[k]].update(visible=True)
            else : 
                window[radio_choices[k]].update(visible=False)
window.close()

此代码可正确编译,但会发生c错误。

解决方法

您写:

此代码可以正确编译...

在这种情况下,我强烈建议您提高编译器的警告级别。

例如“ gcc -Wall code.c”可能会向您发出警告,例如:

In function 'main':
warning: format '%d' expects argument of type 'int *',but argument 2 has type 'int' [-Wformat=]
   15 |                 scanf("%d",tmp_id);
      |                        ~^   ~~~~~~
      |                         |   |
      |                         |   int
      |                         int *

告诉您所有内容,即您将int传递给需要int指针的函数

因此只需传递一个整数指针即可:

scanf("%d",&tmp_id);
            ^
            Take address of tmp_id so that you have a pointer

顺便说一句:对于gcc,您可以使用-Werror以便将所有警告均视为错误

,

您可以直接将值分配给struct。您不需要其他变量。

struct Book a
scanf("%s",a.name);
scanf("%d",&a.id);
printf("name: %s\nid:%d",a.name,a.id);
,

scanf("%d",tmp_id);必须为scanf("%d",&tmp_id);

,

使用scanf时需要通过引用传递。

        scanf("%s",tmp_name);
        scanf("%d",&tmp_id);

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...