Pages

Monday, 13 July 2020

Pointer and Array Relationship


Pointers and arrays have a special relationship in C Language.Name of an array is constant pointer which point first element of array and an array is represented by a variable that is associated with the address of its first storage location. A pointer is also the address of a storage location with a defined type, so name permits the use of the array [ ] index notation with both pointer variables and array variables. For example, the following two references in C language are equivalent in meaning:

Q = &a[0];                                                           trace(Q[2]);
trace(Q[2]);


Diagram shows a pointer to an array of five objects.




This difference is manifested in the D syntax if you attempt to assign pointers and scalar arrays. If x and y are pointer variables, the expression x = y is legal; it simply copies the pointer address in y to the storage location named by x. If x and y are scalar array variables, the expression x = y is not legal. Arrays may not be assigned as a whole in D. However, an array variable or symbol name can be used in any context where a pointer is permitted. If p is a pointer and a is an array, the statement p = a is permitted; this statement is equivalent to the statement p = &a[0].



Some of key point is given below to show relationship between array and pointer.


SN.
Array
Pointer
1.
A special type of data type which hold address of a memory location
Pointer is also a special type of variable which hold a memory location.
2
Name of an array is constant pointer which holds address of first element of an array.
Pointer is a variable which hold a single location of memory.
3
Element of an array is access using index position
Data of a pointer is read by * operator
4
Array is an example of pointer
Pointer is a variable which can be used in much aspect like an array.
5
Physical address can be calculate
Address calculation is part of pointer. Which skip address position As its data type storage capacity?
6
Array always pass to a function by reference
Call by reference is achieved by pointer.


No comments:

Post a Comment