Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
10 views
4 pages
Accesing Structure Using Pointer
Pointers
Uploaded by
apc78044
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save accesing structure using pointer For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
10 views
4 pages
Accesing Structure Using Pointer
Pointers
Uploaded by
apc78044
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save accesing structure using pointer For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save accesing structure using pointer For Later
You are on page 1
/ 4
Search
Fullscreen
OverlQ.com (/) Home) 1 C Programming Tutorial /e-programming-101/) / Pointer to a Structure in C Pointer to a Structure in C #9 Last updated on July 27, 2020 etc, Similar, we can have a painter to structures, where a pointer variable can point to the address of a structure variable“ Here is how we ‘ 4 char breedix » 9 struct dog spikes 31 // declaring a pointer to a structure of type struct dog 32) struct dog *pt This declares a pointer ptr_dog that can store the address of the variable of type struct dog -We can now assign the address of variable spike to ptr_cog using & operator v, Now ptr_dog points to the structure variable spike “Accessing members using Pointer ‘There are two ways of accessing members of structure using pointer 1. Using indirection (+ ) operator and dot (. ) operator.V 2.Using arrow {->) operator or membership operator. Let's start with the first one. / Using Indirection (*) Operator and Dot (.) Operator Girtnis pont str oe points tothe structure variable spike, so by dereferencingitwe wl ge the contents ofthe spike. This means spike ad *pr_aot are funcional equivalent To accessa member of structure wit “pt_aoefolowed by «do. )opertoy, ole by the rae of te nen For example Pointers InC_C Tutorial In Hindi #26 (//codehunteronline/h, J (ptr_dog).nane -refers to the nare of dog (ptr_dog).reed - refers to the breed of dog and so on, Using arrow operator (->) The above method of accessing members of the structure using poles is sight confsng a nat’ why provides another way to acces members using the arrow (-> operator. To acces members using ao rte pointer variable folowesty > operator lowes ty rameaftemember ) RSPAS nercnepo rear1 ptr_dog-snane // refers to the nase of dog 2 prridog->brees // refers to the breed of dog and0 on _/ tere we dont need parenteses, asterisk +) and dot.) operator. This method is much more readable and intuitive We can also mod the vale of members using ponte notation v A streny(otr_dog-rvane, "new_rane"); Here we know that the name of the array (ptr_dg->nane) sa constant pointer and points to the Oth element of the array. Sowe can't assign (2 new string to eusing assignment operator (=) that’s why strcpy() function is used. SY ~ per donate: In the above expression precedence of arrow operator (->) is greater than that of prefix decrement operator ( applied in the expression then its value is decremented by 1 So frst -> operators wa following program demonstrates how we can use a pointer to structure 2 Hincludecstato.h> 2 a 5)” char nane(301s, © char breea(3@)s, 7 ne aes 8 char colori30); 2 21 int nain(y wt 32 struet dog ay_dog = ("tyke", "Bul2éog", 5, “uhite"}; 34 struct cog "yer 15 ptrog = Sry_o. 27 printe(*boq's mane: s\n", ptr_dog-dnare)s 38 printf(*Dog's breed: %e\n", pe_dog-sbreed); 28 printe(*Dog's age: a\a", ote_dog->ase)s, 28 print¢(*DoR's color: s\n", pr_dog->color); 22 // changing the none of dog fron tyke to Jock 2 Steepytptecdog-onane, "Sact")s 2 // Ancreasing oge of dog by 1 year 26 —_ptrsog-vageess 28 printe("Dog's new nane 45: s\n", ptr dog-onane): 29 printe("Dog's age As: d\n", ptr dop->age): 31 // stqnol to operating systen prograw ran fine 52 return 8; a} Expected Output 1 bogs mane: tyke 2 9p's breed: Bulldog 3 ag's ages 5 4 g's color: white After changes 5 og's new nane is: jack 9 op's age is: 6 How it works: a aIn ines 3-9, we have declared a structure of type dog which has four members namely nane, breed, age and color Inline 13,a variable called y_dog oftype struct dog is declared and intialize, Inline 14, pointer variable ptr_dog of ype struct dog is declared Inline 15, the address of ny dog is assigned to ptr dog using & operator. In lines 17-20, the printét) statements prints the details of the dog, In tine 23, anew name is assigned to per_éog using the strepy() function, because we can't assign a string value direc og-ynane Using assignment operator. Inline 26, the value of pte_dog->age Is incremented by 1 using postfix increment operator, Recall that postfix +» operator and -> have the same precedence and associates from left to right. Bu since postfix + Is used in the expression fist the value of ptr_dog-rage is used in the expression then it's value is incremented by 1 € Nested Structures in C Y/c-programming-101/nested-structures.in-/} Pointers as Structure Member in C+ (c-programming-101/pointers-as-structure-member-in-c)
You might also like
Data Structures Made Simple
PDF
100% (6)
Data Structures Made Simple
173 pages
Self Refferential Structure
PDF
No ratings yet
Self Refferential Structure
30 pages
C Pointers Struct Pointer, Function Pointer Made Simple
PDF
91% (11)
C Pointers Struct Pointer, Function Pointer Made Simple
10 pages
Itp Unit-4
PDF
No ratings yet
Itp Unit-4
11 pages
Cheat Sheet
PDF
No ratings yet
Cheat Sheet
3 pages
C - Pointing To Data
PDF
100% (2)
C - Pointing To Data
4 pages
Data Structure 1: Pointer, Function, Structure, Array, Recursive Function Call
PDF
No ratings yet
Data Structure 1: Pointer, Function, Structure, Array, Recursive Function Call
14 pages
Pointers, Strings and Structures
PDF
No ratings yet
Pointers, Strings and Structures
65 pages
DS +apr 2021-2022+
PDF
No ratings yet
DS +apr 2021-2022+
106 pages
08 Pointers
PDF
No ratings yet
08 Pointers
85 pages
Pointers Revisited
PDF
No ratings yet
Pointers Revisited
11 pages
Data Structures With C
PDF
No ratings yet
Data Structures With C
162 pages
Structure and Union in C Language
PDF
No ratings yet
Structure and Union in C Language
14 pages
Lector Notes III IV V
PDF
No ratings yet
Lector Notes III IV V
61 pages
Unit 4 Introduction To Programming-1
PDF
No ratings yet
Unit 4 Introduction To Programming-1
9 pages
Csc211 Lecture 05
PDF
No ratings yet
Csc211 Lecture 05
62 pages
UNIT-3, Programming in C NOTES
PDF
No ratings yet
UNIT-3, Programming in C NOTES
58 pages
Unit 2
PDF
No ratings yet
Unit 2
113 pages
C Arrays and Pointers: - in Java, Pointers Are Easy To Deal With
PDF
No ratings yet
C Arrays and Pointers: - in Java, Pointers Are Easy To Deal With
16 pages
3 of 3
PDF
No ratings yet
3 of 3
57 pages
C Programming - Pointers: Type Variable Name
PDF
No ratings yet
C Programming - Pointers: Type Variable Name
10 pages
C Unit 3
PDF
No ratings yet
C Unit 3
22 pages
C Pointers
PDF
No ratings yet
C Pointers
7 pages
Pointers and Structure
PDF
No ratings yet
Pointers and Structure
37 pages
13 Pointers
PDF
No ratings yet
13 Pointers
49 pages
Pointer
PDF
No ratings yet
Pointer
36 pages
CPPS Unit 3 Pointers
PDF
No ratings yet
CPPS Unit 3 Pointers
39 pages
Pointer
PDF
No ratings yet
Pointer
31 pages
Unit - 4
PDF
No ratings yet
Unit - 4
54 pages
Pointers
PDF
No ratings yet
Pointers
36 pages
C Programming - Pointers
PDF
No ratings yet
C Programming - Pointers
11 pages
Lecture 5
PDF
No ratings yet
Lecture 5
20 pages
Module 1
PDF
No ratings yet
Module 1
25 pages
Pointers
PDF
No ratings yet
Pointers
25 pages
Pointer by Group
PDF
No ratings yet
Pointer by Group
16 pages
Lab Sheet 8
PDF
No ratings yet
Lab Sheet 8
15 pages
PCL Pyq (Unit3)
PDF
No ratings yet
PCL Pyq (Unit3)
25 pages
QB Unit V
PDF
No ratings yet
QB Unit V
10 pages
Day-3 - 4 - C Programming Foundations For DSA
PDF
No ratings yet
Day-3 - 4 - C Programming Foundations For DSA
11 pages
Pointers
PDF
No ratings yet
Pointers
35 pages
Pointers PPS
PDF
No ratings yet
Pointers PPS
15 pages
Chap II. Complex Data Types
PDF
No ratings yet
Chap II. Complex Data Types
24 pages
Unit-Iv C Language Fybsc It
PDF
No ratings yet
Unit-Iv C Language Fybsc It
24 pages
Lecture 6
PDF
No ratings yet
Lecture 6
18 pages
DSA With C Pointer
PDF
No ratings yet
DSA With C Pointer
23 pages
C Programming 20 21 22
PDF
No ratings yet
C Programming 20 21 22
14 pages
0x0e Structures and Typedef
PDF
No ratings yet
0x0e Structures and Typedef
7 pages
14 - Pointers and Structures
PDF
No ratings yet
14 - Pointers and Structures
15 pages
Self Refrential Structure
PDF
No ratings yet
Self Refrential Structure
19 pages
Pointer C
PDF
No ratings yet
Pointer C
21 pages
Pps Using C r20 - Unit-4
PDF
No ratings yet
Pps Using C r20 - Unit-4
9 pages
C Material Part3
PDF
No ratings yet
C Material Part3
23 pages
Unit 4
PDF
No ratings yet
Unit 4
15 pages
Pointers To Structures in C
PDF
No ratings yet
Pointers To Structures in C
5 pages
UNIT 4 C Language Pointers
PDF
No ratings yet
UNIT 4 C Language Pointers
17 pages
Unit V C
PDF
No ratings yet
Unit V C
11 pages
C Structure
PDF
No ratings yet
C Structure
14 pages
Practical No 6
PDF
No ratings yet
Practical No 6
6 pages
Unit 6 C Programming
PDF
No ratings yet
Unit 6 C Programming
8 pages
Concept of Pointer
PDF
No ratings yet
Concept of Pointer
6 pages
Type Def
PDF
No ratings yet
Type Def
5 pages
1 Structure
PDF
No ratings yet
1 Structure
8 pages