Basics of Pointers In C

Pointer logo

Pointers in C

A pointer is a variable which contains the address of the variable specified.
for example: 

int x = 100;
int *data;
data = &x;

this variable data contains the address of the block in the memory which contains the value 100.
* specifies that this is the pointer variable.
& gives the address.

that is the what I am doing here assigning the address of the integer variable x to the pointer variable data.

to get the data from the pointers we have to use the * sign.
for the previous example to get the data of the memory block of the

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

first output

 this should print the output like the image.

Further digging into deep for the pointers.

we can use them for the array.


if we change content in the memory block pointed by the data, then this change should reflect the in the content of the x.
Because variable x has the content of that memory block.
(this is the great advantage of the using the pointers)
we can read, change anything without copying the data from one location to another.

like in this example.

#include <stdio.h>

int main(){
int x = 100;
int *data ;
data = &x;
printf("%d before assigning\n",x );
*data = 120;
// so this change the value of x.
printf("%d after assigning\n",x );
return 0;
}

this code will give the output, Keep in mind we are changing the data variable and change reflect in the variable x.
altering data output

Pointers in C Array.

to work with the pointers in the array.
first, we have to calculate the length of the array.
so take an example of working with the array.

int x[] = {200,300,522,655};
int *data;
data = x; // an array without the index is also address.
// determining the size of the array
size_t n = sizeof(x);
size_t i = sizeof(x[0]);
int arrayLen = 0;
arrayLen = n/i;
for (int iter = 0; iter < arrayLen; iter++)
{
// this iterator variable automatically jumps 4bytes.
// because we are working the memory so we are increamenting
// the memory block each time.
printf("%d ",*(data + iter));
}

This code will give the output
integer output

take one more example of the char array.

char c[] = {'c','o','d','e'};
int *data;
char *name;
printf("\n");
n = sizeof(c);
i = sizeof(c[0]);
arrayLen = n/i;
name = c;
for (int iter = 0; iter < arrayLen; iter++)
{
// this iterator variable automatically jumps 1bytes.
// because we are working the memory so we are increamenting
// the memory block each time.
// this is happening with the help of the type of the pointer.
printf("%c ",*( name + iter));
}

this code will give the output.
char output
so you can see the working.
as this is for the basic so I am not digging deeper into the pointers.

That's it for today's tutorial.
if you like this post check out my other post on the C and other programming languages.
Thanks for being here.

Comments

Popular posts from this blog

C/C++ program to check the Palindrome string.

Second Equation of motion, How to implement using C language

Third Equation of motion, How to implement using C language