Documente online.
Zona de administrare documente. Fisierele tale
Am uitat parola x Creaza cont nou
 HomeExploreaza
upload
Upload




Using Pointers with Arrays in C programming

software


Using Pointers with Arrays

Arrays and pointers are intimately linked in C. To use arrays effectively, you have to know how to use pointers with them. Fully understanding the relationship between the two requires several weeks or even months of study, so do not get discouraged if you do not understand it right away.



Let's start with the treatment of arrays in 13113t197n Pascal and other languages. C is nothing like Pascal in this regard, so it will provide a good contrast. Following is an example of arrays in Pascal:



program samp;
const
max=9;
var
a,b:array[0..max] of integer;
i:integer;
begin
for i:=0 to max do
a[i]:=i;
b:=a;
end.


The elements of the array a are initialized, and then all elements in a are copied into b, so that a and b are identical. Compare the C version:



#define MAX 10

void main()



Enter this code and try to compile it. You will find that C will not compile it. If you want to copy a into b, you have to enter something like the following:



for (i=0; i<MAX; i++)
a[i]=b[i];


Or, to put it more succinctly:



for (i=0; i<MAX; a[i]=b[i], i++);


Better yet, use the memcpy utility in string.h.

Arrays in C are unusual in that variables a and b are not, technically, arrays themselves but permanent pointers to arrays. Thus, they point to blocks of memory that hold the arrays. They hold the addresses of the actual arrays, but since they are permanent pointers, you cannot change their addresses. The statement a=b; therefore does not work.

Because a and b are pointers, you can do several interesting things with pointers and arrays. For example, the following code works:



#define MAX 10

void main()



The statement p=a; works because a is a pointer. Technically, a points to the address of the 0th element of the actual array. This element is an integer, so a is a pointer to a single integer. Therefore, declaring p as a pointer to an integer and setting it equal to a works. Another way to say exactly the same thing would be to replace p=a; with p=&a[0];. Since a contains the address of a[0], a and &a[0]mean the same thing.

Now that p is pointing at the 0th element of a, you can do some rather strange things with it. The a variable is a permanent pointer and can not be changed, but p is not subject to such restrictions. C actually encourages you to move it around using pointer arithmetic . For example, if you say p++;, the compiler knows that p points to an integer, so this statement increments p the appropriate number of bytes to move it to the next element of the array. If p were pointing to an array of 100-byte-long structures, p++; would move p over by 100 bytes. C takes care of the details of element size.

You can copy the array a into b using pointers as well. The following code can replace (for i=0; i<MAX; a[i]=b[i], i++); :



p=a;
q=b;
for (i=0; i<MAX; i++)



You can abbreviate this code as follows:



p=a;
q=b;
for (i=0; i<MAX; i++)
*q++ = *p++;


And you can further abbreviate it to:



for (p=a,q=b,i=0; i<MAX; *q++ = *p++, i++);


What if you go beyond the end of the array a or b with the pointers p or q? C does not care-it blithely goes along incrementing p and q, copying away over other variables with abandon. You need to be careful when indexing into arrays in C, because C assumes that you know what you are doing.

You can pass an array such as a or b to a function in two different ways. Imagine a function dump that accepts an array of integers as a parameter and prints the contents of the array to stdout. There are two ways to code dump:



void dump(int a[],int nia)



or:



void dump(int *p,int nia)



The nia (number_in_array) variable is required so that the size of the array is known. Note that only a pointer to the array, rather than the contents of the array, is passed to the function. Also note that C functions can accept variable-size arrays as parameters, which is not possible in Pascal.


Document Info


Accesari: 992
Apreciat: hand-up

Comenteaza documentul:

Nu esti inregistrat
Trebuie sa fii utilizator inregistrat pentru a putea comenta


Creaza cont nou

A fost util?

Daca documentul a fost util si crezi ca merita
sa adaugi un link catre el la tine in site


in pagina web a site-ului tau.




eCoduri.com - coduri postale, contabile, CAEN sau bancare

Politica de confidentialitate | Termenii si conditii de utilizare




Copyright © Contact (SCRIGROUP Int. 2024 )