0

I've defined a struct and allocated the necessary space in the memory via malloc, I want to write data inside the struct, can someone help me on the syntax of the scanf function in this case?

I thought that when I created a malloc the variable has inside the address so I thought that I could navigate the struct by moving trough the memory evidently I was wrong;

struct airplanesType
{
    char TractionType[15];
    int superficeAlare;
    int peso;
    int coefficenteDiCarico;
    int rendimento;
    int cordaMedia;
    int distanzaCPCG;
    int distanzaCGCoda;
    int distanzaDragTraction;
    int MaxTraction;
    int ClMax;
    int CDclmax;
};

typedef struct airplanesType aereo;

the struct;

    aereo *cesna= malloc(sizeof(aereo));

how I call the the malloc

Ma7071
  • 1
  • 1
  • Use the member access operator `->` to assign values to the `struct` members. (e.g., `cesna->superficeAlare = 1;` I don't fully understand your question. – machine_1 Jul 26 '23 at 14:42

2 Answers2

0
  1. Use objects not types in sizeof when possible.
aereo *cesna= malloc(sizeof(*cesna));
  1. -> dereferences the struct pointer.

can someone help me on the syntax of the scanf function in this case?

scanf("%d", &cesna -> cordaMedia);

you can also use (*cesna).cordaMedia

scanf("%d", &(*cesna).cordaMedia);
0___________
  • 60,014
  • 4
  • 34
  • 74
0

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.