-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer_struct.c
More file actions
48 lines (39 loc) · 1.43 KB
/
pointer_struct.c
File metadata and controls
48 lines (39 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
Problem Statement: Exploring struct pointers in C
- Demonstrates struct pointers with and without the arrow (->) operator.
- Assigns values using a normal struct variable (not pointer).
- Prints values using both dot (.) and arrow (->) operators to show identical output.
*/
#include <stdio.h>
#include <string.h>
struct Person {
char name[50];
int age;
float height;
};
int main() {
struct Person p1;
struct Person *ptr;
ptr = &p1;
strcpy(p1.name, "Nirant");
p1.age = 22;
p1.height = 5.9;
printf("Person Details:\n");
printf("Name: %s\n", ptr->name);
printf("Age: %d\n", p1.age);
printf("Height: %.1f feet\n", ptr->height);
return 0;
}
/*
Step-by-step working:
1. Define a struct 'Person' with three members.
2. Declare a struct variable 'p1'.
3. Declare a struct pointer 'ptr' and assign it the address of 'p1'.
4. Assign values directly using p1 (not using pointer).
5. Print values using a mix of p1.member and ptr->member.
6. Output is the same, proving both methods work.
Explanation:
- Assigning values directly to struct variable does not require a pointer.
- The arrow (->) operator accesses struct members via a pointer.
- Mixing both in printf() confirms that both approaches give the same result.
*/