Thread: array of pointers to an array of structures

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    28

    array of pointers to an array of structures

    how does i access the structure variable by using an array of pointers to that structure..... also the structure variable is an array in this case...

    say for example

    Code:
    struct EMP
         {
            int no;
            char name[50];
            char add[100];
            char date[20];
         }emp[2], *ptr[2];
    how do i access the name member of emp[1]??

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You need to dereference the pointer, and then use a member operator. Since this is so common - a special operator exists that does both: ->

    If emp[1] is a pointer to a struct, emp[1]->name returns the member name (but remember that it's going to return a pointer to the beginning of name - you can still access inidividual elements of that array by saying emp[1]->name[49], etc...)

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    These strcpy()s all do the same thing:
    Code:
    struct EMP
         {
            int no;
            char name[50];
            char add[100];
            char date[20];
         }emp[2], *ptr[2];
    
    strcpy(emp[1].name, "Joe");
    ptr[1] = &emp[1];
    strcpy((*ptr[1]).name, "Joe");
    strcpy(ptr[1]->name, "Joe");
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    got it!!! thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 06:43 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM