5

I know when I have to print I use p->real and so on but what should I write when I am reading 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
  • Just a question: are you sure your sturcture members should be int, not float or double? Commented Oct 1, 2010 at 5:18
  • 1
    So I fixed your code's formatting and changed the indentation so it was readable. It's really helpful to people if you do this when you post your original question... Commented Oct 1, 2010 at 5:22
  • @mark- how do i format the code? kindly help. Commented Oct 1, 2010 at 5:29
  • Things posted here are formatted with something called Markdown, you can read about the syntax here: stackoverflow.com/editing-help Commented Oct 1, 2010 at 5:30
  • Forget scanf exists as it usually buys more trouble than it saves by trying to do two tasks (input and parsing) and does both half-way. fgets and sscanf splits the task and makes parse errors far more easily handled. Commented Oct 1, 2010 at 5:35

4 Answers 4

14

You can write:

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

but that depends heavily on the format in which the numbers come.

Sign up to request clarification or add additional context in comments.

1 Comment

I would probably say &(p->real), just for clarity's sake.
6

scanf requires you to pass the address of the memory space you want to store the result in, unlike printf, which only requires the value (it couldn't care less where the value resides). To get the address of a variable in C, you use the & operator:

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

Meaning: read an integer into the address I specified, in this case the address of a. The same goes for struct members, regardless of whether the struct itself resides on the stack or heap, accessed by pointer, etc:

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

And that would read an integer into the address of pointer plus the offset of member into the structure.

Comments

0

You can follow any one of below:

scanf("%d%d",&(*p).real,&(*P).imaginary);

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

1 Comment

This answer looks like a duplicate of existing answers. If your answer adds something significant that has not been covered already, make sure to clarify what that is.
-3

Use Following code:

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

1 Comment

There's nothing wrong with adding a late answer to a question, but this answer adds nothing to the conservation, it's simply a duplicate of the previous two from two years ago. Please add some additional information when answering questions rather than just copping already accepted answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.