Arrays
An array is a collection of objects of the same data type, allocated contiguously in memory. Individual objects in an array, called elements, are accessed by their position in the array. The subscripting operator ([]) provides the mechanics for creating an index to array elements. This form of access is called indexing or subscripting. An array facilitates the coding of repetitive tasks by allowing the statements executed on each element to be put into a loop that iterates through each element in the array.
A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer to a bit field or a reference.
Some common uses for pointers are:
- To access dynamic data structures such as linked lists, trees, and queues.
- To access elements of an array or members of a structure or C++ class.
- To access an array of characters as a string.
- To pass the address of a variable to a function. (In C++, you can also use a reference to do this.) By referencing a variable through its address, a function can change the contents of that variable.
Note that the placement of the type qualifiers volatile and const affects the semantics of a pointer declaration. If either of the qualifiers appears before the *, the declarator describes a pointer to a type-qualified object. If either of the qualifiers appears between the * and the identifier, the declarator describes a type-qualifed pointer.
The following table provides examples of pointer declarations.
| Declaration | Description |
|---|---|
| long *pcoat; | pcoat is a pointer to an object having type long |
| extern short * const pvolt; | pvolt is a constant pointer to an object having type short |
| extern int volatile *pnut; | pnut is a pointer to an int object having the volatile qualifier |
| float * volatile psoup; | psoup is a volatile pointer to an object having type float |
| enum bird *pfowl; | pfowl is a pointer to an enumeration object of type bird |
| char (*pvish)(void); | pvish is a pointer to a function that takes no parameters and returns a char |
Difference:
- Arrays automatically allocate space which is fixed in size and location; pointers are dynamic.
- Arrays are refered directly to the elements. But in pointers we refers to address of the elements
but not elements . indirect calling of the elements.
- They're related but not totally comparable.
A pointer is an address in memory where a variable is located.An array is a conceptual data representation consisting of a list of more than one item of a particular scalar type (int, float, char, structure, etc.) where each element is accessed by its index. The index can be thought of as a counter or enumerator telling you how many elements you have to skip over to get to the one you're interested in. Here's where addresses come in ... the location of a particular element in memory is offset from the so-called base address (i.e. the address of the starting element) by the value
(sizeof(one element) * index #)
The C compiler is smart enough to take the sizeof() into account when you do pointer arithmetic, so you can find the address of the i-th element as (address of base) + i, rather than having to do the multiplication explicitly.
The idea of element address as offset also accounts for C's use of zero-relative array definitions. Since the first element is offset by 0 positions from the first element its index in C is 0, not 1. The second (ordinal) element is offset from the first by 1 position so its index is 1, and so on.
This comment has been removed by the author.
ReplyDeletewhat are the other difference of the pointer and array??
ReplyDelete