Dot Vs Arrow Operator and Char pointer in Structure C programming tutorial

This C programming tutorial highlights 3 important aspect of structure programming with a single C program. The reason for doing this is to show in minimal and at a glance how these 3 are inter-related. The first thing is to show and highlight the difference between how to use dot operator vs arrow operator to access structure members. Second how to access char pointer members of a structure using dot and arrow operators. And third, to show how to pass the dot and arrow operated members and char pointer members to a function.

See  the following C program code:

We have declared a structure called student_data with properties called age, marks and two pointers to char data type firstname and surname.

In the main function, we have declared two types of structure variable of student_data called student and stu.

The first variable called student is a simple structure variable. This structure variable can be used to access the properties(members) of the structure student_data using the dot operator. Re-shown below is the example:

The second type of variable is stu, which is a pointer variable to the structure student_data. If we want to access the members(properties) of the structure using this pointer variable stu then we have to use the arrow operator. This part of declaration and initialization in the above program code is re-shown below.

Both these types of variables, normal and pointer variables, student and stu, to the structure student_data are then used for accessing pointer to char members firstname and surname. How to do this is shown below.

Finally, the reason for disp() function here is to show how the assigned members of a structure using dot and arrow operators and the char pointer accessed variables can be passed to a function like disp(). The disp() just prints out all the variables to the output display. The following shows this using Code::blocks C programming software.

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

Leave a Reply

Your email address will not be published. Required fields are marked *

Passing Structure Char Pointer to Function in C programming language

Arduino Photoresistor Tutorial