0% found this document useful (0 votes)
10 views4 pages

Accesing Structure Using Pointer

Pointers

Uploaded by

apc78044
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
0% found this document useful (0 votes)
10 views4 pages

Accesing Structure Using Pointer

Pointers

Uploaded by

apc78044
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
You are on page 1/ 4
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 rear 1 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 a In 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