0
0
Cprogramming~20 mins

Accessing structure members - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Structure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of accessing structure members with pointers
What is the output of this C code snippet?
C
#include <stdio.h>

struct Point {
    int x;
    int y;
};

int main() {
    struct Point p = {3, 4};
    struct Point *ptr = &p;
    printf("%d %d\n", ptr->x, (*ptr).y);
    return 0;
}
A3 4
B4 3
C0 0
DCompilation error
Attempts:
2 left
💡 Hint
Remember that '->' accesses members through a pointer, and '(*ptr).member' is equivalent.
Predict Output
intermediate
2:00remaining
Output when accessing nested structure members
What will this program print?
C
#include <stdio.h>

struct Date {
    int day;
    int month;
    int year;
};

struct Event {
    char name[20];
    struct Date date;
};

int main() {
    struct Event e = {"Meeting", {15, 8, 2024}};
    printf("%d/%d/%d\n", e.date.day, e.date.month, e.date.year);
    return 0;
}
ACompilation error
B15/8/2024
C2024/8/15
D8/15/2024
Attempts:
2 left
💡 Hint
Check the order of members in the nested structure.
🔧 Debug
advanced
2:00remaining
Identify the error in accessing structure members
What error does this code produce when compiled?
C
#include <stdio.h>

struct Person {
    char name[10];
    int age;
};

int main() {
    struct Person p = {"Alice", 30};
    struct Person *ptr = &p;
    printf("Name: %s, Age: %d\n", *ptr->name, ptr->age);
    return 0;
}
ACompilation error: invalid operands to binary ->
BOutput: Name: Alice, Age: 30
CRuntime error: segmentation fault
DCompilation error: invalid type argument of unary '*'
Attempts:
2 left
💡 Hint
Look carefully at the expression '*ptr->name' and operator precedence.
Predict Output
advanced
2:00remaining
Output of structure member modification via pointer
What is the output of this program?
C
#include <stdio.h>

struct Counter {
    int count;
};

int main() {
    struct Counter c = {10};
    struct Counter *p = &c;
    p->count += 5;
    (*p).count += 10;
    printf("%d\n", c.count);
    return 0;
}
A25
B15
C10
DCompilation error
Attempts:
2 left
💡 Hint
Both 'p->count' and '(*p).count' modify the same member.
🧠 Conceptual
expert
3:00remaining
Number of items in an array of structures after pointer manipulation
Given the code below, how many elements does the array 'arr' have, and what is the value of 'ptr[2].value'?
C
#include <stdio.h>

struct Item {
    int value;
};

int main() {
    struct Item arr[4] = {{1}, {2}, {3}, {4}};
    struct Item *ptr = arr;
    ptr++;
    printf("%d\n", ptr[2].value);
    return 0;
}
AArray has 4 elements; output is 3
BArray has 3 elements; output is 3
CArray has 4 elements; output is 4
DArray has 3 elements; output is 4
Attempts:
2 left
💡 Hint
Pointer arithmetic moves the pointer; indexing is relative to the new pointer position.