Posts

Showing posts from March, 2023

Arrays in C

Array declaration In C, an array is a collection of elements of the same data type, accessed using a common name and an index or subscript to identify a specific element. Arrays in C are defined by specifying the data type of the elements and the number of elements in the array. Here is an example of defining an integer array with 5 elements in C: int myArray[5]; This creates an integer array called 'myArray' with 5 elements. The elements can be accessed using their index starting at 0, like this: myArray[0] = 1; myArray[1] = 2; myArray[2] = 3; myArray[3] = 4; myArray[4] = 5; Arrays can also be initialized at the time of declaration like this: int myArray[] = {1, 2, 3, 4, 5}; In addition to one-dimensional arrays, C also supports multi-dimensional arrays. For example, a two-dimensional array can be defined like this: int myArray[3][4]; This creates a two-dimensional array with 3 rows and 4 columns. The elements can be accessed using two indices, like this: myA...

Pointers in C

Image
What is pointer? Pointers are an essential concept in programming, especially in languages like C and C++. Pointers are variables that store memory addresses. They allow you to access and manipulate data stored in memory, including variables, arrays, and structures. When you declare a variable in C or any programming language, the system allocates a block of memory to store the value of that variable. This block of memory has a unique address, which you can access using a pointer memory allocation pointer declaration For example, let's say you have a variable called "x" that stores an integer value. You can declare a pointer called "ptr" to store the memory address of "x" as follows: int x = 10; int *ptr = &x; In this example, the "&" operator is used to get the memory address of "x", which is then stored in "ptr". The "*" operator is used to declare "ptr" as a pointer variable of type "in...