Thread: a question about nested structure

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    45

    a question about nested structure

    Hi
    Code:
        struct StudentData{
            char name[30];
            struct{
                int midtermExam;
                int finalExam;
                int absenteeism;
            }Information;
        };

    I have writed a function returning void value. I want to show in the screen.
    Code:
    void showStudentStatus(struct StudentData *Students)
    {
        int i;
        struct StudentData *tempPointer;
        
        for(i=0;i<SIZE;i++)
        {    
            tempPointer=Students;
            printf("%s",Students.Information->finalExam); // ERROR ISNT IT ? 
        }

    Please hep me?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Firstly, the printf()format needs to match whatever it is printing. %s is for strings. The finalExam member of the nested struct (Information) is an int, not a string, so needs to be printed using %d.

    Second, assuming Students is of type "struct StudentData *" (as it is according to specification of your function arguments) AND that it points to a valid object, valid ways of accessing the finalExam member include
    Code:
        Students->Information.finalExam;
    The "->" reflects the fact that Students is a pointer to a struct, Information is a member of that struct. The "." refers to the fact that "Students->Information" is a struct, not a pointer to a struct.

    Alternatively, If Students points to the first element of an array, and i is a valid index in that array, then one way to access the same member (for the i'th element in the array) is
    Code:
        Students[i].Information.finalExam;
    In this case, no need for "->" since Students[i] accesses the struct, and is not a pointer.


    You code doesn't need the variable tempPointer at all. Particularly since, other than assigning a value to it, your code never uses it.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    45
    grumpy

    Thank you so much. I understood very well. Thanks a lot... You're wonderful guy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested Structure help ~
    By Siaw in forum C Programming
    Replies: 6
    Last Post: 10-09-2011, 02:59 AM
  2. Nested Structure help ~
    By Siaw in forum C Programming
    Replies: 32
    Last Post: 10-09-2011, 01:48 AM
  3. pointer to nested structure
    By wahid in forum C Programming
    Replies: 2
    Last Post: 11-10-2010, 04:05 AM
  4. initializing nested structure arrays
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 06-10-2004, 10:58 PM
  5. nested structure help
    By whistler in forum C Programming
    Replies: 1
    Last Post: 05-17-2002, 10:48 AM