A structure in C is a user-defined data type that groups different variables under one name, allowing you to store related data together.
Click to reveal answer
beginner
How do you access a member of a structure variable in C?
You use the dot operator (.) to access a member of a structure variable. For example, if person is a structure variable and age is a member, you write person.age.
Click to reveal answer
beginner
How do you access a member of a structure through a pointer in C?
You use the arrow operator (->) to access a member of a structure through a pointer. For example, if p is a pointer to a structure and name is a member, you write p->name.
Click to reveal answer
intermediate
What is the difference between the dot (.) and arrow (->) operators in C structures?
The dot operator (.) is used to access members of a structure variable directly, while the arrow operator (->) is used to access members through a pointer to a structure.
Click to reveal answer
beginner
Given struct Point { int x; int y; } p;, how do you set the x coordinate to 10?
You write p.x = 10; to set the x coordinate of the structure variable p to 10.
Click to reveal answer
Which operator is used to access a member of a structure variable in C?
A. (dot)
B-> (arrow)
C* (asterisk)
D& (ampersand)
✗ Incorrect
The dot operator (.) is used to access members of a structure variable directly.
How do you access a structure member through a pointer in C?
AUsing the asterisk (*)
BUsing the dot operator (.)
CUsing the arrow operator (->)
DUsing the ampersand (&)
✗ Incorrect
The arrow operator (->) is used to access members through a pointer to a structure.
Given struct Person { int age; } *ptr;, how do you access the age member?
Aptr.age
B&ptr.age
C*ptr.age
Dptr->age
✗ Incorrect
You use ptr->age to access the age member through the pointer.
What will happen if you try to use the dot operator on a pointer to a structure?
AIt works fine
BIt causes a compilation error
CIt accesses the member correctly
DIt dereferences the pointer automatically
✗ Incorrect
Using the dot operator on a pointer to a structure causes a compilation error because the pointer must be dereferenced first or use the arrow operator.
If p is a structure variable, which of these is correct to assign 5 to member val?
Ap.val = 5;
B*p.val = 5;
Cp->val = 5;
D&p.val = 5;
✗ Incorrect
You use the dot operator to assign a value to a member of a structure variable.
Explain how to access members of a structure variable and a pointer to a structure in C.
Think about how you access properties of an object directly versus through a pointer.
You got /3 concepts.
Describe the difference between the dot (.) and arrow (->) operators when working with structures in C.
Consider what happens when you have a pointer versus a normal variable.
You got /3 concepts.
Practice
(1/5)
1. Which operator is used to access members of a structure variable in C?
easy
A. Colon operator (:)
B. Arrow operator (->)
C. Double colon (::)
D. Dot operator (.)
Solution
Step 1: Understand structure member access
In C, when you have a structure variable, you use the dot operator (.) to access its members.
Step 2: Differentiate from pointer access
The arrow operator (->) is used when you have a pointer to a structure, not the structure variable itself.
Final Answer:
Dot operator (.) -> Option D
Quick Check:
Structure variable member access = dot operator (.) [OK]
Hint: Use dot (.) for struct variables, arrow (->) for pointers [OK]
Common Mistakes:
Confusing dot (.) with arrow (->) operator
Using colon (:) which is invalid in C
Using double colon (::) which is from other languages
2. Given the structure struct Point { int x; int y; } p;, which is the correct way to assign 10 to member x?
easy
A. p->x = 10;
B. p:x = 10;
C. p.x = 10;
D. p[x] = 10;
Solution
Step 1: Identify the variable type
Here, p is a structure variable, not a pointer.
Step 2: Use correct member access syntax
For structure variables, use the dot operator (.) to access members, so p.x = 10; is correct.
Final Answer:
p.x = 10; -> Option C
Quick Check:
Structure variable assignment uses dot (.) [OK]
Hint: Use dot (.) for struct variables, arrow (->) only for pointers [OK]
Common Mistakes:
Using arrow (->) with non-pointer variable
Using colon (:) which is invalid syntax
Using array-like syntax on struct members
3. What is the output of this code?
struct Person {
char name[10];
int age;
};
int main() {
struct Person p = {"Amy", 25};
printf("%s %d", p.name, p.age);
return 0;
}
medium
A. Amy 25
B. Amy 0
C. Error: cannot assign string to array
D. Random garbage values
Solution
Step 1: Understand structure initialization
The structure p is initialized with "Amy" for name and 25 for age.
Step 2: Print structure members
The printf prints p.name as a string and p.age as an integer, so output is Amy 25.
Final Answer:
Amy 25 -> Option A
Quick Check:
Struct initialized correctly, prints members [OK]
Hint: Initialize structs with braces; access members with dot [OK]
Common Mistakes:
Thinking string assignment to array is invalid here
Confusing pointer and array initialization
Expecting syntax error for string in struct
4. Identify the error in this code snippet:
struct Data {
int id;
float value;
};
int main() {
struct Data d;
d->id = 5;
d.value = 3.14;
return 0;
}
medium
A. Using arrow operator (->) with structure variable
B. Missing semicolon after struct definition
C. Incorrect float assignment
D. No error, code is correct
Solution
Step 1: Check member access operators
The variable d is a structure variable, not a pointer, so d->id is incorrect.
Step 2: Correct operator usage
Use dot operator (.) for structure variables: d.id = 5; is correct.
Final Answer:
Using arrow operator (->) with structure variable -> Option A
Quick Check:
Arrow (->) only for pointers, dot (.) for variables [OK]
Hint: Use dot (.) for struct variables, arrow (->) only for pointers [OK]