Structures in C

We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except t...




We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create a structure called person which is made up of a string for the name and an integer for the age. Here is how you would create that person structure in C:
struct person
{
   char *name;
   int age;
};

The above is just a declaration of a type. You must still create a variable of that type to be able to use it. Here is how you create a variable called p of the type person:
#include<stdio.h>
 
struct person
{
   char *name;
   int age;
};
 
int main()
{
   struct person p;
   return 0;
}

To access the string or integer of the structure you must use a dot between the structure name and the variable name.
#include<stdio.h>
 
struct person
{
   char *name;
   int age;
}/;
 
int main()
{
   struct person p;
   p.name = "John Smith";
   p.age = 25;
   printf("%s",p.name);
   printf("%d",p.age);
   return 0;
}/

Type definitions

You can give your own name to a variable using a type definition. Here is an example of how to create a type definition called intptr for a pointer to an integer.
#include<stdio.h>
 
typedef int *intptr;
 
int main()
{
   intptr ip;
   return 0;
}/

Type definitions for a structure

If you don't like to use the word struct when declaring a structure variable then you can create a type definition for the structure. The name of the type definition of a structure is usually all in uppercase letters.
#include<stdio.h>
 
typedef struct person
{
   char *name;
   int age;
} PERSON;
 
int main()
{
   PERSON p;
   p.name = "John Smith";
   p.age = 25;
   printf("%s",p.name);
   printf("%d",p.age);
   return 0;
}

Pointers to structures

When you use a pointer to a structure you must use -> instead of a dot.
#include<stdio.h>
 
typedef struct person
{
   char *name;
   int age;
} PERSON;
 
int main()
{
   PERSON p;
   PERSON *pptr;
   PERSON pptr = &p;
   pptr->name = "John Smith";
   pptr->age = 25;
   printf("%s",pptr->name);
   printf("%d",pptr->age);
   return 0;
}/ 

Related

C TUTORIAL 4304437776552286013

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

Search Here

Popular query

Ads By Google

Get free Update

Enter your email address:

E-mail verification is must for complete subscription

Delivered by FeedBurner

RecentRecommendedComments

Recent

“The successful determination of an alleged dominant player’s domination in the relevant market is mandatory, before one can successfully proceed under S. 4 of the Competition Act, 2002”- Do you agree? Explain

 Below is a point-wise explanation discussing whether the successful determination of an alleged dominant player’s position in the relevant market is mandatory under Section 4 of the Competi...

OnePlus Pad 2: A Comprehensive Review

In the rapidly evolving landscape of tablets, OnePlus has made a significant splash with its latest offering, the OnePlus Pad 2. This tablet is packed with impressive specs and features, positioning i...

DLSA Loan Settlement Notice | How to settle | One time Settlement agreement | Advocate guideline

 If your client has received a notice for loan settlement from the District Legal Services Authority (DLSA), this means the matter is being referred for amicable settlement through Lok Adalat or ...

Unfair Means in Public Examinations: A Comprehensive Overview of the Public Examinations (Prevention of Unfair Means) Act, 2024

Public examinations hold a crucial position in shaping the academic and professional trajectories of individuals. Given their importance, the integrity of these examinations is paramount. However, unf...

Comments

Urvi Blog:

Nice blog you are posting thanks for your post : BS System Solutions

Make Cash:

UPLOAD SAMPLE FILLED UP FORM OF NOTE SHEET.

Anonymous:

Best blog I received valuable thing it give me full information. thank for making this kind of blog.it help me a lot. for more this kind of information you can check my blog also the International Co...

mithu dowari:

please provide NOC from

Circle AFS on Google Plus!

Follow AFS Google+ page
 

Side Ads

DMCA protected
Information, images and the content on this blog is Copyright ©AFS2011-2018. Please do not copy Any content for commercial purpose else we have to take a legal action. Thanks !!

Total Pageviews

Recent

free counters
 

Connect Us

Speech by ReadSpeaker

item