Thread: pointer to array of structures

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Cool pointer to array of structures

    Basically, I have a pointer to a pointer which points to an array of structures. I want to be able to dereference the pointer for individual elements in the structure.

    But the code I have written doesn't work...


    Code:
    printf( "Name: %s %s\n", (**cust[i]).first, (**cust[i]).last);
    ... Can anyone please help??

    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Thumbs up

    This is the full code for the program

    Code:
    #define _CRT_SECURE_NO_DEPRECATE
    #include "airline.h"
    
    void display_menu()
    { 
    	printf( "=========================================================\n");
    	printf( " To chose a function, enter its letter label:\n\n");
    	printf( "a) Show assigned seats with passenger names\n"); printf( "b) Show list of empty seats\n");
    	printf( "c) Assign a customer to a seat\n"); printf( "d) Edit customer details, for a specific seat\n");
    	printf( "e) Delete a seat assignment\n"); printf( "f) Quit\n");
    	printf( "=========================================================\n"); 
    }
    
    void options(pass* p) 
    { 
    	char choice;
    	for(;;) 
    	{ 
    		scanf("%c", &choice);
    		switch (tolower(choice)) 
    		{ 
    		case 'a': 
    			disp_assigned(&p);
    			break; 
    		case 'b': 
    			//empty_seat();
    			break; case 'c': 
    			//allocate_seat();
    			break; case 'd': 
    			//edit_details(); 
    			break; case 'e': 
    			//delete_seat(); 
    			break; case 'f': 
    			exit(1); 
    			break; 
    		default: 
    			printf( "Invalid option, please try again:\n"); fflush(stdin);
    			break; 
    		} 
    	} 
    } 
    
    void disp_assigned(pass **cust) 
    { 
    	int i; 
    
    	for (i = 0; i < LIMIT; i++) 
    	{ 
    		if (!(**cust).flag)		
    			printf("Seat unassigned");
    		else
    		printf( "\nThe details of passenger %d are:\n", i+1);
    		printf( "Name: %s %s\n", (**cust).first, (**cust).last); 
    	}
    } 
    
    
    
    int main() 
    { 
    	pass	customers[LIMIT]; // An array of 14 (LIMIT) structures
    	int		i; 
    
    	for(i=0; i<LIMIT; i++)
    
        {
          customers[i].row = (i/2) + 1;   /* rows from 1 to 7 */
          customers[i].label = (i%2)?'B':'A';  /* odd i B, even A */
          customers[i].flag = 0;   /* seat empty */
          strcpy(customers[i].first, "Not ");
    	  strcpy(customers[i].last, "Assigned");
    	}
    
    	
    	for(i=0; i<LIMIT; i++) 
    	{ 
    	strcpy(customers[i].first, "Unassigned");
    	}
    
    display_menu(); 
    
    options(&customers[LIMIT]); 
    
    }


    this is my header file:


    Code:
    /* airline.h */ 
    #define _CRT_SECURE_NO_DEPRECATE
    
    #include<stdio.h>
    #include<string.h>
    
    #define NAME 20
    #define LIMIT 14
     
    struct	passenger
    {
    int row;     /* from 1 to 7 */
    char label;  /* A or B */
    int flag;  //Use as a boolean - 1 if seat taken, 0 if not takes
    char first[NAME];
    char last[NAME];
    };
    
    typedef struct passenger pass;
    
    /* function prototypes */ 
    
    void display_menu(void);
    void options(pass* p); 
    void disp_assigned(pass** cust);

    Hopefully that will help you guys to help me


    Thanks again !

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    (&customers[LIMIT]); access out of bounds
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    ok, so how do i get round this problem??

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should access only members of the array inside the bounds from 0 to LIMIT-1
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    so what should i do about this line???

    Code:
    printf( "Name: %s %s\n", (**cust[i]).first, (**cust[i]).last);

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > disp_assigned(&p);
    Are you doing this because you have to (for your assignment), or because you think you need to (you don't).

    > options(&customers[LIMIT]);
    To pass the array, it's just
    options( customers );

    > printf( "Name: %s %s\n", (**cust[i]).first, (**cust[i]).last);
    It you must use a pointer to a pointer, then you would do this
    printf( "Name: %s %s\n", (*cust)[i].first, (*cust)[i].last );

    Or you can remove a level of indirection by doing something like
    Code:
    void disp_assigned(pass **cust) 
    { 
    	int i; 
    	pass *temp = *cust;
    
    	for (i = 0; i < LIMIT; i++) 
    	{ 
    		if (!temp[i].flag)		
    			printf("Seat unassigned");
    		else
    		printf( "\nThe details of passenger %d are:\n", i+1);
    		printf( "Name: %s %s\n", temp[i].first, temp[i].last); 
    	}
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Thumbs up

    I get ya!!! Thanks Salem!!! :-) :-)

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    OK I have modified my code to thus...

    Code:
    #define _CRT_SECURE_NO_DEPRECATE
    #include "airline.h"
    
    void display_menu()
    { 
    	printf( "=========================================================\n");
    	printf( " To chose a function, enter its letter label:\n\n");
    	printf( "a) Show assigned seats with passenger names\n"); 
    	printf( "b) Show list of empty seats\n");
    	printf( "c) Assign a customer to a seat\n");
    	printf( "d) Edit customer details, for a specific seat\n");
    	printf( "e) Delete a seat assignment\n");
    	printf( "f) Quit\n");
    	printf( "=========================================================\n"); 
    }
    
    void options(pass* p) 
    { 
    	int j=0;
    	char choice;
    	for(;;) 
    	{ 
    		scanf("%c", &choice);
    		switch (tolower(choice)) 
    		{ 
    		case 'a': 
    			for(j=0; j<LIMIT; j++)
    			{
    			disp_assigned(p, j);
    			}
    			break; 
    		case 'b': 
    			//empty_seat();
    			break; case 'c': 
    			//allocate_seat();
    			break; case 'd': 
    			//edit_details(); 
    			break; case 'e': 
    			//delete_seat(); 
    			break; case 'f': 
    			exit(1); 
    			break; 
    		default: 
    			printf( "Invalid option, please try again:\n"); fflush(stdin);
    			break; 
    		} 
    	} 
    } 
    
    void disp_assigned(pass *cust, int i) 
    {
    		if (cust[i].flag = 0)		
    			printf("Seat %d is unassigned", i+1);
    		else
    		printf( "\nThe details of passenger %d are:\n", i+1);
    		printf( "Name: %s %s\n", cust[i].first, cust[i].last); 
    } 
    
    
    
    int main() 
    { 
    	pass	customers[LIMIT]; // An array of 14 (LIMIT) structures
    	int		i; 
    
    	for(i=0; i<LIMIT; i++)
    
        {
          customers[i].row = (i/2) + 1;   /* rows from 1 to 7 */
          customers[i].label = (i%2)?'B':'A';  /* odd i B, even A */
          customers[i].flag = 0;   /* seat empty */
          strcpy(customers[i].first, "Not ");
    	  strcpy(customers[i].last, "Assigned");
    	}
    
    display_menu(); 
    
    options(customers); 
    
    }

    header file...

    Code:
    /* airline.h */ 
    #define _CRT_SECURE_NO_DEPRECATE
    
    #include<stdio.h>
    #include<string.h>
    
    #define NAME 20
    #define LIMIT 14
     
    struct	passenger
    {
    int row;     /* from 1 to 7 */
    char label;  /* A or B */
    int flag;  //Use as a boolean - 1 if seat taken, 0 if not takes
    char first[NAME];
    char last[NAME];
    };
    
    typedef struct passenger pass;
    
    /* function prototypes */ 
    
    void display_menu(void);
    void options(pass* p); 
    void disp_assigned(pass* cust, int j);

    This piece of code isnt working... its not getting the value what it should be. How do i rectify this?

    Code:
    if (cust[i].flag = 0)

    thanks

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    = is for assignment, == is for comparison. You're doing a comparison, so use ==.

    [edit] BTW, tolower() is in <ctype.h>. [/edit]
    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.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (cust[i].flag = 0)
    Use ==

    > printf( "\nThe details of passenger %d are:\n", i+1);
    > printf( "Name: %s %s\n", cust[i].first, cust[i].last);
    The second line will always be printed.
    Use braces to make your intention explicit.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    void display_menu()
    { 
    	printf( "=========================================================\n");
    	printf( " To chose a function, enter its letter label:\n\n");
    	printf( "a) Show assigned seats with passenger names\n"); 
    	printf( "b) Show list of empty seats\n");
    	printf( "c) Assign a customer to a seat\n");
    	printf( "d) Edit customer details, for a specific seat\n");
    	printf( "e) Delete a seat assignment\n");
    	printf( "f) Quit\n");
    	printf( "=========================================================\n"); 
    }
    You don't really need that many printf() statements. Have a look at this: http://board.theprogrammingsite.com/viewtopic.php?t=85
    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.

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Smile

    Thanks guys! Much appreciated!!!

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Question

    I've another problem - I got that other bit working fine now - thanks!! Its with the options function - theres no way of breaking out of the loop unless you exit. This is a problem since after executing a particular option from the menu I want the program to display the menu again except for choice (f) and to keep doing so until the program is terminated.

    I'm a bit stuck as what to do. Any ideas or suggestions please??

    Thanks!!!!!!!!!

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Put a call to display_menu() in the for(;;) loop.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 2
    Last Post: 01-08-2008, 02:05 PM
  3. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 4
    Last Post: 01-02-2008, 11:20 AM
  4. Pros pls help, Pointer to array
    By kokopo2 in forum C Programming
    Replies: 7
    Last Post: 08-17-2005, 11:07 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM