Thread: Structures and pointers

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    7

    Structures and pointers

    Hello everyone,
    I am new to programming. I have a simple C programming question.
    My program at the minute seem to be very complicated so I am returning back to basics of structures and pointers. To make the question simple, lets say I have a structure
    Code:
    typedef struct
    {
    char name[30];
    int roll;
    }student;
    
    and i have a function which needs to access the members of this structure with a pointer. 
    
    void display()
    {
    //Access the member name and print
    //access roll and print
    
    }

    I DON'T call this function in the main at all and I dont need the structure to be a global. Therefore I have typedef the structure. Please help. The question seems trivial but the answer to this question will clarify my concepts and help me in building my code a great deal .at the moment I am stuck.
    Thank you very much

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Not a lot of information to go on there but I guess you mean you want to do something like this:
    Code:
    typedef struct student_
    {
      int roll;
      char name[30];
    } student;
    int displayStudent( FILE* uiTextArea, student *pStudent )
    {
      fprintf( uiTextArea, "Roll No #%i: %s", pStudent->roll, pStudent->name );
    }
    int main( void )
    {
      student defStudent = {0,"Default"};
      return displayStudent( stdout, &defStudent );
    }
    If your talking about the keyword "this" then hate to break it to ya but it don't exist in normal C, OBJ-C might have it, I've never gone down that route before though so I wouldn't know
    Last edited by awsdert; 02-07-2015 at 01:38 PM. Reason: spelling mistake

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    7
    Thank you very much awsdert
    for your reply.

    First of all,let me clearify , the word "this" have got highlighted due to the interpreter and has nothin to do with the code.
    The code you presented is quite nice. However my intent was different.Let me reiterate my problem.

    Code:
    typedef struct
    {
    char name[30]={"ROB"};
    }student;
     
    void display() //pass some pointer(pointing to the structure called student) as an argument to access the member "name" of the structure
    {
     //do a simple display name
    }
    
    int main()
    {
    //at no point does the main calls the display function nor do we pass any arguments to this function through main
    function() //does some random function which is not related to display()
    return 0;
    }
    I know its simple but I am simply not able to get around this problem.
    many thanks once again

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    First you say
    Quote Originally Posted by biswa View Post
    pass some pointer(pointing to the structure called student) as an argument to access the member "name" of the structure
    then you say
    Quote Originally Posted by biswa View Post
    at no point does the main calls the display function nor do we pass any arguments to this function through main. does some random function which is not related to display()
    So then, how does the pointer get to display()?

    Are you asking how to use a singleton? Like so
    Code:
    struct student_t
    {
    char name[30]={"ROB"};
    };
    student_t student;
     
    void display(  )
    {
    student.name //do a simple display name
    }
    
    int main()
    {
    // i'm just using random words here in hopes of making it harder for you to know what i'm talking about
    return 0;
    }

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Yup yup, I think Yarin has cracked it.

    Quote Originally Posted by biswa
    I DON'T call this function in the main at all and I dont need the structure to be a global. Therefore I have typedef the structure.
    I think you have misunderstood what typedef does. All it does is rename stuff. It's common with structs in C as otherwise you have to keep prefixing the word "struct" every time you use it. You're not the first beginner to be flummoxed by this:

    Code:
    typedef struct
    {
    char name[30];
    int roll;
    }student;
    This is what you have, which creates a type called "student".
    Code:
    struct 
    {
    char name[30];
    int roll;
    }student;
    This very very very similar code creates an actual structure called student. However, please use Yarin's code, as the example I just gave creates an unnamed structure type, which is a bit yuck.

    It's still global though. You can add "static" in front of it to make it only visible within the file. However, if you want it to be truly local, and pass a pointer to display(), then that's easy enough too. It's just a bit tricky to tell what you're after.

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    display will still need to be called before it will ever do anything, otherwise as far as the application is concerned it is just plain data doing nothing for it but eating space.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Ok -- what I was getting at, is that there's going to be more than one unction that needs access to the array. Unless you plan to populate the array in display() too. Nasty!

    So you need to declare the struct in the highest level function that uses it, and expect several functions to have to accept it as an argument. Here's how I'd do it:

    Code:
    typedef struct 
    {
        stuff
    } student_t;
    
    /* This jjust defines a type as I said before. It's useful to have it at the top level so functions can access the fields. */
    
    void display(student_t *s) 
    {
      s->field = stiff;
      // do stuff to s. 
    }
    
    void VeryImportantFunction(void)
    {
        student_t student;
    
        display&student);
    }
    The moment the last function ends, the struct will be destroyed..It's called "going out of scope". This isn't a problem for a lot of programs and use cases, but there are obviously many ways around it....

    • Make it global.
    • Dynocally allocate it -- but then you must still keep an eye on the pointer.
    • Define it in main. Then it'll exist until the program ends.
    • Erm, sure there's a pile of others too. Depends how badly you want that struct to survive

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    7
    smokeyangel
    you are spot on..dats exactly wat i was looking for.. But I thank everyone else for their replies too. I really appreciate the fact that so many people are ready to help each other without any gains...I am sorry i wasnt clear what i was looking for...

    However there is another problem now...I have many functions in the code which gets executed through indirect function calls. It is similar to the example of smokeyangel
    Now as in the example of smokeyangel...the function

    void display(student_t *s)

    is actually a library function. So if i pass the structure as a pointer into the function , it returns an error and I cannot change the library functions as that would lead to more complicaies in other files.So I am stuck here. I hope I am making my point clear.if not i will try to include more details..thanks once again

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by biswa
    the function

    void display(student_t *s)

    is actually a library function. So if i pass the structure as a pointer into the function , it returns an error and I cannot change the library functions as that would lead to more complicaies in other files.So I am stuck here.
    Why are you stuck? What is the error returned? Can you fix it?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help w/ Structures and Pointers
    By emanly in forum C Programming
    Replies: 5
    Last Post: 11-20-2011, 09:34 PM
  2. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM
  3. structures with pointers to structures
    By kzar in forum C Programming
    Replies: 3
    Last Post: 11-20-2004, 09:32 AM
  4. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 06:14 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM