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
Rodeo Drive | Waar projecten soepel verlopen
Expected Monetary Value: How To Calculate It in Project Management
Tabc On The Fly Final Exam Answers
Lexington Herald-Leader from Lexington, Kentucky
Sissy Hypno Gif
Kristine Leahy Spouse
Chuckwagon racing 101: why it's OK to ask what a wheeler is | CBC News
Top Golf 3000 Clubs
Cube Combination Wiki Roblox
Campaign Homecoming Queen Posters
Nioh 2: Divine Gear [Hands-on Experience]
Leeks — A Dirty Little Secret (Ingredient)
Gdlauncher Downloading Game Files Loop
Fdny Business
Convert 2024.33 Usd
Tinker Repo
Menus - Sea Level Oyster Bar - NBPT
Mega Personal St Louis
Ceramic tiles vs vitrified tiles: Which one should you choose? - Building And Interiors
8000 Cranberry Springs Drive Suite 2M600
Prot Pally Wrath Pre Patch
Prep Spotlight Tv Mn
Www Pointclickcare Cna Login
15 Primewire Alternatives for Viewing Free Streams (2024)
Timeline of the September 11 Attacks
Trinket Of Advanced Weaponry
Ullu Coupon Code
Jazz Total Detox Reviews 2022
Jamielizzz Leaked
Bj's Tires Near Me
Package Store Open Near Me Open Now
County Cricket Championship, day one - scores, radio commentary & live text
Syracuse Jr High Home Page
Tmj4 Weather Milwaukee
Roch Hodech Nissan 2023
Slv Fed Routing Number
Police Academy Butler Tech
Skip The Games Ventura
How to Destroy Rule 34
1v1.LOL Game [Unblocked] | Play Online
Frommer's Philadelphia &amp; the Amish Country (2007) (Frommer's Complete) - PDF Free Download
062203010
Vindy.com Obituaries
Academic Calendar / Academics / Home
Vérificateur De Billet Loto-Québec
Human Resources / Payroll Information
Lira Galore Age, Wikipedia, Height, Husband, Boyfriend, Family, Biography, Net Worth
Skyward Login Wylie Isd
Ret Paladin Phase 2 Bis Wotlk
The Missile Is Eepy Origin
Obituary Roger Schaefer Update 2020
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.