Strings

A string is just a character array with the convention that the end of the valid data is marked by a null '\0'. Manipulating strings is very much a matter of pointers and special string functions. For example, the strlen(str) function returns the number of characters in the string str. It does this simply by counting the number of characters up to the first null in the character array - so it is important that you are using a valid null-terminated string. Indeed this is important with all of the C string functions.

For example:
char a[l0],b[10];
b = a;

does not appear to make a copy of the characters in a, but this is an illusion. What actually happens is that the pointer b is set to point to the same set of characters that a points to, i.e. a second copy of the string isn't created.

You need strcopy(a,b) which really does make a copy of every character in a in the array b up to the first null character. In a similar fashion strcat(a,b) adds the characters in b to the end of the string stored in a. Finally there is the all-important strcmp(a,b) which compares the two strings character by character and returns true - that is 0 - if the results are equal.

You can see that you need to understand pointers to avoid making simple mistakes using strings. One last problem is how to initialise a character array to a string. However, you can use:

strcopy(a,"hello")

because a string constant is passed in exactly the same way as a string variable, i.e. as a pointer. The main disadvantage of this method is that many compilers use an optimisation trick that results in only a single version of identical constants being stored.

For example:
strcopy(b,"hello");

A much better method is to use array initialisation. You can specify constants to be used to initialise any variable when it is declared.

For example:
int a=10;

declares a to be an integer and initialises it to 10. A character array can be initialised in the same way.

For example:
char a[5]={'h','e','l','l','o'};

but a much better way is to write:

char a[6]="hello";

and let the compiler work out how many array elements are needed. Some compilers cannot cope with the idea of initialising a variable that doesn't exist for the entire life of the program. For those compilers to make initialisation work you need to add the keyword static to the front of the string declaration, therefore:

static char a[] = "hello";

Study this program carefully with particular attention to the way arrays, array elements and variables are passed. It is worth saying that in some cases there are better ways of achieving the same results. In particular, it would have been easier not to use the variable done, but to have returned the state as the result of the scan function.

#include
void randdat(int a[] , int n);
void sort(int a[] , int n);
void scan(int a[] , int n , int *done);
void swap(int *a ,int *b);
main()
{
int i;
int a[20];
randdat(a , 20);
sort(a , 20);
for(i=0;i<20;++i) printf("%d\n" ,a[i]);
}
void randdat(int a[1] , int n)
{
int i;
for (i=0 ; i a[i] = rand()%n+1;
}
void sort(int a[1] , int n)
{
int done;
done = 1;
while(done == 1) scan(a , n , &done);
}
void scan(int a[1] , int n , int *done)
{
int i;
*done=0;
for(i=0 ; i {
if(a[i] {
swap(&a[i],&a[i+1]);
*done=1;
}
}
}
void swap(int *a ,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}