C PROGRAMMING: POINTERS-1

C PROGRAMMING: POINTERS-1

1st rule of programming :-

If it works don’t touch it.

pointers:

  • It is a type of data type.
  • A variable that stores the address of another variable.
  • Can be int, float, char, array, function, or any other pointer.
  • Size depends on the architecture ( ex: 2 bytes for 32 bit ).
  • Pointer in C programming language can be declared using *( asterisk symbol ).

pointer is a variable that stores the address of another variable. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. For example, an integer variable holds (or you can say stores) an integer value, however, an integer pointer holds the address of an integer variable. 

‘&’ and ‘*’ operators:

  • The address of the operator ‘&’ returns the address of a variable.
  • * is the dereference operator ( also called indirection operator ) used to get the value at a given address.
To learn more about the operators, visit our previous article 
DATA TYPES-3 AND OPERATORS IN C PROGRAMMING LANGUAGE:

simple explanation how to access the address of a variable without using pointer;

In this program, we have a variable num of int type. The value of num is 10 and this value must be stored somewhere in the memory, right? Memory space is allocated for each variable that holds the value of that variable, this memory space has an address. For example, we live in a house and our house has an address, which helps other people to find our house. In the same way, the value of the variable is stored in a memory address, which helps the C program to find that value when it is needed.

So let’s say the address assigned to variable num is: 073xx6682, which means whatever value we would be assigning to num should be stored at the location: 073xx6682.  Let’s take an example:

#include <stdio.h>
int main()
{
   int num = 60;
   printf("Value of variable num is: %d", num);
   /* To print the address of a variable we use %p
    * format specifier and ampersand (&) sign just
    * before the variable name like &num.
    */
   printf("nAddress of variable num is: %p", &num);
   return 0;
}

output:

Value of variable num is: 60 
Address of variable num is: 0x7ifif7685dc58

Point to note: %p is a format specifier which is used for displaying the address in hex format.
Now that you know how to get the address of a variable but how to store that address in some other

advantages of using pointers:

  1. Pointer act as references to different types of objects such as variables, arrays, functions, structures, etc. However, C language does not have the concept of references as in C++. Therefore, in C we use the pointer as a reference.
  2. Pointers are the instruments of dynamic memory Management. It is helpful in the allocation and de-allocation of memory during the execution of the program.
  3. Pointers are helpful in traversing through arrays and character strings.
  4. Pointers enhance the execution speed of a program.
  5. Storage of strings through pointers saves memory space.
  6. Pointers may be used to pass on arrays, strings, functions, and variables as arguments of a function.

disadvantages of using pointers:

  • Uninitialized pointers might cause a segmentation fault.
  • The dynamically allocated block needs to be freed explicitly.
  •  Otherwise, it would lead to a memory leak.
  • Pointers are slower than normal variables. If pointers are updated with incorrect values, it might lead to memory corruption.

null pointer:

  • A pointer that is not assigned any value but NULL is known as the NULL pointer.
  • In general computer programming a null pointer is a pointer that does not point to any object or function.
  • We can use it to initialize a pointer variable when that pointer variable isn’t assigned any valid memory address.

int *ptr = NULL;

example of null pointer:

#include<stdio.h>
int main()
{  
   int *ptr2= NULL;
printf(" The address of some garbage is: %p n", ptr2);

return 0;
}

output:

The address of some garbage is:
00000

uses of pointers:

  • Dynamic memory allocation.
  • Arrays, Functions and structure.
  • Return multiple values from a function.
  • Pointer reduces the code and improves the performance.

Now, let’s do some coding on pointers:

program-1

#include <stdio.h>
int main()
{
   //Variable declaration
   int num = 10;

   //Pointer declaration
   int *p;

   //Assigning address of num to the pointer p
   p = #

   printf("Address of variable num is: %p", p);
   return 0;
}

output:

Address of variable num is: 0x7fff5694dc58

Important point to note is: The data type of pointer and the variable must match, an int pointer can hold the address of int variable, similarly a pointer declared with float data type can hold the address of a float variable. In the example below, the pointer and the variable both are of int type.

program-2

#include <stdio.h>
int main()
{
    int var =10;
    int *p;
    p= &var;

    printf ( "Address of var is: %p", &var);
    printf ( "nAddress of var is: %p", p);

    printf ( "nValue of var is: %d", var);
    printf ( "nValue of var is: %d", *p);
    printf ( "nValue of var is: %d", *( &var));

    /* Note I have used %p for p's value as it represents an address*/
    printf( "nValue of pointer p is: %p", p);
    printf ( "nAddress of pointer p is: %p", &p);

    return 0;
}

output:

Address of var is: 0x7fff5d027c58 
Address of var is: 0x7fff5d027c58 
Value of var is: 10 
Value of var is: 10 
Value of var is: 10 
Value of pointer p is: 0x7fff5d027c58 
Address of pointer p is: 0x7fff5d027c50

How to read the following pointer?

  1. int (*p)(int (*)[2], int (*)void))  

Explanation

This pointer will be read as p is a pointer to such function which accepts the first parameter as the pointer to a one-dimensional array of integers of size two and the second parameter is the pointer to a function which parameter is void and return type is the integer.

I hope everythimg is clear to you, In the next article we will dicuss about "POINTER tO POINTER","POINTERS TO FUNCTIONS" and many more things.
 I will see you next time... 
Also visit our previos articles listed below:

And many more…

Scroll to Top