C Programming

9-Pointers In C Programming

C Programming Lessons, Learn C Programming, Introduction to C Language, How to Program with C, Pointers in C

The concept of Pointer is an important feature in C and C++ programming.

Why are pointers used? Pointers are used to manipulate variables whose address they hold.

As we remember from the scanf() function, the & (and – ampersand) sign was used for addressing.

The & address operator is a single operator that returns the address of the operand.

For example

int y = 5;

int *yPtr;

In the above expression, the expression yPtr=&y; assigns the address of the variable y to the pointer variable yPtr. The yPtr variable is showing y.

C Programming. Pointers in C. Arrays and Functions with Pointers. What is Pointer? How to Use Pointers in C? Learn C Language. Lessons for C

How to Declare a Pointer

Here is 3 ways to declare int type Pointers

int* Ptr;

int *Ptr1;

int * Ptr2;

Here, p1 is a pointer and p2 is a normal variable;

int* p1, p2;

Assigning Addresses to Pointers

int* Ptr, y;

y = 6;

Ptr = &y;

Here, 6 is assigned to the variable y. The address of y is assigned to the pointer Ptr.

& gives the address of the variable

* returns the value at the specified address

Mini Example

int main()
{
    int number=10;
    printf("%d\n", number);
    printf("%p", &number);
}
Value and Address of a Variable
Value and Address of a Variable

Example : Working of Pointers

#include <stdio.h>
int main()
{
   int* Ptr, x; //Ptr is pointer and x is normal variable

   x = 5; //defining x as 5
   printf("Address of x: %p\n", &x); //printing x's address by &
   printf("Value of x: %d\n\n", x);  // 5

   Ptr = &x; //pointer gets the variable's address
   printf("Address of pointer Ptr: %p\n", Ptr); //prints address info
   printf("Content of pointer Ptr: %d\n\n", *Ptr); // prints value info (5)

   x = 10; //change the x to 10
   printf("Address of pointer Ptr: %p\n", Ptr); //prints address of x
   printf("Content of pointer Ptr: %d\n\n", *Ptr); // prints value info (10)

   *Ptr = 2; //change the x to 2 by using pointer
   printf("Address of x: %p\n", &x); //prints x's address info
   printf("Value of x: %d\n\n", x); // prints x's value (2)

   return 0;
}
Working with Pointers
Working with Pointers

Some errors for Pointers

int x, *Ptr;

Ptr=x;                   //Error, here Ptr is an address but x is a value

*Ptr=&x;             // Error, here *Ptr is a value but &x is an address

Ptr=&x;                //Correct, both are addresses

*Ptr=x;                //Correct, both are values

Using Pointers with Arrays

In previous topic we mentioned about Arrays in C. Now we will see the use of these arrays with pointers.

Now let’s come to the magic sentence that explains the relationship between arrays and pointers;

The name of an array is the memory address of the first element of that array.

char characters[8];

characters[0];   //first element

characters[2];   // 3rd element from the beginning

characters[7];   //last(8.) element

We can reach the addresses of these elements with the address operator &

&characters[0];                //address of the first element

&characters[2];                //address of the 3rd element from the beginning

&characters[7];                // address of the last(8.) element

Here, the &characters[0] sequence and the characters sequence refer to the same address.

characters&characters[0]
characters + 1&characters[1]
characters + 2&characters[2]
characters + 3&characters[3]
characters + 7&characters[7]

If the characters Pointer is used with the content operator

*characterscharacters[0]
*(characters + 1)characters[1]
*(characters + 2)characters[2]
*(characters + 3)characters[3]
*(characters + 7)characters[7]
Let’s Make an Example:
#include <stdio.h>
int main()
{
  int k[5] = {10, 11, 12, 13, 14};
  int* Ptr;

  Ptr = &k[2]; // Ptr is assigned to the address of the third element

  printf("*Ptr = %d \n", *Ptr);   // 12
  printf("*(Ptr+1) = %d \n", *(Ptr+1)); // 13
  printf("*(Ptr-1) = %d", *(Ptr-1));  // 11

  return 0;
}
Pointers with Arrays
Pointers with Arrays

Using Pointers with Functions

Passing Address to Function

#include <stdio.h>
void swap(int *p1, int *p2);

int main()
{
    int num1 = 10, num2 = 20;

    printf("num1 = %d\n", num1); //printing numbers before swap
    printf("num2 = %d\n", num2);

    //addresses of num1 and num2 is passed to swap function
    swap(&num1, &num2);

    printf("num1 = %d\n", num1); ////printing numbers after swap
    printf("num2 = %d", num2);
    return 0;
}

void swap(int* p1, int* p2) //swap returns nothing, its type is void
{
    //here pointers p1 p2 swapped so actually num1 num2 swapped
    int Temp;
    Temp = *p1;
    *p1 = *p2;
    *p2 = Temp;
}
Passing Address to Function
Passing Address to Function

Passing Pointer to Function

#include <stdio.h>

void increaseOne(int* Ptr)
{
  (*Ptr)++; // increasing *Ptr by 1
  //Ptr and t pointers both have same address
}

int main()
{
  int *t, j = 10;
  t = &j;

  increaseOne(t); //passing pointer to the function

  printf("%d", *t); // 11

  return 0;
}
Passing Pointer to Function 
C Programming. Pointers in C. Arrays and Functions with Pointers. What is Pointer? How to Use Pointers in C? Learn C Language. Lessons for C
Passing Pointer to Function

Leave a Reply

Your email address will not be published. Required fields are marked *