Pointers and arrays in C

From Wikiversity

(Redirected from Pointers and Arrays)
Jump to: navigation, search

The C language provides arrays to store a series of elements of one data type. It also has pointers. Though pointers have been removed out of some modern languages such as Java, it still remains a topic of interest.

Contents

[edit] Introduction to Arrays

Let us start with an analogy. Suppose that you have a cupboard in your house. You would like to keep books on each shelf of the cupboard. It would be nice to keep Books related to one subject on one rack, books of another subject on another rack. A rack is an array. A rack is used to store books of the same subject. Similarly, an array can store data of the same type.

e.g. Array of 5 integers, array of 10 floating point nos., array of characters.


1 2 3 4 5

An array of 5 Numbers

The syntax for declaring an array is :

data_type array_name[size];

int x[5]; /* Array of 5 integers */
float y[6]; /* Array of 6 floating point numbers */
char string[10]; /* Character array of length 10 */

The starting element of the array is called 0th element.
The next element is 1st element.
...
The last element is (n-1)th element, where n is the size of the array.

[edit] Array Initialization

Suppose we have an array 'x'.
0th element of x is denoted by x[0]
1st element of x is denoted by x[1]
... (n-1)th element of x is denoted by x[n-1]

When we declare an array, the array is initially empty. The array does not contain any values. We can initialise this array as follows -

int x[5] = {5, 7, 2, 3, 8};

Array.jpg

Or we can let the compiler do the work, and initialize it like this -

int x[] = {5, 7, 2, 3, 8};

The compiler will count the number of initializers and create the array with five elements.

[edit] More Array Initialization

Let us see a small code snippet that prints the number of days per month.

#include<stdio.h>
#define MONTHS 12
 
int main(void)
{
  int days[MONTHS] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  int i;
 
  for (i = 0; i < MONTHS; i++)
  {
    printf("Month %d has %d days.\n", i+1, days[i]);
  }
 
  /* indicate success */
  return 0;
}
 
/*
 * The output looks like this:
 * Month 1 has 31 days.
 * Month 2 has 28 days.
 * Month 3 has 31 days.
 * Month 4 has 30 days.
 * Month 5 has 31 days.
 * Month 6 has 30 days.
 * Month 7 has 31 days.
 * Month 8 has 31 days.
 * Month 9 has 30 days.
 * Month 10 has 31 days.
 * Month 11 has 30 days.
 * Month 12 has 31 days.
 */

It is better to leave it to the compiler to count the elements of the array. So lacking faith in our ability to count correctly, we let the computer give us the size of array.

#include<stdio.h>
 
int main(void)
{
  int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; /* Use empty brackets */
  int i;
 
  for (i = 0; i < sizeof days / sizeof (int); i++)
  {
    printf(" Month %d has %d days. \n",i+1,days[i]);
  }
 
  /* indicate success */
   return 0;
}

[edit] Assigning Array Values

We can assign values to array members by using array index.

int x[5];
int i;
 
for (i = 0; i < 5; i++)
{  
  x[i] = i+1;
}

Thus the array x will contain the elements 1, 2, 3, 4, 5.

[edit] Functions and Arrays

[edit] Passing Arrays to a Function

Arrays cannot be passed to functions. If an array name is used as an argument in a function call, the address of the first element of the array is passed. The function can access the array through that address.

Suppose we want to write a function that returns the sum of elements of the array.

#include <stdio.h>
 
/* prototype */
int sum(int a[]);
 
int main(void) 
{
    int marbles[] = {5, 6, 2, 3, 7};
    int ans;
 
    ans = sum(marbles);
    printf("The total number of marbles: %d",ans);
 
    return 0;
}
 
int sum(int arr[])
{
    int i, s = 0;
    for (i = 0; i < 5; i++)
    {
        s = s + arr[i];
    }
    return s;
}

Note that the array name marbles is used as an argument to the function. The function is expecting an array because its parameter is defined as int arr[]. What is actually passed is the address of marbles[0]. The function then can access the array using the [n] notation.
ans = sum(marbles);
When defining the function we write -
int sum(int arr[])
Since marbles has been passed to the function, arr will refer to the marbles array inside the function.

[edit] How Are Arrays Stored in Memory?

When we declare an array
int arr[size]
space is reserved in the memory of the computer for the array. The elements of the array are stored in these memory locations. The important thing about arrays is that array elements are always stored in consecutive memory locations. We can verify this fact by printing the memory addresses of the elements. (Just like every person has a street address, every location in the memory has a memory address, usually a number, by which it can be uniquely identified.)

#include<stdio.h>
 
int main(void)
{
    int a[] = {1,2,3,4,5,6,7,8,9,10};
    int i;
 
    /* Print the Addresses */
    for (i = 0; i < 10; i++)
    {
        printf("\nAddress of a[%d] : %p", i, (void *) &a[i]);
    }
 
    /* indicate success */
    return 0;
}
 
/* OUTPUT
 
Address of a[0] : ffe2
Address of a[1] : ffe4
Address of a[2] : ffe6
Address of a[3] : ffe8
Address of a[4] : ffea
Address of a[5] : ffec
Address of a[6] : ffee
Address of a[7] : fff0
Address of a[8] : fff2
Address of a[9] : fff4
 
*/

As we can see from my implementation, the elements are stored as ffe2, ffe4, ffe6,... But you might wonder why they are not consecutive. The reason is very simple. The size of the int data type in C is at least 2 bytes (depending on the implementation). In this example it is 2 bytes wide, so a[0] will be stored at ffe2, ffe3, a[1] will be stored at ffe4, ffe5, a[2] will be stored at ffe6, ffe7 and so on. Note that the way the %p conversion specification displays values is implementation-dependent, so the values as well as being different, may be also displayed differently on your system.

In place of int, if we use an array of type float and each float uses 4 bytes, we will find a[0] will be stored at ffe2, ffe3, ffe4, ffE5, a[1] will be stored at ffe6, ffe7, ffe8, ffe9 and so on.



Project: Topic:C
Previous: Functions and Methods — Pointers and arrays in C — Next: String handling in C