Thread: Need serious help on array of structures

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    Need serious help on array of structures

    I can't figure out how to access data members in an array of structures to store data.

    Here is my program:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    struct drivers_license
    {
    char lastname[10];
    char firstname[10];
    int licensenumber[8];
    char statecode[2];
    struct address;
    int height;
    char eyecolor[8];
    char haircolor[8];
    struct vehicle;
    char visioncode[10];
    int birthdate[3];
    int expirationdate[3];
    char organdonor[3];
    };

    struct address
    {
    int streetnumber[5];
    char streetname[10];
    int aptnumber[5];
    char city[10];
    char state[10];
    int zipcode[5];
    };

    struct vehicle
    {
    char motorcycle;
    char automobile;
    char schoolbus;
    char commericaltruck;
    char semitrailer;
    };

    void records(struct drivers_license record[15]);
    void changes(struct drivers_license record[15]);

    int main()
    {
    struct drivers_license record[15];
    return 0;
    }

    //Now this below is what someone else wrote to help and sprintf does not do what exactly I would like to do, I want the character string inputed in but it won't print out, I need someone to show different ways of inputing the character strings in the array of structures

    void records(struct drivers_license record[15])
    {

    sprintf( record[0].lastname, "Smith" );
    }

    void changes(struct drivers_license *record[15])
    {
    }

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    To store a name in your drivers_license structure you would do strcpy(record[0].firstname, "Myname");
    This will store myname in the first structure of the array.
    When using a pointer you need to use the -> operator instead of the . operator
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    well what do you want to do? sprintf should work...here's an example of how to do this kinda stuff:

    Code:
    struct info_{
    	char name[30];
    	int age;
    }info[4];
    
    int main(void)
    {
    	sprintf(info[0]->name,"Jimmy Olsen");
    	info[0].age = 23;
    	sprintf(info[1]->name,"something");
    	info[1].age = 49; 
    	sprintf(info[2]->name,"something else");
    	info[2].age = 63; 
    	sprintf(info[3]->name,"Joe bob");
    	info[3].age = 47; 
    	sprintf(info[4]->name,"Mary Poppins");
    	info[4].age = 10; 
    
    	for(int x=0;x<4;x++)
    		printf("%s\n%d\n",info[x]->name, info[x].age);
    
    	return 0;
    }
    or you could get the name/age from the user by using...

    Code:
    printf("Enter name: ");
    gets(info[x]->name);
    printf("enter age: ");
    scanf("%d",&info[x].age);
    that help at all?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 60
    Last Post: 01-09-2009, 01:09 PM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. filling an array of structures?
    By voodoo3182 in forum C Programming
    Replies: 9
    Last Post: 08-06-2005, 05:29 PM
  4. Array of Structures: Sorting
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-13-2005, 09:55 AM
  5. Filling an Array of Structures
    By Zildjian in forum C Programming
    Replies: 5
    Last Post: 11-12-2003, 05:54 PM