Thread: structures

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    24

    structures

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    struct dir{
    char name[10];
    int no[10];
    };
    struct dir doc[4];
    int i;
    for(i=0;i<5;i++){
    printf("Enter name of doctor %d\n",i+1);
    scanf("%s",doc.name);
    printf("Enter the phone number of doctor %d\n",i+1);
    scanf("%d",&doc.no);
    }
    }

    problem?
    structure required on lefft side of . or .*

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Clue:

    Code:
    int x[4];
    
    scanf("%d",&x[0]);

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by royroy View Post
    [code]
    problem?
    structure required on lefft side of . or .*
    You need to indicate to your input statements which struct in the array you want to populate with the information. So give your scanf statement an index.

    Also check your for loop. You declare an array of 4 structures of dir type then you populate 5 of those structs, instead of 4. Basically an off by one error is what you have there.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Which 'doc' are you reading into each time?
    Where you've got a 5 and where you've got a 4, those are going to need to be the same number.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Apart from the problem with indices
    Code:
    struct dir doc[4];
    // ...
    scanf("%s",doc.name);
    is the cause of the original problem.

    An array of struct dir's does not support the dot operator for struct access.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures as members of structures & offset
    By trish in forum C Programming
    Replies: 24
    Last Post: 07-16-2011, 05:46 PM
  2. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  3. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 03:55 AM
  4. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM