0% found this document useful (0 votes)
50 views5 pages

C Class Sep6

The document discusses pointers in C including: 1) Declaring pointers to variables and accessing their values; 2) Pointer arithmetic to access array elements and iterate through arrays; 3) Pointers to pointers (double pointers); 4) Passing pointers to functions; 5) Dynamic memory allocation using pointers; 6) Pointers to structures; 7) Null pointers; and 8) Pointers to functions. Examples are provided for each concept.

Uploaded by

srisribalaji108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views5 pages

C Class Sep6

The document discusses pointers in C including: 1) Declaring pointers to variables and accessing their values; 2) Pointer arithmetic to access array elements and iterate through arrays; 3) Pointers to pointers (double pointers); 4) Passing pointers to functions; 5) Dynamic memory allocation using pointers; 6) Pointers to structures; 7) Null pointers; and 8) Pointers to functions. Examples are provided for each concept.

Uploaded by

srisribalaji108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Sturct Student {

char name[100];
int roll no;
char Section ;
float marks;

}
main()
{
Student students[10];
}

Dynamic Memory management


o Allocation & deallocation
o Book keeping information
o Alias
o Dangling pointer
o Garbage and memory leak

#include <stdio.h>
#include <stdlib.h>

int main() {
// Allocation - Allocating memory for an integer
int *ptr1 = (int *)malloc(sizeof(int));
if (ptr1 == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return 1;
}

// Storing a value in the allocated memory


*ptr1 = 42;

// Bookkeeping - Printing the address and value


printf("Address of ptr1: %p\n", (void *)ptr1);
printf("Value at ptr1: %d\n", *ptr1);

// Alias - Creating another pointer pointing to the same memory


int *ptr2 = ptr1;

// Dangling pointer - Deallocating memory and leaving ptr1 as a dangling


pointer
free(ptr1);
ptr1 = NULL;

// Garbage - Attempting to access memory through a dangling pointer


// This can lead to undefined behavior
// printf("Value at ptr1 (dangling): %d\n", *ptr1); // Uncommenting this line
will lead to undefined behavior

// Preventing memory leak - Freeing memory when it's no longer needed


free(ptr2);
ptr2 = NULL;

return 0;
}
starce ./a.out

o Life and scope

In C, a union is a user-defined data type that


allows you to store different types of data in the same memory
location. Unlike structures (structs), where each member has
its own memory space, unions share the same memory space for all
their members. This means that only one member of a union can hold
a value at any given time. Here's a full code
example that covers the layout, access, and free union in C:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Define a union named "Data" that can hold an integer, float, or string
union Data {
int intValue;
float floatValue;
char stringValue[20];
};

int main() {
// Declare a union variable
union Data myUnion;

// Assign values to the union members


myUnion.intValue = 42;
printf("Integer value: %d\n", myUnion.intValue);

myUnion.floatValue = 3.14;
printf("Float value: %.2f\n", myUnion.floatValue);

strcpy(myUnion.stringValue, "Hello, Union!");


printf("String value: %s\n", myUnion.stringValue);

// Accessing union members after reassignment


printf("After reassignment:\n");
printf("Integer value: %d\n", myUnion.intValue);
printf("Float value: %.2f\n", myUnion.floatValue);
printf("String value: %s\n", myUnion.stringValue);

// Size of the union


printf("Size of the union: %lu bytes\n", sizeof(union Data));

// Freeing memory (not applicable to unions)


// Unions do not dynamically allocate memory like structures, so there's no
need to free them.

return 0;
}

printf("size of stucture is =%d \n",sizeof(struct student));


printf("size of float=%d \n",sizeof(float));
==============

#include <stdio.h>

int main() {
int choice;
int num1, num2;
int result;
char operator;

while (1) {
printf("Menu:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

if (choice == 5) {
printf("Goodbye!\n");
break; // Exit the program
}

if (choice >= 1 && choice <= 4) {


printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
}

switch (choice) {
case 1:
operator = '+';
result = num1 + num2;
break;
case 2:
operator = '-';
result = num1 - num2;
break;
case 3:
operator = '*';
result = num1 * num2;
break;
case 4:
operator = '/';
// Check for division by zero
result = (num2 != 0) ? (num1 / num2) : 0;
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
continue; // Skip the rest of the loop and show the menu again
}

printf("Result: %d %c %d = %d\n", num1, operator, num2, result);


}

return 0;
}
pointer 10 examples

#include <stdio.h>

int main() {
// Example 1: Declaring a pointer to an integer
int num = 42;
int *ptr = &num;
printf("Value of num: %d\n", *ptr);

// Example 2: Modifying the value through a pointer


*ptr = 99;
printf("Modified value of num: %d\n", num);

// Example 3: Pointer arithmetic (with an array)


int arr[5] = {1, 2, 3, 4, 5};
int *arr_ptr = arr;
printf("Value at arr[2]: %d\n", *(arr_ptr + 2));

// Example 4: Iterating through an array using pointers


for (int i = 0; i < 5; i++) {
printf("%d ", *(arr_ptr + i));
}
printf("\n");

// Example 5: Pointers to pointers (double pointers)


int val = 10;
int *ptr1 = &val;
int **ptr2 = &ptr1;
printf("Value through double pointer: %d\n", **ptr2);

// Example 6: Passing pointers to functions


void modifyValue(int *x) {
(*x) *= 2;
}
int value = 7;
modifyValue(&value);
printf("Modified value: %d\n", value);

// Example 7: Dynamic memory allocation


int *dynamic_array = (int *)malloc(3 * sizeof(int));
dynamic_array[0] = 11;
dynamic_array[1] = 22;
dynamic_array[2] = 33;
printf("Dynamic array: %d %d %d\n", dynamic_array[0], dynamic_array[1],
dynamic_array[2]);
free(dynamic_array);

// Example 8: Pointers and structures


struct Point {
int x;
int y;
};
struct Point p1 = {5, 10};
struct Point *point_ptr = &p1;
printf("Accessing structure members through pointers: %d %d\n", point_ptr->x,
point_ptr->y);

// Example 9: Null pointers


int *null_ptr = NULL;
if (null_ptr == NULL) {
printf("Null pointer\n");
}
// Example 10: Pointers to functions
int add(int a, int b) {
return a + b;
}
int (*add_ptr)(int, int) = add;
printf("Function pointer result: %d\n", add_ptr(3, 4));

return 0;
}

You might also like