- DIFFERENT ASPECTS OF USING POINTERS IN OTHER METHODS IN C
- C POINTERS OPERATORS
- ADVANTAGES
- USES
- NULL POINTER
- COMPLEX POINTER
One of the most interesting and unusual
aspects of C is its pointers. The tongue becomes more
flexible and potent as a result. The advice can seem complicated and
challenging at first, but if you get it, trust me—C opens up a world of
opportunities.
When a variable is declared in a program,
the system specifies where in memory the value should be stored. According to
the software up above, we can get this place's address.
Assume for the purposes of argument that
the system has reserved 80F in memory.
Int n=10
Int p=&n
Declaring a pointer
In C, a pointer can be declared
using the * (letter symbol). Also known
as an indirect pointer, it is used to dereference a pointer.
- int *a;//pointer to int
- char *c;//pointer to char
the help of * (indirection operator), we can print the value of
pointer variable p.
Let's see the pointer example as explained in the above figure.
Now we will understand different aspects of using pointers in
other methods using C
C Pointer Operators
Two distinct kinds of pointer operators
exist in C:
- operator and
- & operato
Operators in C have been examined in detail in distinct sections.
The operand's memory address is returned by
the & operator. As an illustration,
a = &b;
The memory address of variable b will be
saved in variable a.
The counterpart of & is the * operator.
The value found at the specified location is returned by this operator.
For instance, if the variable's memory
location is included in a, then the code,
c is equal to *a;
will cause the variable's value to be stored in c.
let us understand the pointer in C with a C program
Advantages of using pointer
· When dealing with arrays and C
structures, pointers are the way to go.
It is simpler to pass functions as parameters to other functions when pointers
are used because they permit references to functions.
• A C function can change its arguments sent to it using Pointers.
• The program's size and runtime are reduced thereafter.
• It makes it possible for C's dynamic memory management to work.
Usage of pointers
The C language has several uses for pointers.
1) Allocating memory dynamically
We may utilize the pointer-based malloc() and
calloc() methods in the C language to dynamically allocate memory.
2) Structures, Functions, and Arrays
In C, pointers are often utilized in arrays,
functions, and structures. It increases performance and decreases code.
NULL Pointer
The NULL pointer is a
pointer that is only NULL and has no value assigned to it. If you declare the
pointer without an address to provide, you can set its value to NULL. It will
provide a better approach.
Int *p=NULL;
Most of the time in libraries of it may have value 0
Let us try one C program to swap two numbers using 3rd variable
Several things must be taken into consideration while reading the complex pointers in C. Let’s
see the precedence and associativity of the operators that are used regarding
pointers.
Here in the above table, please mark
- The bracket operator,
denoted by the symbol (), is used to declare and define the function.
- An array subscript
operator is used here: This is known as the pointer operator.
- A pointer's name serves
as its identifier. There is never going to be anything more important than
this.
- Kind of information: A pointer's data type identifies the sort of variable it is pointing to. It also includes other modifiers such as signed int and long.
- The bracket operator,
denoted by the symbol (), is used to declare and define the function.
-
To interpret the pointer, we must recognize that () and [] are equally precedential. Consequently, we must consider their associativity. The precedence is with (), as the associativity is climbing from left to right.
The pointer name (identifier) p and the pointer operation * have the same precedence inside the() bracket. Due to their right-to-left associativity, p has precedence over *, with * coming in second.
As the data type comes first, assign [] the third priority. The pointer will therefore look something like this:
The pointer will be read as p is a pointer to an array of integers of
size 10.
Example
How to read the following pointer?
Here, "p" designates a function that accepts a void pointer as its second parameter and a two-dimensional array of integers as its first, with an integer as the return type of the letter.
No comments:
Post a Comment