0% found this document useful (0 votes)
13 views

Arrays and Pointers

Arrays and pointers are related, as an array is a pointer to its first element. This allows arrays to be passed to functions and modified without explicit dereferencing. While arrays can have their elements modified, the array itself is treated as a constant in the declaring function. Function parameters can specify arrays in two ways, either as type name[] or type *, with both allowing arrays of any size to be passed.

Uploaded by

dhanunjayarao
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Arrays and Pointers

Arrays and pointers are related, as an array is a pointer to its first element. This allows arrays to be passed to functions and modified without explicit dereferencing. While arrays can have their elements modified, the array itself is treated as a constant in the declaring function. Function parameters can specify arrays in two ways, either as type name[] or type *, with both allowing arrays of any size to be passed.

Uploaded by

dhanunjayarao
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Arrays and Pointers

To fully understand the workings of C you must know that pointers and arrays are related.

An array is actually a pointer to the 0th element of the array. Dereferencing the array name will
give the 0th element. This gives us a range of equivalent notations for array access. In the
following examples, arr is an array.

There are some differences between arrays and pointers. The array is treated as a constant in the
function where it is declared. This means that we can modify the values in the array, but not the
array itself, so statements like arr ++ are illegal, but arr[n] ++ is legal.

Since an array is like a pointer, we can pass an array to a function, and modify elements of that
array without having to worry about referencing and de-referencing. Since the array is
implemented as a hidden pointer, all the difficult stuff gets done automatically.

A function which expects to be passed an array can declare that parameter in one of two ways.

Either of these definitions is independent of the size of the array being passed. This is met most
frequently in the case of character strings, which are implemented as an array of type char. This
could be declared as char string[]; but is most frequently written as char *string; In the same
way, the argument vector argv is an array of strings which can be supplied to function main. It
can be declared as one of the following.

Don't panic if you find pointers confusing. While you will inevitably meet pointers in the form of
strings, or as variable arguments for functions, they need not be used in most other simple types
of programs.

You might also like