0% found this document useful (0 votes)
8 views10 pages

Student Structures and Pointers in C

The document presents a series of programming problems related to structures and pointers in C. It includes examples of creating structures to store student information, initializing structures, copying and comparing structures, and using pointers to manipulate data. Each problem is accompanied by code snippets and sample outputs to illustrate the concepts.

Uploaded by

ankitag1108
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views10 pages

Student Structures and Pointers in C

The document presents a series of programming problems related to structures and pointers in C. It includes examples of creating structures to store student information, initializing structures, copying and comparing structures, and using pointers to manipulate data. Each problem is accompanied by code snippets and sample outputs to illustrate the concepts.

Uploaded by

ankitag1108
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Structures and Pointers

Problem 1: Storing Student Information

Problem Statement: Create a structure Student to store a student's roll number,


name (up to 50 characters), and marks. Write a program to take input for one
student and display their information.
Code:

#include <stdio.h>
#include <string.h>

struct Student {
int rollno;
char name[50];
float marks;
};

int main() {
struct Student s1;

printf("Enter student roll number: ");


scanf("%d", &[Link]);

printf("Enter student name: ");


scanf("%s", [Link]); // Note: scanf stops at whitespace. For names with
spaces, use fgets.

printf("Enter student marks: ");


scanf("%f", &[Link]);

printf("\n--- Student Information ---\n");


printf("Roll Number: %d\n", [Link]);
printf("Name: %s\n", [Link]);
printf("Marks: %.2f\n", [Link]);

return 0;
}

Output:

Enter student roll number: 101


Enter student name: Alice
Enter student marks: 85.5

--- Student Information ---


Roll Number: 101
Name: Alice
Marks: 85.50
Problem 2: Array of Structures for Multiple Students

Problem Statement: Extend the previous problem to store information for 3


students using an array of Student structures. Display the information for all
students.
Code:

#include <stdio.h>
#include <string.h>

struct Student {
int rollno;
char name[50];
float marks;
};

int main() {
struct Student students[3];

for (int i = 0; i < 3; i++) {


printf("Enter details for student %d:\n", i + 1);
printf("Roll Number: ");
scanf("%d", &students[i].rollno);
printf("Name: ");
scanf("%s", students[i].name);
printf("Marks: ");
scanf("%f", &students[i].marks);
printf("\n");
}

printf("--- All Student Information ---\n");


for (int i = 0; i < 3; i++) {
printf("Student %d:\n", i + 1);
printf("Roll Number: %d\n", students[i].rollno);
printf("Name: %s\n", students[i].name);
printf("Marks: %.2f\n", students[i].marks);
printf("\n");
}

return 0;
}

Output:

Enter details for student 1:


Roll Number: 101
Name: Alice
Marks: 85.5

Enter details for student 2:


Roll Number: 102
Name: Bob
Marks: 78.0

Enter details for student 3:


Roll Number: 103
Name: Charlie
Marks: 92.5
--- All Student Information ---
Student 1:
Roll Number: 101
Name: Alice
Marks: 85.50

Student 2:
Roll Number: 102
Name: Bob
Marks: 78.00

Student 3:
Roll Number: 103
Name: Charlie
Marks: 92.50

Problem 3: Structure Initialization

Problem Statement: Define a structure Point with x and y coordinates. Initialize a


Point variable with values (10, 20) and print its coordinates.

Code:

#include <stdio.h>

struct Point {
int x;
int y;
};

int main() {
struct Point p1 = {10, 20}; // Initialization

printf("Point coordinates: (%d, %d)\n", p1.x, p1.y);

return 0;
}

Output:

Point coordinates: (10, 20)


Problem 4: Copying and Comparing Structures

Problem Statement: Create two Point structures. Initialize the first one with (5,
15). Copy the contents of the first structure to the second. Then, compare if the two
structures are identical and print the result.
Code:

#include <stdio.h>

struct Point {
int x;
int y;
};

int main() {
struct Point p1 = {5, 15};
struct Point p2;

// Copying structure
p2 = p1;

printf("P1: (%d, %d)\n", p1.x, p1.y);


printf("P2: (%d, %d)\n", p2.x, p2.y);

// Comparing structures
if (p1.x == p2.x && p1.y == p2.y) {
printf("Structures p1 and p2 are identical.\n");
} else {
printf("Structures p1 and p2 are different.\n");
}

return 0;
}

Output:

P1: (5, 15)


P2: (5, 15)
Structures p1 and p2 are identical.
Problem 5: Array within a Structure

Problem Statement: Create a structure SubjectMarks that contains an array of


integers to store marks for 5 subjects. Write a program to take marks for 5 subjects
for one student and calculate the total marks.
Code:

#include <stdio.h>

struct SubjectMarks {
int marks[5];
};

int main() {
struct SubjectMarks sm1;
int totalMarks = 0;

printf("Enter marks for 5 subjects:\n");


for (int i = 0; i < 5; i++) {
printf("Subject %d: ", i + 1);
scanf("%d", &[Link][i]);
totalMarks += [Link][i];
}

printf("\nTotal marks: %d\n", totalMarks);

return 0;
}

Output:

Enter marks for 5 subjects:


Subject 1: 80
Subject 2: 75
Subject 3: 90
Subject 4: 88
Subject 5: 95

Total marks: 428


Pointers

Problem 6: Understanding Pointers and Addresses

Problem Statement: Declare an integer variable, assign it a value, and then


declare a pointer variable. Store the address of the integer variable in the pointer.
Print both the value of the integer variable and the value stored in the pointer
(which is the address).

Code:

#include <stdio.h>

int main() {
int num = 25;
int *ptr; // Declare a pointer to an integer

ptr = &num; // Store the address of 'num' in 'ptr'

printf("Value of num: %d\n", num);


printf("Address of num: %p\n", &num); // %p is for printing addresses
printf("Value of ptr (address of num): %p\n", ptr);
printf("Value accessed through ptr (dereferencing): %d\n", *ptr); //
Dereferencing to get the value

return 0;
}

Output:

Value of num: 25
Address of num: 0x7ffee6e8a7e8 // This address will vary each time you run the
program
Value of ptr (address of num): 0x7ffee6e8a7e8
Value accessed through ptr (dereferencing): 25
Problem 7: Modifying Variable through Pointer

Problem Statement: Declare an integer variable and a pointer to it. Initialize the
variable. Then, use the pointer to change the value of the variable and print the new
value.

Code:

#include <stdio.h>

int main() {
int counter = 10;
int *pCounter;

pCounter = &counter;

printf("Initial value of counter: %d\n", counter);

// Modify the value of counter using the pointer


*pCounter = 20;

printf("New value of counter (accessed directly): %d\n", counter);


printf("New value of counter (accessed through pointer): %d\n", *pCounter);

return 0;
}

Output:

Initial value of counter: 10


New value of counter (accessed directly): 20
New value of counter (accessed through pointer): 20
Problem 8: Pointer to a Character

Problem Statement: Declare a character variable, assign it a character, and then


declare a pointer to a character. Store the address of the character variable in the
pointer and print the character using the pointer.

Code:

#include <stdio.h>

int main() {
char initial = 'J';
char *pInitial;

pInitial = &initial;

printf("The initial is: %c\n", *pInitial);

return 0;
}

Output:

The initial is: J


Problem 9: Pointer Arithmetic with Integers

Problem Statement: Declare an integer array. Declare a pointer and initialize it to


point to the first element of the array. Use pointer arithmetic to access and print the
second and third elements of the array.

Code:

#include <stdio.h>

int main() {
int numbers[] = {10, 20, 30, 40, 50};
int *ptr;

ptr = numbers; // 'numbers' itself is the address of the first element

printf("First element: %d\n", *ptr);

// Move pointer to the next element


ptr++;
printf("Second element: %d\n", *ptr);

// Move pointer again


ptr++;
printf("Third element: %d\n", *ptr);

return 0;
}

Output:

First element: 10
Second element: 20
Third element: 30
Problem 10: Using Pointers with Structures

Problem Statement: Define a structure Book with title (string) and price (float).
Declare a Book variable, initialize it. Then, declare a pointer to Book and make it point
to the Book variable. Use the pointer to access and print the title and price of the
book.
Code:

#include <stdio.h>
#include <string.h>

struct Book {
char title[100];
float price;
};

int main() {
struct Book b1 = {"The Great Gatsby", 15.99};
struct Book *pBook; // Pointer to a structure

pBook = &b1; // Make the pointer point to the structure variable

// Accessing members using the pointer


// Option 1: Using -> operator (preferred)
printf("Book Title (using ->): %s\n", pBook->title);
printf("Book Price (using ->): %.2f\n", pBook->price);

// Option 2: Using dereference and dot operator (less common for structures)
printf("Book Title (using * and .): %s\n", (*pBook).title);
printf("Book Price (using * and .): %.2f\n", (*pBook).price);

return 0;
}

Output:

Book Title (using ->): The Great Gatsby


Book Price (using ->): 15.99
Book Title (using * and .): The Great Gatsby
Book Price (using * and .): 15.99

You might also like