c Programming
c Programming
Pointer Syntax:
data_type *pointer_name;
data_type: The type of data the pointer will point to (like int, char, etc.).
*: Indicates that it's a pointer.
pointer_name: The name of the pointer.
Example:
int num = 5;
int *ptr;
ptr = # // Store the address of 'num' in the pointer 'ptr'
Here, num is an integer, and ptr is a pointer to an integer. &num gives the
address of the variable num.
In C programming, the indirection operator (*) is used with pointers to access the value
stored at the memory address the pointer is pointing to.
1. Pointer Declaration: A pointer holds the memory address of a variable, rather than
the value itself.
2. Indirection Operator (*): When applied to a pointer, it "dereferences" the pointer,
meaning it accesses the value at the memory location the pointer is pointing to.
Syntax:
*pointer: This is used to get the value stored at the memory address the pointer is pointing to.
Example:
#include <stdio.h>
int main() {
int num = 10; // Normal integer variable
int *ptr = # // Pointer variable that holds the address of num
// Using the indirection operator to access the value of num via ptr
printf("Value of num: %d\n", num); // Direct access
printf("Value of num through pointer: %d\n", *ptr); // Indirect access using pointer
return 0;
}
data_type array_name[array_size];
Example:
SORTING ARRAY
In C programming, sorting an array refers to arranging the elements of an array
in a particular order (either ascending or descending). The most common sorting
algorithms include Bubble Sort, Selection Sort, Insertion Sort, and more
complex algorithms like Merge Sort and Quick Sort.
You can use pointer arithmetic to access array elements:
#include <stdio.h>
int main() {
int arr[3] = {10, 20, 30};
int *ptr = arr;
return 0;
}
Arrays are always passed to functions as pointers. This means when you pass an
array to a function, you are passing the address of the first element.
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30};
display(arr, 3); // Passing array to function
return 0;
}
In C, you can allocate memory at runtime using pointers. This is done using
functions like malloc(), calloc(), realloc(), and freeing memory using free().
Example (malloc):
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n = 5;
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
By understanding pointers and arrays, you can manage memory efficiently and
perform tasks like dynamic memory allocation and passing large arrays to
functions.
Dynamic Memory Allocation in C
Dynamic memory allocation in C allows you to allocate memory during the
runtime of your program, as opposed to static memory allocation, where memory
is allocated before the program begins execution. This is especially useful when
you don’t know the amount of memory you’ll need in advance or when the memory
requirements change during the program's execution.
malloc()
calloc()
realloc()
free()
Syntax:
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n = 5;
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
Syntax:
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n = 5;
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
Here, calloc() allocates memory for 5 integers and initializes them to zero.
3. realloc() (Reallocation)
Syntax:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n = 5;
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
if (arr == NULL) {
printf("Memory reallocation failed!\n");
return 1;
}
In this example, we initially allocate memory for 5 integers. Then, we resize the
memory block to accommodate 10 integers using realloc().
The free() function deallocates the memory that was previously allocated using
malloc(), calloc(), or realloc(). After calling free(), the pointer no longer points
to valid memory, and it should not be accessed again. It's good practice to set
the pointer to NULL after freeing the memory.
Syntax:
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n = 5;
// Allocate memory
arr = (int *)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
// It's a good practice to set the pointer to NULL after freeing memory
arr = NULL;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n = 5;
return 0;
}
String in C Language
In C, strings are essentially arrays of characters that are terminated by a special null character
('\0'). A string is a sequence of characters enclosed in double quotes.
char stringName[size];
In this case, the compiler automatically allocates enough space for the string, including
the null character '\0'.
Here, the string "Hello" is initialized, and the rest of the array will be filled with the
null character '\0' (up to 20 characters in this example).
However, you can also implement these functions manually, without using the standard library.
Definition:
The my_strlen() function counts the number of characters in a string until it encounters the
null character ('\0'), which marks the end of the string.
Steps:
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
printf("Length of string: %d\n", my_strlen(str)); // Output: 13
return 0;
}
Definition:
The my_strcpy() function copies each character from a source string to a destination string,
ensuring the destination string is null-terminated.
Steps:
int main() {
char source[] = "Hello";
char destination[20];
my_strcpy(destination, source);
printf("Copied string: %s\n", destination); // Output: Hello
return 0;
}
Definition:
The my_strcat() function appends one string (source) to the end of another string (destination).
Steps:
1. Find the end of the destination string (where the null character '\0' is).
2. Use a loop to copy characters from the source string to the end of the destination
string.
3. Add the null character at the end of the concatenated string.
#include <stdio.h>
int j = 0;
while (src[j] != '\0') { // Copy characters from source to destination
dest[i] = src[j];
i++;
j++;
}
dest[i] = '\0'; // Null-terminate the concatenated string
}
int main() {
char str1[50] = "Hello";
char str2[] = " World!";
my_strcat(str1, str2);
printf("Concatenated string: %s\n", str1); // Output: Hello World!
return 0;
}
Definition:
The my_strcmp() function compares two strings. It returns a negative value if the first string
is smaller, 0 if both strings are equal, or a positive value if the first string is greater.
Steps:
#include <stdio.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = my_strcmp(str1, str2);
if (result < 0)
printf("\"%s\" is less than \"%s\"\n", str1, str2);
else if (result == 0)
printf("\"%s\" is equal to \"%s\"\n", str1, str2);
else
printf("\"%s\" is greater than \"%s\"\n", str1, str2);
return 0;
}
Summary:
Strings in C: A string is an array of characters, ending with the null character ('\0').
String Operations:
o my_strlen(): Finds the length of a string.
o my_strcpy(): Copies one string into another.
o my_strcat(): Concatenates (appends) one string to another.
o my_strcmp(): Compares two strings to determine which is lexicographically greater
or smaller.
These functions provide basic string operations without using the C standard library
functions. Each function helps you manipulate strings in a controlled way.