Dot operator is used to access(or assign) the values of data members in a structure. It is represented as follows:
structure_variable.data_member = value;
struct student {
char sex;
int rank;
}obj;
- obj is structure variable
- student is structure name or tag
- sex and rank are data members
Let us try to assign values to data members in structure student using dot operator.
obj.sex = 'M'; //'M' is assigned to data member sex
obj.rank = 1; // 1 is assigned to data member rank
Example:
#include <stdio.h>
#include <string.h>
struct student {
char name[100];
int rank;
}obj;
int main() {
strcpy(obj.name, "jp");
obj.rank = 5;
printf("Name:%s\n", obj.name);
printf("Rank:%d\n", obj.rank);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Name:jp
Rank:5
Name:jp
Rank:5
No comments:
Post a Comment