Monday, November 28, 2011

Array and Pointer and Difference

*pointer of array:-All the variables can be manipulated in the memory by their addresses. CPU uses these addresses to fetch them, change them and save them. So, all variables have an address (not only those that you pointed to them). We can find out the address of a variable by (&) operator, in form of:
1.int* ptr=&intvar

array of pointer:-
An array is just a sequence of variables.

Differences:

1) A pointer is a place in memory that keeps address of another place inside, while an array is a single, preallocated chunk of contiguous elements (all of the same type), fixed in size and location.

2) Array like pointers can't be initialized at definition like arrays.
char car[3]={'a','b',66};
char* cpt=new char[3];

3) When we allocate memory for a pointer to use it as a dynamic array. The memory can be resized or freed later. But this is not the case for arrays.

For example:
char* pta=new char[12];
4.)Pointers are used to manipulate data using the address. Pointers use * operator to access the data pointed to by them.Arrays use subscripted variables to access and manipulate data.Array variables can be equivalently written using pointer expression.

No comments:

Post a Comment