How to read values ​​in a structure using pointers?

I know when I have to print, I use p->realand so on, but what should I write when I read numbers using scanf?

#include <stdio.h>

typedef struct {
    int real;
    int imaginary;
} complex;

void read(complex*);

void main() {
    complex c;
    read(&c);
}    

void read(complex* p){
    /*what to write in scanf*/
}
+5
source share
3 answers

You can write:

scanf("%d %d", &p->real, &p->imaginary);

but it depends a lot on the number input format.

+10
source

scanfit is required to pass the address of the memory space in which you want to store the result, unlike printffor which only the value is required (it does not care where the value is located). To get the address of a variable in C, you use the and operator:

int a;
scanf("%d", &a);

: , a. , , , ..:

struct some_struct* pointer = ........;
scanf("%d", &pointer->member);

.

+4

Use the following code:

scanf("%d",&pointer->variable);
-2
source

All Articles