FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
321 views
in Tutorial & Interview questions by Goeduhub's Expert (9.3k points)

The member access operators (dot . and arrow ->) are used to access a member of a struct.

1 Answer

0 like 0 dislike
by Goeduhub's Expert (9.3k points)
 
Best answer

The member access operators (dot . and arrow ->) are used to access a member of a struct. 

Member of object 

Evaluates into the lvalue denoting the object that is a member of the accessed object.

struct MyStruct 

{   

 int x;    

int y; 

};

struct MyStruct myObject; 

myObject.x = 42; 

myObject.y = 123;

printf(".x = %i, .y = %i\n", myObject.x, myObject.y); /* Outputs ".x = 42, .y = 123". */

Member of pointed-to object

Syntactic sugar for dereferencing followed by member access. Effectively, an expression of the form x->y is shorthand for (*x).y — but the arrow operator is much clearer, especially if the structure pointers are nested.

struct MyStruct 

{    

int x;    

int y;

};

struct MyStruct myObject; 

struct MyStruct *p = &myObject;

p->x = 42; 

p->y = 123;

printf(".x = %i, .y = %i\n", p->x, p->y); /* Outputs ".x = 42, .y = 123". */ 

printf(".x = %i, .y = %i\n", myObject.x, myObject.y); /* Also outputs ".x = 42, .y = 123". */

Address-of

The unary & operator is the address of operator. It evaluates the given expression, where the resulting object must be an lvalue. Then, it evaluates into an object whose type is a pointer to the resulting object's type, and contains the address of the resulting object.

int x = 3; 

int *p = &x; 

printf("%p = %p\n", (void *)&x, (void *)p); /* Outputs "A = A", for some implementation-defined A. */

Dereference

The unary * operator dereferences a pointer. It evaluates into the lvalue resulting from dereferencing the pointer that results from evaluating the given expression.

int x = 42; 

int *p = &x; 

printf("x = %d, *p = %d\n", x, *p); /* Outputs "x = 42, *p = 42". */

*p = 123; 

printf("x = %d, *p = %d\n", x, *p); /* Outputs "x = 123, *p = 123". */

Indexing

Indexing is syntactic sugar for pointer addition followed by dereferencing. Effectively, an expression of the form a[i] is equivalent to *(a + i) — but the explicit subscript notation is preferred.

int arr[] = { 1, 2, 3, 4, 5 }; 

printf("arr[2] = %i\n", arr[2]); /* Outputs "arr[2] = 3". */

Interchangeability of indexing

Adding a pointer to an integer is a commutative operation (i.e. the order of the operands does not change the result) so pointer + integer == integer + pointer.

A consequence of this is that arr[3] and 3[arr] are equivalent.

printf("3[arr] = %i\n", 3[arr]);                /* Outputs "3[arr] = 4". */

Usage of an expression 3[arr] instead of arr[3] is generally not recommended, as it affects code readability. It tends to be a popular in obfuscated programming contests.

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

0 like 0 dislike
1 answer 256 views
0 like 0 dislike
1 answer 249 views
0 like 0 dislike
0 answers 247 views
0 like 0 dislike
1 answer 578 views
0 like 0 dislike
1 answer 320 views

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...