Background Theory - Structure

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Background theory – Structure, pointer and

File handling
Q1. What is structure? Explain with examples

In C programming, a structure is a user-


defined data type that allows you to group
together variables of different data types
under a single name. Each variable within the
structure is called a member or field. The
idea is to organize related information into a
meaningful and logical structure.
Here's an example of a structure in C:
#include <stdio.h>

// Define a structure named 'Person'


struct Person {
char name[50];
int age;
float height;
};

int main() {
// Declare a variable of type 'struct
Person'
struct Person person1;

// Access and modify the members of the


structure
strcpy(person1.name, "John Doe");
person1.age = 25;
person1.height = 175.5;

// Display the information


printf("Person's name: %s\n",
person1.name);
printf("Person's age: %d\n", person1.age);
printf("Person's height: %.2f\n",
person1.height);

return 0;
}

Q2. Explain pointer with their operators.


A pointer is a variable that stores the memory
address of another variable. Pointers in C are
powerful but require careful handling to avoid
errors. There are several operators associated
with pointers:

Address-of operator (&): Obtains the memory


address of a variable.
int num = 10;
int *ptr = &num; // 'ptr' now holds the
address of 'num'

Dereference operator (*): Accesses the value


stored at the memory address pointed to by a
pointer.
printf("Value of 'num': %d\n", *ptr); //
Prints the value at the address stored in
'ptr'

Pointer arithmetic: Allows manipulation of


pointer values using arithmetic operations.
int arr[] = {1, 2, 3, 4, 5};
int *p = arr; // 'p' points to the first
element of 'arr'

printf("Value at 'p': %d\n", *p); // Prints 1


p++; // Moves 'p' to the next element
printf("Value at 'p' after increment: %d\n",
*p); // Prints 2

Q3. What is file handling in C? Explain


different file handling functions with syntax
and examples.
File handling in C involves operations on
files, such as reading from or writing to
them. The standard I/O library provides
functions for file handling. Here are some
commonly used file handling functions:

fopen: Opens a file.

FILE *fptr;
fptr = fopen("example.txt", "w");

fclose: Closes a file.


fclose(fptr);

fprintf and fscanf: Used for writing and


reading formatted data to/from a file.
fprintf(fptr, "Hello, %s!\n", "world");
fscanf(fptr, "%s", buffer);
Q4. Write differences between Structure and
Union.
Structures and unions are both used to group
variables of different data types, but they
have some key differences:

Memory Allocation:

In a structure, each member has its own memory


space, and the total memory allocated is the
sum of the sizes of all members.
In a union, all members share the same memory
space. The size of a union is the size of its
largest member.

Accessing Members:

In a structure, you can access each member


independently.
In a union, only one member can be accessed at
a time, and accessing a different member
changes the content of the union.

Memory Usage:

Structures are used when you want to group


related but distinct pieces of information.
Unions are useful when you want to save memory
by sharing the same memory space for different
members, and only one member is active at a
time.

You might also like