C pointers definition and declaration


C pointers definition and declaration

C pointers are enjoyable and straightforward to learn. While pointers make specific C programming jobs easier, others, such as dynamic memory allocation, are difficult to accomplish without them. To become a competent C programmer, it is vital to learn pointers, and let's begin by studying them in little steps.

As you may know, every variable is a memory location, and each memory location has an address that can be accessed using the ampersand (&) operator, which represents a memory address. Take a look at the sample below, which prints the addresses of the variables defined.

Example of C pointers definition and declaration

#include<stdio.h> #include<conio.h> void main() { int num=10; int*p; p=&num; /* stores the address of num */ printf("adress of p is: %x \n",p); /* give address of the num */ printf("value of p is: %d ",*p); /* get the value stored at the address */ getch(); }

When the preceding code is built and run, the following result is obtained

adress of p is: fff4 value of p is: 10

What exactly are pointers?

Like any other variable or constant, a pointer must be declared before it may be used to hold any variable address. The type *var-name; a form of a pointer variable declaration, is the most common.

The pointer's base type is type, which must be a valid C data type, and the pointer variable's name is var-name. The asterisk * used to define a pointer is the same as the asterisk used to multiply. On the other hand, the asterisk is used to mark a variable as a pointer in this sentence. Consider a few examples of acceptable pointer declarations.

    char *ch /* pointer to a character */ int *ip; /* pointer to an integer */ double *dp; /* pointer to a double */ float *fp; /* pointer to a float */ char *ch /* pointer to a character */ int *ip; /* pointer to an integer */ float *fp; /* pointer to a float *

Whether integer, float, character or other, all pointer values have the same data type: a lengthy hexadecimal number that denotes a memory address. The data type of the variable or constant to which the pointer points is the sole distinction between pointers of various data kinds.

In C, a pointer is a variable that keeps track of the address of another variable. A pointer can also be used to refer to another pointer's function. A pointer can be increased or decreased, pointing to the next or previous memory address. The goal of pointers is to save memory and speed up the execution process.

Pointer Declarations

Except for the asterisk (*) character before the pointer variable name, pointer declaration is comparable to other variable declaration types.

Here is the syntax to declare a pointer

    data_type *poiter_name;

    Let's consider with following example statement

    int *ptr;

    In this statement, ptr is the name of a pointer variable (name of the memory blocks in which the address of another variable is going to be stored).

    The character asterisk (*) instructs the compiler to declare the identifier ptr as a pointer.

    The data type int instructs the compiler to save the memory location of the integer type variable in pointer ptr.