array of pointers to an array of structures

This is a discussion on array of pointers to an array of structures within the C Programming forums, part of the General Programming Boards category; how does i access the structure variable by using an array of pointers to that structure..... also the structure variable ...

  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
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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,046
    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, 10: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, 05:43 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 06:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21