Pointers – Let us C https://c.praveshagrawal.com Example C Programs Sat, 02 Dec 2023 11:16:57 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 Pointers to Functions https://c.praveshagrawal.com/2023/12/02/pointers-to-functions/ https://c.praveshagrawal.com/2023/12/02/pointers-to-functions/#respond Sat, 02 Dec 2023 11:16:57 +0000 https://c.praveshagrawal.com/?p=107 #include <stdio.h> // Function that adds two numbers int add(int a, int b) { return a + b; } int main() { // Pointers to functions int (*sum)(int, int); sum = add; printf("Sum: %d\n", sum(3, 5)); return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/pointers-to-functions/feed/ 0 Dynamic Memory Allocation with Pointers https://c.praveshagrawal.com/2023/12/02/dynamic-memory-allocation-with-pointers/ https://c.praveshagrawal.com/2023/12/02/dynamic-memory-allocation-with-pointers/#respond Sat, 02 Dec 2023 11:16:43 +0000 https://c.praveshagrawal.com/?p=108 #include <stdio.h> #include <stdlib.h> int main() { // Dynamic memory allocation with pointers int *dynamicArray; int size; printf("Enter the size of the array: "); scanf("%d", &size); dynamicArray = (int *)malloc(size * sizeof(int)); if (dynamicArray == NULL) { printf("Memory allocation failed.\n"); return 1; } // Use the dynamically allocated array // Free the allocated memory free(dynamicArray); return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/dynamic-memory-allocation-with-pointers/feed/ 0 Pointers and Functions https://c.praveshagrawal.com/2023/12/02/pointers-and-functions/ https://c.praveshagrawal.com/2023/12/02/pointers-and-functions/#respond Sat, 02 Dec 2023 11:16:28 +0000 https://c.praveshagrawal.com/?p=109 #include <stdio.h> // Function that swaps two integers using pointers void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { // Pointers and functions int x = 5, y = 10; printf("Before swapping: x = %d, y = %d\n", x, y); swap(&x, &y); printf("After swapping: x = %d, y = %d\n", x, y); return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/pointers-and-functions/feed/ 0 Pointer Arithmetic https://c.praveshagrawal.com/2023/12/02/pointer-arithmetic/ https://c.praveshagrawal.com/2023/12/02/pointer-arithmetic/#respond Sat, 02 Dec 2023 11:16:13 +0000 https://c.praveshagrawal.com/?p=110 #include <stdio.h> int main() { // Pointer arithmetic int numbers[] = {1, 2, 3, 4, 5}; int *ptr = numbers; printf("First element: %d\n", *ptr); printf("Second element: %d\n", *(ptr + 1)); printf("Third element: %d\n", *(ptr + 2)); return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/pointer-arithmetic/feed/ 0 Simple Pointer Declaration and Use https://c.praveshagrawal.com/2023/12/02/simple-pointer-declaration-and-use/ https://c.praveshagrawal.com/2023/12/02/simple-pointer-declaration-and-use/#respond Sat, 02 Dec 2023 11:15:59 +0000 https://c.praveshagrawal.com/?p=111 #include <stdio.h> int main() { // Simple pointer declaration and use int number = 42; int *ptr = &number; printf("Value of number: %d\n", number); printf("Address of number: %p\n", (void *)&number); printf("Value of pointer (address of number): %p\n", (void *)ptr); printf("Value pointed by pointer: %d\n", *ptr); return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/simple-pointer-declaration-and-use/feed/ 0