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

1.How can we write a program using pointers?

#include <stdio.h>

#include <stdlib.h>

int main() {

int num = 10; // Declare an integer variable

int *ptr; // Declare a pointer variable

ptr = &num; // Assign the address of 'num' to the pointer variable

// Print the value of 'num' using the pointer

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

// Modify the value of 'num' using the pointer

*ptr = 20;

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

// Dynamic memory allocation

int *dynamicArray;

int size = 5;

dynamicArray = (int *)malloc(size * sizeof(int)); // Allocate memory for an array

if (dynamicArray == NULL) {

printf("Memory allocation failed.\n");

return 1;

// Assign values to the dynamically allocated array

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

dynamicArray[i] = i * 2;
}

// Print the values of the dynamically allocated array

printf("Values of dynamicArray: ");

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

printf("%d ", dynamicArray[i]);

printf("\n");

// Free dynamically allocated memory

free(dynamicArray);

return 0;

2. Similarities:

Data Organization: All three data structures organize and store elements of various data types.

Access: Elements in arrays, records, and linked lists can be accessed using specific methods or
indices.

Data Types: They can store elements of different data types, including integers, floats, characters,
and user-defined types.

Differences:

Memory Allocation:

Arrays: Contiguous memory allocation. All elements in an array are stored in adjacent memory
locations.

Records: Also known as structures, records store elements of different data types under a single
name. The elements within a record may not be contiguous in memory.

Linked Lists: Non-contiguous memory allocation. Linked lists use pointers to connect individual
nodes, allowing them to be scattered throughout memory.

Size Flexibility:
Arrays: Fixed size in most programming languages. Once created, the size of an array cannot be
changed.

Records: The size of a record is determined by the combined sizes of its individual elements and is
usually fixed.

Linked Lists: Dynamic size. Linked lists can grow or shrink during runtime, as nodes are dynamically
allocated or deallocated.

Element Access:

Arrays: Elements are accessed using indices.

Records: Elements are accessed using field names.

Linked Lists: Elements are accessed by traversing the list using pointers.

Insertion and Deletion:

Arrays: Insertion and deletion operations can be inefficient, especially if elements need to be shifted.

Records: Records are typically not used for insertion and deletion operations directly, as they are
usually used to represent fixed structures.

Linked Lists: Efficient for insertion and deletion operations, as they involve updating pointers to add
or remove nodes.

Traversal:

Arrays: Traversal is straightforward using loops and indices.

Records: Traversal is based on accessing individual elements directly.

Linked Lists: Traversal requires following pointers from one node to another until the desired node is
reached.

You might also like