Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

C Programming Structure

October 1, 2013 Posted by: GPA Views: 26075 0 comments

Problem

What is a structure in C programming?

Solution

Structure is a collection of different variables under one name. For example, You want to store inforamtion about person: fisrt name, last name, age, email, password and so on. For this you can use four or five variables of different types. For first,last name, and emal you can use char* or char array, for age - integer type. You can write these variables separately, but for such problem You can use structure and name it - "Person". Structure is usually used to format data structures such as stacks, linked lists, queues and trees.

Structure Definition

Structure consists of different data types. Variables declared in the body of the structure are the structure’s members

struct Person{

    char firstname[20]; 

   char lastname[20]; 

   char email[20];

   char password[20]; 

    int age; 

}; 

Keyword struct is used to introduce the structure definition. Person is  a name of structure is used with the keyword struct to declare variables of the structure type. Members of the same structure type must have unique names. 

Initializing Structures

After stucture is defined we can create varibles of type Person. In main function or in other function You can declare:

struct Person person1;

struct Person* person2;

You also can declare array of persons.

struct Person persons[20]; 

Accessing Structure Members 

Two operators are used to access members of structures:

 - structure member operator(.);

 -  structure pointer operator (->).

For example: to print firstname of stucture Person use the statement:

printf( "%s", person1.firstname ); 

To print firstname of structure Person with pointer person2, use the statement:

printf( "%s", person2->firstname );

Using keyword typedef while using structure

You can use typedef keyword while using structure in C language. For example:

typedef struct Person{

    char firstname[20]; 

   char lastname[20]; 

   char email[20];

   char password[20]; 

    int age; 

}per; 

In main function create variables of "per" type:

per p1,p2;

In this example typedef  is used to create a type per. Then, two variables p1 and p2 are created by this per type without using keyword "struct".

 

Example:



#include <stdio.h>
#include <stdlib.h>
typedef struct Person{ 
char firstname[20];
char lastname[20];
char email[20];
char password[20];
int age;
 
}per;  

int
main(int argc, char *argv[])
{
struct Person person1;//declare variable person1 of struct Person
struct Person* person2;//declare variable person1 of struct Person using pointer
struct Person persons[20]; //array of persons
per person3;//declare variable person3 using keyword typedef

//fill information about first Person
strcpy(person1.firstname,"Peter");
strcpy(person1.lastname,"Johnson");
strcpy(person1.email,"Peter@gamil.com");
strcpy(person1.password,"password");
person1.age=29;

//show information about first person
printf("Information about person1: \n");
printf("\nFirst name: %s",person1.firstname);
printf("\nLast name: %s",person1.lastname);
printf("\nEmail: %s",person1.email);
printf("\nPassword: %s",person1.password);
printf("\nAge: %d",person1.age);

//allocates the memory for 1 structures with pointer person2 pointing to base address
person2=(struct Person*)malloc(1*sizeof(struct Person));
//fill information about second Person
strcpy(person2->firstname,"Bred");
strcpy(person2->lastname,"Peterson");
strcpy(person2->email,"Peterson@gamil.com");
strcpy(person2->password,"password1");
person2->age=56;

//show information about second person
printf("\n\nInformation about person2: \n");
printf("\nFirst name: %s",person2->firstname);
printf("\nLast name: %s",person2->lastname);
printf("\nEmail: %s",person2->email);
printf("\nPassword: %s",person2->password);
printf("\nAge: %d",person2->age);

//fill information about third Person
strcpy(person3.firstname,"person3");
strcpy(person3.lastname,"person3lastname");
strcpy(person3.email,"person3lastname@gamil.com");
strcpy(person3.password,"person3password");
person3.age=33;

//show information about third person
printf("\n\nInformation about person3: \n");
printf("\nFirst name: %s",person3.firstname);
printf("\nLast name: %s",person3.lastname);
printf("\nEmail: %s",person3.email);
printf("\nPassword: %s",person3.password);
printf("\nAge: %d",person3.age);

//using array
int i=0;
for(i=0;i<3;i++){
strcpy(persons[i].firstname,"Fisst name "+i);
strcpy(persons[i].lastname,"Last name"+i);
strcpy(persons[i].email,i+"email@gamil.com");
strcpy(persons[i].password,"password"+i);
persons[i].age=29+i;
}
printf("\n\nInformation about three persons using array: \n");
for(i=0;i<3;i++){
printf("\n\nPerson %d",i+1);
printf("\nFirst name: %s",persons[i].firstname);
printf("\nLast name: %s",persons[i].lastname);
printf("\nEmail: %s",persons[i].email);
printf("\nPassword: %s",persons[i].password);
printf("\nAge: %d",persons[i].age);
}

//When you use pointer you can write (*person2).firstname and then you can use structure member operator(.)
//show information about second person converting structure pointer operator(->) to structure member operator(.)
printf("\n\nInformation about person2: \n");
printf("\nFirst name: %s",(*person2).firstname);
printf("\nLast name: %s",(*person2).lastname);
printf("\nEmail: %s",(*person2).email);
printf("\nPassword: %s",(*person2).password);
printf("\nAge: %d\n\n\n",(*person2).age);
system("PAUSE");//delay
return 0;
}

Output

Structure

Download this example


0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe