Why using pointer (c programming)

Today, i receive email from one of forum asking about why we are using a pointer.

As long as I know we can use a pointer for making dynamic array. We can’t use variable without pointer to make a dynamic array. As an example:

We can’t do:

int size;
size=10;
int i[size];

But we have to do:

int size;
size = 10;
int *i=new int [size];

The other reason is if we want to make a function, which one of input parameter is output, we can use pointer. But in my experience it’s better if we use reference. For example

void add(int a, int b, int &c)
{
c=a+b;
}

Be Sociable, Share!

Leave a Reply

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