c - I do not understand this stu[en-11] line of code. How exactly does accessing a structure member work? -
#include<stdio.h> #include<conio.h> struct student { int enroll; char name[50]; }stu[2] = { {11, "rj"}, {12, "ay"} }; int main() { int en; printf("enter enroll: "); scanf("%d", &en); if(en==11 || en==12) { printf("%s", stu[en-11].name); printf("\t%d", stu[en-11].enroll); } else printf("wrong"); return 0; }
stu[2]
array of structure should use loop accessing each members of structure, or stu[0].name
, stu[1].name
, in code below can access members using stu[en-11]
. please help. how work?
the correct index stu
in case 0 , 1.
so, en
value has either 11 or 12 valid access. you'll needing or operator ||
instead of , operator &&
.
change
if(en==11 && en==12)
to
if(en==11 || en==12)
coming stu[en-11]
part, you've asked, index value has int
, , expression en-11
produces int
. that's all.
Comments
Post a Comment