I want to write data inside the struct, can someone help me on the
syntax of the scanf function in this case?
Like was previously mentioned, whenever you are working with structs, use the .
or ->
operators to access the fields within the struct directly. You can work with direct memory manipulation, but doing that is much, much harder, leads to much messier code, and is very prone to errors.
In this particular case you can access the peso
field like any other variable by using cesna->peso
, and the clMax
field by using cesna->clMax
.
However...
scanf function
Whomever taught you about this function is doing you a great disservice. Not only is this function borderline obsolete, unless you know exactly what to do with scanf
, chances are big you will introduce memory leaks in your code. See this question for further info.
Disadvantages of scanf
It is widely recommended that you either look into the fgets()
system call or look up how to send arguments to the program via command line and the argc
+ argv
functions. Both are useful tools in your arsenal. More on this topic:
https://c-for-dummies.com/blog/?p=3379
http://crasseux.com/books/ctutorial/argc-and-argv.html
Argc+Argv does require you to start the program from command line but is super useful to integrate with other tools in the Linux world.
To be clear, scanf
is nice to teach beginners so they do not need to be concerned about things like string-to-numbers conversions and buffer overflows just to write a program that asks and displays your age, but as soon as you learn what a string is and how to use it in C... You should drop scanf
, for your own sanity.