0% found this document useful (0 votes)
6 views

pointer structures

Uploaded by

priyamoviesoff
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)
6 views

pointer structures

Uploaded by

priyamoviesoff
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
You are on page 1/ 5

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

// Deallocate memory

free(ptr);

return 0;

Common Errors:

1. Memory Leaks: Failing to deallocate memory.

2. Dangling Pointers: Pointers that point to deallocated memory.

3. Null Pointer Dereference: Dereferencing a null pointer.

3(a) Define Structure? How to create and access Structure members?

Definition of Structure:

A structure (also known as a struct) is a collection of variables of different data types that are stored
together in memory. It is a user-defined data type that allows you to combine multiple variables into
a single unit.

Creating a Structure:

To create a structure, you use the struct keyword followed by the name of the structure and the
variables that make up the structure.

struct Person {

int age;

char name[20];

float height;

};

In this example, we have created a structure called Person that has three variables: age, name, and
height.

Declaring Structure Variables:

To use a structure, you need to declare variables of that structure type:

struct Person person1, person2;

Accessing Structure Members:

To access the members of a structure, you use the dot (.) operator.

person1.age = 25;

strcpy(person1.name, "John");
person1.height = 175.5;

printf("Age: %d\n", person1.age);

printf("Name: %s\n", person1.name);

printf("Height: %.2f\n", person1.height);

In this example, we are accessing the members of the person1 structure variable using the dot
operator.

Using Pointers to Access Structure Members:

You can also use pointers to access structure members.

struct Person *ptr = &person1;

(*ptr).age = 25; // or ptr->age = 25;

strcpy((*ptr).name, "John"); // or strcpy(ptr->name, "John");

(*ptr).height = 175.5; // or ptr->height = 175.5;

In this example, we are using a pointer ptr to access the members of the person1 structure variable.

Array of Structures:

You can also create an array of structures.

struct Person persons[5];

In this example, we have created an array of 5 Person structures.

Accessing Array of Structure Members:

To access the members of an array of structures, you use the array index and the dot operator.

persons[0].age = 25;

strcpy(persons[0].name, "John");

persons[0].height = 175.5;

printf("Age: %d\n", persons[0].age);

printf("Name: %s\n", persons[0].name);

printf("Height: %.2f\n", persons[0].height);

In this example, we are accessing the members of the first element of the persons array.

3(b) Explain the concept of Array of Structures?

Array of Structures:

An array of structures is a collection of structures of the same type stored in contiguous memory
locations. Each element of the array is a structure, and each structure can have multiple members.

Declaring an Array of Structures:


The syntax to declare an array of structures is:

struct structure_name array_name[size];

Example:

struct Student {

int roll_no;

char name[20];

float marks;

};

struct Student students[5];

In this example, we have declared an array of 5 structures of type Student.

Initializing an Array of Structures:

You can initialize an array of structures using the following syntax:

struct Student students[5] = {

{1, "John", 85.5},

{2, "Alice", 90.0},

{3, "Bob", 78.2},

{4, "Charlie", 92.1},

{5, "David", 88.3}

};

Accessing Members of an Array of Structures:

To access the members of an array of structures, you use the array index and the dot operator.

Example:

printf("Roll No: %d\n", students[0].roll_no);

printf("Name: %s\n", students[0].name);

printf("Marks: %.2f\n", students[0].marks);

In this example, we are accessing the members of the first element of the students array.

Advantages of Array of Structures:

1. Efficient memory usage: Array of structures allows for efficient memory usage, as multiple
structures are stored in contiguous memory locations.

2. Easy access: Members of an array of structures can be easily accessed using the array index and
dot operator.
3. Simplified code: Array of structures can simplify code, as you can perform operations on multiple
structures using a single loop.

4(a) Explain the concept of Structure pointer?

Structure Pointer:

A structure pointer is a pointer variable that points to a structure. It is a variable that holds the
memory address of a structure.

Declaring a Structure Pointer:

The syntax to declare a structure pointer is:

struct structure_name *pointer_name;

Example:

struct Student {

int roll_no;

char name[20];

float marks;

};

struct Student *ptr;

In this example, we have declared a structure pointer ptr that points to a structure of type Student.

Initializing a Structure Pointer:

You can initialize a structure pointer by assigning it the address of a structure.

Example:

struct Student student = {1, "John", 85.5};

struct Student *ptr = &student;

In this example, we have initialized the structure pointer ptr with the address of the structure
variable student.

Accessing Structure Members using a Structure Pointer:

To access the members of a structure using a structure pointer, you use the arrow operator (->).

Example:

printf("Roll No: %d\n", ptr->roll_no);

printf("Name: %s\n", ptr->name);

printf("Marks: %.2f\n", ptr->marks);


In this example, we are accessing the members of the structure pointed to by ptr using the arrow
operator.

Dereferencing a Structure Pointer:

You can dereference a structure pointer using the dereference operator (*).

Example:

(*ptr).roll_no = 2;

strcpy((*ptr).name, "Alice");

(*ptr).marks = 90.0;

In this example, we are dereferencing the structure pointer ptr and accessing its members using the
dot operator.

Advantages of Structure Pointers:

1. Efficient memory usage: Structure pointers allow for efficient memory usage, as you can pass
structures to functions without copying the entire structure.

2. Improved code readability: Structure pointers can improve code readability, as you can use the
arrow operator to access structure members.

3. Simplified code: Structure pointers can simplify code, as you can perform operations on structures
using a single pointer.

4 (b) Discuss about union?


Unions:

A union is a special data type in C that allows storing different types of data in the same memory
location. It is a collection of variables of different data types that share the same memory space.

Declaring a Union:

The syntax to declare a union is:

union union_name {

data_type member1;

data_type member2;

...

data_type memberN;

};

Example:

union Data {

int i;

You might also like