Pointer and Array in C Programming Language
Pointer and Array
- pointer and array are two variables that are used in a similar manner, they are both used to connect data and address.
- despite their similar purpose, they have notable differences.
- pointer, by definition, store the address of another variable. They take different addresses as value.
- array, on the other hand, saves data in a structure to be accessed individually or as a group. All the elements are homogeneous, can be non sequential, and have fixed addresses
- pointer syntax :<type> *ptr_name
- example: ptr=i; *ptr=5. This means that i=5
- pointer to pointer: ptr=i; ptr_ptr=ptr.
- *ptr=5. This means that i=5
- **ptr_ptr=9. This means that i=9 or *ptr=9
- array syntax : <type> array_value[value_dim];
- This consists of a type, identifier, operator, and dimensional value
- example: int A[5]; this means that the array has 5 elements
- array can also be initialized without dimensional value declarations
- example: int A[]; {1,2,3,4}; this means the array has 4 elements
- example: int A[5]; {1,2,3,4}; this means the array has 5 elements with 4 declared.
- array can also be two dimensional and three dimensional
- example: int A[2][2]; two dimensional
- example: int A[2][2][2]; three dimensional
- array and pointer can be combined to create an array of pointers
Comments
Post a Comment