Topic : Introduction to C Programming
Author : Mkoubaa
Page : << Previous 9  Next >>
Go to page :


class=code>

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)  
{    
    int i;    
     for (i=0; i<nia; i++)      
        printf("%d\n",a[i]);  
}

or:

void dump(int *p,int nia)  
{    
    int i;    
     for (i=0; i<nia; i++)      
        printf("%d\n",*p++);  
}


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.

Introduction to C Programming
Part 12: 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 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()  
{    
    int a[MAX];    
    int b[MAX];    
    int i;
     for(i=0; i<MAX; i++)
        a[i]=i;  
    b=a;  
}


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()  
{    
    int a[MAX];    
    int b[MAX];    
    int i;    
    int *p,*q;
    
    for(i=0; i<MAX; i++);  
        a[i]=i;  
    p=a;    
    printf("%d\n",*p);  
}


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++)    
{      
    *q = *p;      
    q++;      
    p++;    
}


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)  
{    
    int i;    
     for (i=0; i<nia; i++)      
        printf("%d\n",a[i]);  
}


or:

void dump(int *p,int nia)  
{    
    int i;    
     for (i=0; i<nia; i++)      
        printf("%d\n",*p++);  
}


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.

Introduction to C Programming
Part 13: Strings in C


Strings in C are intertwined with pointers to a large extent. You must become familiar with the pointer concepts covered in tutorial 9 through tutorial 12 to use C strings effectively. Once you get used to them, however, you can often perform string manipulations more efficiently than you can in Pascal.
A string in C is simply an array of characters. The following line declares an array that can hold a string of up to 99 characters.

char str[100];

It holds characters as you would expect: str[0] is the first character of the string, str[1] is the second character, and so on. But why is a 100-element array unable to hold up to 100 characters? Because C uses null-terminated strings, which means that the end of any string is marked by the ASCII value 0 (the null character), which is also represented in C as '\0'.
Null termination is very different from the way Pascal compilers handle strings. In Pascal, each string consists of an array of characters, with a length byte that keeps count of the number of characters stored in the array. This structure gives Pascal a definite advantage when you ask for the length of a string. Pascal can simply return the length byte, whereas C

Page : << Previous 9  Next >>