Arrays and Pointers
As we saw arrays and pointers are very similar. Just as a block of memory can be allocated for a pointer and the starting address assigned to a pointer, a block of memory is allocated for an array in the same fashion.
If we say
int array1[10] ;
Then a block of 10 integers is allocated in RAM and the starting address is assigned to the variable "array1" . The key difference between array variables and pointer variables is that a pointer variable's value can be changed but an array variable's value can never be changed. We can access elements in an array using pointer notation and vice versa.
We can obtain the address of the array variable in many ways. Ex:
/* Array address*/
#include<stdio.h>
#include <stdlib.h>
main()
{
int array1[] = {100,200,300} ;
int* ptr1 ;
printf( "%p\n" , array1) ;
printf( "%p\n" , &array1) ;
printf( "%p\n" , &(array1[0]) ) ;
}
The above program has an array declared with the name "array1" . A block of 3 elements is allocated and the address assigned to "array1" . There are many different ways of getting the address. We can just use the array name "array1" and that is probably the easiest way to obtain the address. We can use "&array1" that does the same thing. Since the first element is at the starting address and if we take the address of that using the notation "&(array1[0])" then we have the same address also.
Next example will show how an array can be treated as a pointer and vice-versa.
/* Array and pointers */
#include<stdio.h>
#include <stdlib.h>
main()
{
int array1[] = {100,200,300} ;
int* ptr1 ;
//An array can be treated as a pointer
//array1 = ptr1 ;
ptr1 = array1 ;
printf( "%d\n" , *ptr1 ) ;
printf( "%d\n" , *(ptr1+1) ) ;
printf( "%d\n" , *array1 ) ;
printf( "%d\n" , *(array1+1) ) ;
// A pointer can be treated as an array.
ptr1 = (int*)malloc( sizeof(int) * 10 ) ;
*ptr1 = 10 ;
*(ptr1+1) = 20 ;
printf( "%d\n" , ptr1[0] ) ;
printf( "%d\n" , ptr1[1] ) ;
free ( ptr1) ;
}
As the above example shows we can have an array variable "array1" . We can access elements using "*array1" and "*(array1+1)" .
As we stated earlier a pointer can hold the address of a single element or a block. When it holds the address of the block then it acts like an array. We can also think of a pointer as an array. What about an array of pointers ? Each element is a pointer and if we think of that as an array , then we have a array of arrays or in other words a 2 dimensional array.
Addresses
The following shows how to use the printf formatting options to print the same variable
in different ways.
File: c1.cpp
#include<stdio.h>
#include<stdlib.h>
int main()
{
char* str1 = "string1" ;
char str2[] = "string2" ;
char ch = 'A' ;
int x1 = 100 ;
printf( "%s\n", str1 ) ;
printf( "%s\n", str2 ) ;
printf( "%c\n", ch ) ;
printf( "Address of str1:%p\n", str1 ) ;
printf( "Address of str1:%p\n", str2 ) ;
printf( "Address of character: %p\n", &ch ) ;
printf( "Address of x1: %p\n", &x1 ) ;
}
Output:
string1
string2
A
Address of str1:0x100403000
Address of str1:0xffffcc10
Address of character: 0xffffcc0f
Address of x1: 0xffffcc08
String Initialization
File: "c2.c"
#include<stdio.h>
#include<stdlib.h>
int main()
{
char* str1 = "string1" ;
char str2[] = "string2" ;
//Compiler error. str1 is a pointer to a read only string
//str1[0] = 'a' ;
str2[0] = 'a' ;
}
The above shows 2 ways to initialize a character string. The difference is that the "str1" is a pointer that points to a read only area that contains the string "string1". We cannot do anything with the pointer. If we try something like:
str1[0] = 'a'
We receive a "segmentation" error with the above. In general the array notation "str2[]" should be used.