Pointer, Structure and Union

 

1. Simple Pointer Example

This program demonstrates the basic usage of a pointer.

#include <stdio.h>
int main() { int x = 10; int *p; // Declaration of pointer p = &x; // Assign the address of x to pointer p printf("Value of x: %d\n", x); printf("Address of x: %p\n", &x); printf("Value of p (Address stored in p): %p\n", p); printf("Value at address p (Value of *p): %d\n", *p); return 0; }

2. Pointer to Array

This program shows how a pointer can be used to access elements of an array.


#include <stdio.h> int main() { int arr[5] = {10, 20, 30, 40, 50}; int *p; p = arr; // p now points to the first element of the array for(int i = 0; i < 5; i++) { printf("Element %d: %d\n", i+1, *(p + i)); } return 0; }

3. Pointer to Pointer

This example demonstrates a pointer to a pointer.


#include <stdio.h> int main() { int x = 100; int *p; int **pp; p = &x; // Pointer p stores the address of x pp = &p; // Pointer to pointer pp stores the address of p printf("Value of x: %d\n", x); printf("Value at p (Value of *p): %d\n", *p); printf("Value at pp (Value of **pp): %d\n", **pp); return 0; }

4. Pointer Arithmetic

This program demonstrates pointer arithmetic.


#include <stdio.h> int main() { int arr[3] = {1, 2, 3}; int *p; p = arr; // p now points to the first element of the array printf("Address of p: %p, Value at p: %d\n", p, *p); p++; printf("Address after p++: %p, Value at new p: %d\n", p, *p); p--; printf("Address after p--: %p, Value at new p: %d\n", p, *p); return 0; }

5. Function Pointers

This example shows how to use pointers to functions.


#include <stdio.h> void displayMessage() { printf("Hello, World!\n"); } int main() { void (*funcPtr)(); // Declaration of a function pointer funcPtr = displayMessage; // Assign address of the function // Call the function using the pointer (*funcPtr)(); return 0; }



Structure in C

A structure in C is a user-defined data type that allows grouping variables of

different types under a single name.

Example Code: Structure


#include <stdio.h> // Defining a structure struct Student { int rollNumber; char name[50]; float marks; }; int main() { // Declaring a structure variable struct Student student1; // Assigning values to the structure members student1.rollNumber = 1; strcpy(student1.name, "John Doe"); student1.marks = 85.5; // Accessing structure members printf("Student Roll Number: %d\n", student1.rollNumber); printf("Student Name: %s\n", student1.name); printf("Student Marks: %.2f\n", student1.marks); return 0; }

Union in C

A union in C is similar to a structure, but it allows storing different

data types in the same memory location. This means only one member can contain

a value at any given time.

Example Code: Union


#include <stdio.h> // Defining a union union Data { int i; float f; char str[20]; }; int main() { // Declaring a union variable union Data data; // Assigning and accessing integer value data.i = 10; printf("data.i: %d\n", data.i); // Assigning and accessing float value data.f = 220.5; printf("data.f: %.2f\n", data.f); // Assigning and accessing string value strcpy(data.str, "C Programming"); printf("data.str: %s\n", data.str); // Notice that the value of `data.i` and `data.f` has been overwritten printf("data.i after str assignment: %d\n", data.i); printf("data.f after str assignment: %.2f\n", data.f); return 0; }

Key Differences Between Structure and Union:

Memory Allocation: A structure allocates memory for all its members,
whereas a union allocates memory equal to the size of its largest member.
Data Storage: All members of a structure can store data simultaneously,
but only one member of a union can store data at a time.


Comments