C structs and Pointers (With Examples) (2024)

Before you learn about how pointers can be used with structs, be sure to check these tutorials:

C Pointers to struct

Here's how you can create pointers to structs.

struct name { member1; member2; . .};int main(){ struct name *ptr, Harry;}

Here, ptr is a pointer to struct.

Example: Access members using Pointer

To access members of a structure using pointers, we use the -> operator.

#include <stdio.h>struct person{ int age; float weight;};int main(){ struct person *personPtr, person1; personPtr = &person1; printf("Enter age: "); scanf("%d", &personPtr->age); printf("Enter weight: "); scanf("%f", &personPtr->weight); printf("Displaying:\n"); printf("Age: %d\n", personPtr->age); printf("weight: %f", personPtr->weight); return 0;}

In this example, the address of person1 is stored in the personPtr pointer using personPtr = &person1;.

Now, you can access the members of person1 using the personPtr pointer.

By the way,

  • personPtr->age is equivalent to (*personPtr).age
  • personPtr->weight is equivalent to (*personPtr).weight

Dynamic memory allocation of structs

Before you proceed this section, we recommend you to check C dynamic memory allocation.

Sometimes, the number of struct variables you declared may be insufficient. You may need to allocate memory during run-time. Here's how you can achieve this in C programming.

Example: Dynamic memory allocation of structs

#include <stdio.h>#include <stdlib.h>struct person { int age; float weight; char name[30];};int main(){ struct person *ptr; int i, n; printf("Enter the number of persons: "); scanf("%d", &n); // allocating memory for n numbers of struct person ptr = (struct person*) malloc(n * sizeof(struct person)); for(i = 0; i < n; ++i) { printf("Enter first name and age respectively: "); // To access members of 1st struct person, // ptr->name and ptr->age is used // To access members of 2nd struct person, // (ptr+1)->name and (ptr+1)->age is used scanf("%s %d", (ptr+i)->name, &(ptr+i)->age); } printf("Displaying Information:\n"); for(i = 0; i < n; ++i) printf("Name: %s\tAge: %d\n", (ptr+i)->name, (ptr+i)->age); return 0;}

When you run the program, the output will be:

Enter the number of persons: 2Enter first name and age respectively: Harry 24Enter first name and age respectively: Gary 32Displaying Information:Name: HarryAge: 24Name: GaryAge: 32

In the above example, n number of struct variables are created where n is entered by the user.

To allocate the memory for n number of struct person, we used,

ptr = (struct person*) malloc(n * sizeof(struct person));

Then, we used the ptr pointer to access elements of person.

C structs and Pointers (With Examples) (2024)
Top Articles
BofA adds support for cardless ATM access via Apple Pay digital wallet
How do I withdraw money from my funded FX?
$4,500,000 - 645 Matanzas CT, Fort Myers Beach, FL, 33931, William Raveis Real Estate, Mortgage, and Insurance
3 Tick Granite Osrs
AMC Theatre - Rent A Private Theatre (Up to 20 Guests) From $99+ (Select Theaters)
Design215 Word Pattern Finder
Access-A-Ride – ACCESS NYC
Blackstone Launchpad Ucf
Mama's Kitchen Waynesboro Tennessee
No Hard Feelings Showtimes Near Metropolitan Fiesta 5 Theatre
Delectable Birthday Dyes
2022 Apple Trade P36
GAY (and stinky) DOGS [scat] by Entomb
Used Wood Cook Stoves For Sale Craigslist
Turning the System On or Off
Reddit Wisconsin Badgers Leaked
All Buttons In Blox Fruits
Equibase | International Results
Missouri Highway Patrol Crash
Faurot Field Virtual Seating Chart
At&T Outage Today 2022 Map
Why Are Fuel Leaks A Problem Aceable
Strange World Showtimes Near Savoy 16
Margaret Shelton Jeopardy Age
New Stores Coming To Canton Ohio 2022
Arlington Museum of Art to show shining, shimmering, splendid costumes from Disney Archives
Section 408 Allegiant Stadium
Pokemon Inflamed Red Cheats
10-Day Weather Forecast for Santa Cruz, CA - The Weather Channel | weather.com
Trust/Family Bank Contingency Plan
Lincoln Financial Field, section 110, row 4, home of Philadelphia Eagles, Temple Owls, page 1
Warren County Skyward
Melissa N. Comics
Spy School Secrets - Canada's History
Craigslist Hamilton Al
Mp4Mania.net1
4083519708
Tiny Pains When Giving Blood Nyt Crossword
ENDOCRINOLOGY-PSR in Lewes, DE for Beebe Healthcare
My Locker Ausd
Gravel Racing
Ursula Creed Datasheet
Tedit Calamity
Devon Lannigan Obituary
The Attleboro Sun Chronicle Obituaries
Despacito Justin Bieber Lyrics
10 Types of Funeral Services, Ceremonies, and Events » US Urns Online
Mcoc Black Panther
Windy Bee Favor
Roller Znen ZN50QT-E
Vrca File Converter
Latest Posts
Article information

Author: Mr. See Jast

Last Updated:

Views: 5862

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.