Challenge - 5 Problems
Structure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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;
}Attempts:
2 left
💡 Hint
Remember that '->' accesses members through a pointer, and '(*ptr).member' is equivalent.
✗ Incorrect
The pointer 'ptr' points to 'p'. Accessing 'ptr->x' gives 3 and '(*ptr).y' gives 4, so output is '3 4'.
❓ Predict Output
intermediate2: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;
}Attempts:
2 left
💡 Hint
Check the order of members in the nested structure.
✗ Incorrect
The 'date' member of 'e' is initialized with day=15, month=8, year=2024. So the output is '15/8/2024'.
🔧 Debug
advanced2: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;
}Attempts:
2 left
💡 Hint
Look carefully at the expression '*ptr->name' and operator precedence.
✗ Incorrect
The expression '*ptr->name' tries to dereference a char array which is invalid here. The correct way is 'ptr->name' without '*'.
❓ Predict Output
advanced2: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;
}Attempts:
2 left
💡 Hint
Both 'p->count' and '(*p).count' modify the same member.
✗ Incorrect
Starting from 10, adding 5 then 10 results in 25 printed.
🧠 Conceptual
expert3: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;
}Attempts:
2 left
💡 Hint
Pointer arithmetic moves the pointer; indexing is relative to the new pointer position.
✗ Incorrect
The array 'arr' has 4 elements. After 'ptr++', 'ptr' points to arr[1]. So 'ptr[2]' is arr[3], which has value 4.