Pointers in C
What is pointer?
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 "int".
Basic uses of pointers
Once you have a pointer variable, you can use it to access the value of the variable it points to or change that value. For example, you can print the value of "x" using the pointer as follows:
int main() {
int x = 10;
int *ptr = &x;
// Accessing the value of the variable that ptr points to
printf("The value of x is: %d\n", *ptr);//print value 10
// Changing the value of the variable that ptr points to
*ptr = 20;
printf("The new value of x is: %d\n", x);//print value 20
*ptr += 100;
printf("The new value of x is: %d\n", x);//print value 120
*ptr *= *ptr;
printf("The new value of x is: %d\n", x);//print value 14400 (120 * 120)
return 0;
}
In this example, we declare an integer variable ‘x’
and a pointer variable ‘ptr’ that points to the address of ‘x’.
We then use the ‘*’ operator to access the value of ‘x’ using the
pointer ‘ptr’, and print it to the console using ‘printf()’.
Next, we change the value of ‘x’ by assigning a new
value to the variable that ‘ptr’ points to using the ‘*’
operator. Finally, we print the new value of ‘x’ to the console using ‘printf()’.
Array operations with pointers
int* ptr = arr;
int first_element = *ptr;int* ptr = arr;
for (int i = 0; i < array_length; i++) {
printf("%d ", *ptr);
ptr++;
}
Here, the pointer ptr is initialized to the address
of the first element of the array arr. Inside the loop, the value of the
current element is printed using the * operator, and the pointer is
incremented to point to the next element.
void insert_begining(int *arr,int lenth,int value){
for (int i = lenth -1; i > 0; i--)
{
arr[i]=arr[i-1];
}
*arr = value;
}
void main(){
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int len = sizeof(arr)/sizeof(arr[0]);
for (int i = 0; i < len; i++)
{
printf("%d ",arr[i]);
}//output 1 2 3 4 5 6 7 8 9 10
printf("\n");
insert_begining(arr,len,100);
for (int i = 0; i < len; i++)
{
printf("%d ",arr[i]);
}//output 100 1 2 3 4 5 6 7 8 9
}
Here, the array 'arr' is passed to the function 'insert_begning' as a pointer to the first element of the array. The length of the array is also passed as a separate argument.
int *my_function(int array_length)
{
int *arr = (int *)malloc(array_length * sizeof(int));
if (arr == NULL)
{
printf("Error: could not allocate memory.\n");
exit(1);
}
for (int i = 0; i < array_length; i++)
{
arr[i] = pow(2,i);
}
return arr;
}
Here, the function 'my_function' takes an integer 'array_length' as an argument and allocates an array of 'array_length' integers using 'malloc'. If 'malloc' returns a null pointer, the function prints an error message and exits with a non-zero status code. int array_length = 5;
int* my_array = my_function(array_length);
for (int i = 0; i < array_length; i++) {
printf("%d ", my_array[i]);
}
free(my_array);
Here, 'array_length' is set to '5', and 'my_function' is called with 'array_length' as an argument. The function returns a pointer to the first element of the allocated array, which is stored in the variable 'my_array'.
Comments
Post a Comment