Pointers

 Pointers

Accessing address of a variable:-

In C language, to access the address of a variable, you can use the "address-of" operator "&". For example, if you have a variable named "x", you can get its address using "&x".



To access the address of a variable with a short word, you can use a typedef to create a shorter alias for the data type of the variable. For example, if you have a variable named "count" of type "unsigned int", you can create a typedef for "unsigned int" called "ushort" (short for "unsigned short") and then use "&count" to get its address:

typedef unsigned int ushort;
ushort count;
ushort *ptr = &count; // get the address of count

In this example, "ptr" is a pointer to an "unsigned short", which is the same as an "unsigned int" but with a shorter name. The "&" operator is used to get the address of "count", and the resulting pointer is assigned to "ptr".

declaring and initializing pointers:-

In C language, you can declare and initialize pointers using the "*" operator. To declare a pointer, you specify the data type that the pointer points to, followed by an asterisk. For example, to declare a pointer to an integer, you would use the following syntax:

int *ptr;

Here, "ptr" is a pointer to an integer.

To initialize a pointer, you can assign it the address of a variable using the "&" operator. For example, if you have an integer variable named "num", you can initialize a pointer to point to it using the following syntax:

int num = 42;
int *ptr = #

Here, "ptr" is a pointer to an integer, and "&num" is the address of the "num" variable. The assignment statement assigns this address to "ptr", so that "ptr" now points to "num".

To use a shorter alias for the data type of the pointer, you can use a typedef. For example, to create a shorter alias for "int *" called "iptr", you can use the following syntax:

typedef int *iptr;
iptr ptr = #

Here, "iptr" is a shorter name for "int *", and "ptr" is a pointer to an integer initialized with the address of "num".

pointer expression:-

In C language, pointer expressions are used to manipulate pointers and the data they point to. A pointer expression typically involves one or more of the following operators: "*", "&", "+", "-", and "[]". To use a shorter alias for the data type of the pointer, you can use a typedef. Here are some examples of pointer expressions with short words:

typedef int *iptr;
iptr ptr = NULL; // initialize pointer to NULL

int num = 42;
ptr = # // assign address of num to pointer

*num = 99; // dereference pointer and assign value 99 to num
printf("%d", num); // prints 99

iptr ptr2 = ptr + 1; // increment pointer by 1 (i.e. sizeof(int))
printf("%d", *ptr2); // prints garbage value (undefined behavior)

int arr[] = {1, 2, 3};
iptr ptr3 = arr; // assign pointer to first element of array
printf("%d", *(ptr3 + 2)); // prints 3 (access third element of array)

iptr ptr4 = &arr[1]; // assign pointer to second element of array
printf("%d", ptr4[-1]); // prints 1 (access first element of array using negative index)

In this example, "iptr" is a shorter name for "int ". The "ptr" variable is initialized to NULL and then assigned the address of "num". The "" operator is used to dereference the pointer and assign a value to "num". The "+" and "-" operators are used to increment and decrement the pointer by a certain number of bytes (in this case, the size of an integer). The "[]" operator is used to access elements of an array using pointer arithmetic.

pointer and array:-

In C language, arrays and pointers are closely related. An array is essentially a sequence of contiguous elements of the same type, while a pointer is a variable that stores the memory address of another variable.

To declare an array in C with a shorter word, you can use a typedef. For example, to declare an array of integers with 5 elements called "myarr", you can use the following syntax:

typedef int myintarr[5];
myintarr arr = {1, 2, 3, 4, 5};

Here, "myintarr" is a shorter name for the array data type "int[5]", and "arr" is an array of integers initialized with the values 1 to 5.

To declare a pointer in C with a shorter word, you can use a typedef as well. For example, to declare a pointer to an integer called "iptr", you can use the following syntax:

typedef int *iptr;
int num = 42;
iptr ptr = #

Here, "iptr" is a shorter name for the pointer data type "int *", and "ptr" is a pointer to an integer initialized with the address of "num".

To access elements of an array using a pointer, you can use pointer arithmetic. For example, if you have a pointer to an integer called "ptr" and you want to access the third element of an array, you can use the following syntax:

int arr[] = {1, 2, 3, 4, 5};
iptr ptr = &arr[0];
int third_element = *(ptr + 2);

Here, "ptr" is a pointer to the first element of the array "arr", and the "+ 2" expression increments the pointer by two integers (i.e. the size of two integers). The "*" operator is used to dereference the resulting pointer and access the third element of the array.

You can also use array indexing to access elements of an array using a pointer. For example, if you have a pointer to an integer called "ptr" and you want to access the fourth element of an array, you can use the following syntax:

int arr[ ] = {1, 2, 3, 4, 5};
iptr ptr = &arr[0];
int fourth_element = ptr[3];

Here, "ptr" is a pointer to the first element of the array "arr", and the "ptr[3]" expression accesses the fourth element of the array. This is equivalent to the expression "*(ptr + 3)".

pointer and function:-

In C language, pointers to functions can be used to pass functions as arguments to other functions, or to store functions in data structures. Here's an example of how to declare and use a pointer to a function using short words:

typedef int (*fptr)(int);
int square(int x) {
    return x * x;
}

fptr ptr = square; // assign pointer to square function
int result = ptr(5); // call square function through pointer
printf("%d", result); // prints 25

Here, "fptr" is a shorter name for the function pointer data type "int (*)(int)", which is a pointer to a function that takes an integer argument and returns an integer value. The "square" function is defined to compute the square of its argument. The "ptr" variable is a pointer to a function that points to the "square" function. The "ptr(5)" expression calls the "square" function through the pointer, passing it the value 5 as an argument, and the resulting value is assigned to the "result" variable.

Function pointers can also be used as arguments to other functions. Here's an example:

void apply(fptr f, int x) {
    int result = f(x);
    printf("%d", result);
}

apply(square, 5); // prints 25

Here, the "apply" function takes a function pointer "f" and an integer argument "x". It calls the function pointed to by "f" with "x" as an argument, and prints the resulting value. The "apply(square, 5)" expression calls the "apply" function, passing it the "square" function pointer and the value 5.

In addition to using typedef to define function pointers with shorter words, you can also use the shorthand syntax when declaring and initializing function pointers:

int (*ptr)(int) = square; // shorthand syntax for declaring and initializing function pointer
int result = ptr(5); // call square function through pointer
printf("%d", result); // prints 25

This is equivalent to the first example above, but the data type is specified explicitly using parentheses.

pointer and structure:-

In C language, pointers to structures can be used to store and manipulate data in more complex data structures. Here's an example of how to declare and use a pointer to a structure using short words:

typedef struct {
    int x;
    int y;
} point;

point p = {3, 4};
point *ptr = &p; // assign pointer to p structure
int x = ptr->x; // access x field through pointer
int y = (*ptr).y; // access y field through pointer using dereferencing
printf("%d %d", x, y); // prints 3 4

Here, "point" is a shorter name for the structure that contains two integer fields "x" and "y". The "p" variable is an instance of the "point" structure initialized with the values 3 and 4. The "ptr" variable is a pointer to the "p" structure. The "->" operator is used to access the fields of the structure through the pointer. The "ptr->x" expression accesses the "x" field of the "p" structure, while the "(*ptr).y" expression accesses the "y" field of the "p" structure using dereferencing.

You can also use the shorthand syntax when declaring and initializing structure pointers:

typedef struct {
    int x;
    int y;
} point;

point p = {3, 4};
point *ptr = &p; // assign pointer to p structure
int x = ptr->x; // access x field through pointer
int y = ptr->y; // access y field through pointer using shorthand syntax
printf("%d %d", x, y); // prints 3 4

This is equivalent to the first example above, but the data type is specified using the "struct" keyword followed by the structure name.

pointer to pointer :-

In C language, a pointer is a variable that stores the memory address of another variable. A pointer to pointer is a pointer variable that stores the memory address of another pointer variable.

In short, a pointer to pointer is a variable that points to another pointer variable.

Here's an example of how you can declare a pointer to pointer in C:

int **ptr_to_ptr;

This declares a variable named ptr_to_ptr that is a pointer to another pointer. To assign a value to this variable, you need to allocate memory for both pointers using the malloc() function, like this:

int *ptr = malloc(sizeof(int));
*ptr = 42;

ptr_to_ptr = malloc(sizeof(int *));
*ptr_to_ptr = ptr;

This code allocates memory for an integer pointer ptr and assigns it the value 42. It then allocates memory for another pointer ptr_to_ptr and assigns it the address of ptr.

You can access the value of ptr using *ptr_to_ptr, like this:

printf("%d\n", **ptr_to_ptr);

This will print 42 to the console.











Post a Comment

Previous Post Next Post