Arrays and Pointers in C

Pointers and arrays are closely related; an array name without an index is functionally a pointer to the first element in the array.

#include <stdio.h>


int main(void){
 
  char arrayEmp[10];
 
  printf("&arrayEmp[0] = %p\n", &arrayEmp[0]);
  printf("arrayEmp = %p\n", arrayEmp);
 
 
  return 0;
 
}

As we have seen above, an array identifier without an index generates a pointer. Likewise, we can use offsets to index a pointer to an array.

#include <stdio.h>

int main(void){
 
  int i;
 
  int arrayEmp[5];
  int *arrayPtr;
  arrayPtr = arrayEmp;
 
  *(arrayPtr+0) = 42;
  *(arrayPtr+1) = 73;
  *(arrayPtr+2) = 1138;
  *(arrayPtr+3) = 1701;
  *(arrayPtr+4) = 8086;
 
 
  for(i=0;i<5;i++){
    printf("arrayEmp[%d] = %d\n", i, arrayEmp[i]);
  }
 
 
  return 0;
 
}

Remember, arrays start at zero. To access the fifth element, we must use the index number 4. Thanks to pointer arithmetic, we have two different ways to access an array element.

#include <stdio.h>

void printString1(const char *str);
void printString2(const char *str);
void printString3(const char *str);

int main(void){
 
 
  char str[30]={'T','h','e',' ','c','a','k','e',' ','i','s',' ','a',' ','l','i','e','\n',''};

 
  printString1(str);
  printString2(str);
   printString3(str);
  return 0;
 
}


void printString1(const char *str){
  int i;
  for(i=0; str[i]; i++){
    putchar(str[i]);
  }
}


void printString2(const char *str){
  while(*str){
    putchar(*str++);
  }
}

void printString3(const char *str){
  int i = 0;
  while(*(str+i)){
    putchar(*(str+i));
    i++;
  }
}

C allows for arrays of more than two dimensions; although, arrays of more than three dimensions are relatively rare. In multidimensional arrays, it takes the computer time to compute each index. This means that accessing an element in an multidimensional array can be slower than accessing an element in a single-dimension array.

When passing multidimensional arrays into functions, we must declare all but the leftmost dimension.

Pointers are sometimes used to access array elements because pointer arithmetic is faster than array indexing.

#include <stdio.h>

#define LINELENGTH 5

void printLine(int *intArray, int size);

void printArray(int *intArray, int size);

int main(void){
 
  int intArray[3][LINELENGTH];
 
  int *arrayPtr;
 
  int i, j;
 
  for(i=0; i<3; i++){
   for(j=0; j<5; j++){
     intArray[i][j]=i+j;
   }
  }
 
  printLine(intArray[0], LINELENGTH);
 
  putchar('\n');
 
  printLine(intArray[1], LINELENGTH);
 
  putchar('\n');
 
  printLine(intArray[2], LINELENGTH);
 
  printf("\n\n");
 
  arrayPtr = &intArray[0][0];
 
  printArray(arrayPtr, 3*LINELENGTH);
   
  putchar('\n');
 
  return 0;
 
}



void printLine(int *intArray, int size){
  while(size-->0){
      printf("%d\t", *intArray++);
  }
}


void printArray(int *intArray, int size){
  while(size-->0){
    printf("%d\t", *intArray++);
  }
}

A two dimensional array can be reduced to a pointer to an array of one-dimensional arrays. We can use a separate pointer variable as a way to use pointers to access elements within a row of a two-dimensional array.

#include <stdio.h>


int main(void){
 
  int intArray[5][7];
 
  int i = 0;
  int j = 0;
  int *ptr;
 
  //set pointer to second row
  ptr = &intArray[2][0];
 
  for(i=0; i<7; i++){
    *(ptr+i) = i * 2;
  }
 
 
  for(i=2, j=0; j<7; j++){
    printf("%d\t", *(intArray[i]+j));
  }
 
  putchar('\n');
 
  for(i=0; i<5; i++){
      ptr = &intArray[i][0];
      for(j=0; j<7; j++){
    *ptr++ = i + j;
      }
  }
 
  //set pointer to fourth row
  ptr = &intArray[4][0];
 
  for(i=0; i<7; i++){
    printf("%d\t", *(ptr+i));
  }
 
  putchar('\n');
 
  for(i=0; i<7; i++){
    printf("%d\t", *ptr++);
  }
 
  return 0;
 
}

For a more thorough look at pointers and the C language, check out my book http://www.amazon.com/Big-Als-C-Standard-ebook/dp/B00A4JGE0M/

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s