Vaibhav Prasad 2K22CE142 Assignment 1
Vaibhav Prasad 2K22CE142 Assignment 1
Aim
To revise the useful concepts of programming prerequisites, including Structures, Unions,
Pointers, and 1D Arrays.
Theory
Structures
A structure in C/C++ is a user-defined data type that allows combining data items of
different kinds. It is used to group related variables.
usage: Structures are used to group related variables, like combining a student’s name, age,
and grades into a single data type.
Example Syntax:
struct Student {
char name[50];
int age;
float marks;
};
Revelent code
struct Student {
char name[50];
int age;
float marks;
};
int main() {
struct Student s1 = {"John", 20, 85.5};
printf("Name: %s\nAge: %d\nMarks: %.2f\n", s1.name, s1.age, s1.marks);
return 0;
}
Unions
A union allows storing different data types in the same memory location. Only one member
can hold a value at any time.
Difference from Structures: In a union, all members share the same memory location, so only
one member can hold a value at any time.
Example Syntax:
union Data {
int i;
float f;
char str[20];
};
Revelent code
union Data { int i;
float f;
char str[20]
Example 2
union car
{
char name[50];
int price;
};
int main()
{
union car car1, car2, *car3;
return 0;
}
Pointers
Pointers store memory addresses of other variables. They are crucial for dynamic memory
allocation and efficient function handling
Usage: They are crucial for dynamic memory allocation, arrays, and handling functions more
efficiently.
Example Syntax:
int var = 10; int *ptr = &var;
// 'ptr' now holds the address of 'var'
Revelent code
int main() {
int var = 30;
int *ptr;
ptr = &var;
printf("Value of var: %d\n", var);
printf("Address of var: %p\n", ptr);
printf("Value at ptr: %d\n", *ptr);
return 0;
}
Example 2: Working of
C++Pointers
#include <iostream>
using namespace std;
int main() {
int var = 5;
return 0;
}
Example 3: Changing Value
Pointed by Pointers
#include <iostream>
using namespace std;
int main() {
int var = 5;
// print var
cout << "var = " << var << endl;
// print *point_var
cout << "*point_var = " <<
*point_var << endl
<< endl;
// print var
cout << "var = " << var << endl;
// print *point_var
cout << "*point_var = " <<
*point_var << endl
<< endl;
// print var
cout << "var = " << var << endl;
// print *point_var
cout << "*point_var = " <<
*point_var << endl;
return 0;
}
1D Arrays
A 1D array is a collection of elements stored in contiguous memory. Arrays allow storing
multiple items of the same type under one name. Array is a linear data structure where all
elements are arranged sequentially. It is a collection of elements of same data type stored at
contiguous memory locations.
Usage: Arrays allow storing multiple items of the same type under a single name and accessing
them using an index.
Example Syntax:
data_type array_name[Size_of_array];
Example
int arr[5];
Relevent code
// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array
#include <stdio.h>
int main() {
int values[5];
Examole 2:
#include <stdio.h>
int main() {
return 0;
}
#include <iostream>
using namespace std;
int main() {
return 0;
}
#include <iostream>
using namespace std;
int main() {
int numbers[5];
return 0;
}