Problem description:
|
|
|
Example program illustrating the add operation of a reference typed value and a integer:
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] )
{
int *A; // sizeof( *A ) = sizeof(int) = 4
int i = 3;
A = malloc( 10*sizeof(int) );
printf("A = %p\n", A);
printf("A+1 = %p\n", A+1);
printf("A+2 = %p\n", A+2);
printf("A+i = %p\n", A+i);
}
|
Output: 0x557946b49260, 0x557946b49264, 0x557946b49268 and 0x557946b4926c
This diagram shows that A+i is the address of the array element A[i] (for an int typed array) assuming that A contains the value 8000:
The value in A is irrelevant as long as it is equal to the base address of the (dynamic) array
When we use a short * pointer, the increase is by 2 bytes:
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] )
{
short *A; // sizeof( *A ) = sizeof(short) = 2
int i = 3;
A = malloc( 10*sizeof(int) );
printf("A = %p\n", A);
printf("A+1 = %p\n", A+1);
printf("A+2 = %p\n", A+2);
printf("A+i = %p\n", A+i);
}
|
Output: 0x564f71902260, 0x564f71902262, 0x564f71902264 and 0x564f71902268
This diagram shows that A+i is the address of the array element A[i] (for an short typed array) assuming that A contains the value 8000:
The value in A is irrelevant as long as it is equal to the base address of the (dynamic) array
When we use a double * pointer, the increase is by 8 bytes:
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] )
{
double *A; // sizeof( *A ) = sizeof(double) = 8
int i = 3;
A = malloc( 10*sizeof(int) );
printf("A = %p\n", A);
printf("A+1 = %p\n", A+1);
printf("A+2 = %p\n", A+2);
printf("A+i = %p\n", A+i);
}
|
Output: 0x557946b49260, 0x557946b49268, 0x557946b49270 and 0x557946b49278
This diagram shows that A+i is the address of the array element A[i] (for an double typed array) assuming that A contains the value 8000:
The value in A is irrelevant as long as it is equal to the base address of the (dynamic) array
We can use the + operation on pointer variables to access elements in a dynamic array of any datatype as follows:
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] )
{
int *A;
int i;
A = malloc( 10*sizeof(int) ); // Create a dynamic array
for ( i = 0; i < 10; i++ )
*(A + i) = i*i; // Assigns i^2 to A[i]
for ( i = 0; i < 10; i++ )
printf("%d\n", *(A + i) );
}
|
|