First and foremost, I will manifest one of the prerequisites of Strings and pointers, Arrays, but before I commence to spiel the technical details, I want to point out the purposive of them. One: they are useful for lists of information, such as telephone numbers, grade lists etc.. Two: You need arrays in C in order to use strings, because C has no built-in string data type. Now that you have been briefed, we shall move on.
Arrays are a list of variables of the equivalent data type that are seperated by what you call "array elements". To declare an array, use the form of
Code: Select all
type var_name[size];
Code: Select all
int arrayi[20];
e.g.
Code: Select all
int array1[20], array2[20];
array1 = array2; /* THIS IS WRONG */
If you desire to copy one array to the other, it's essential that you copy it element by element. To copy element by element, you could dissipate your time and do
Code: Select all
array1[0] = array2[0];
array1[1] = array2[1];
array1[2] = array2[2];
....and so on.
Code: Select all
int i;
for(i = 0; i < 20; i++) array1[i] = array2[i];
This is all that is imperative to know about arrays in regards to strings.
Strings
-------
First I want to properly delineate a string. A string in C is a null-terminated character array. Null being equivalent to 0. So when declaring a string array it is necessary that you assign it to be one byte larger than the string.
Due to a string being a null-terminated character array, you declare it like so
Code: Select all
char var_name[size];
The method we will use to read in strings is the gets() function which is in the "stdio.h" header file. When you read in a string using the gets() function, you simply supply the name of the character array, without the index.
e.g.
Code: Select all
char string[80];
gets(string);
Code: Select all
printf(string);
Code: Select all
printf("%s\n", string);
strlen() - returns the length, in characters, of a string. strlen() does not count the null terminator.
syntax strlen(string);
strcpy() - copies contents into variable.
syntax strcpy(to, from);
strcat() - adds contents of one string to another.
syntax strcat(to, from);
strcmp() - compares to strings.
syntax strcmp(string1, string2);
strcmp() returns 0 if the strings are the same. It returns less than 0 if string1 is less than string2. It returns greater than 0 is string1 is greater than string2.
One string is not greater than the other because it has more characters, strcmp() compares strings lexicographically, which means in dictionary order. Therefore, a string is less than another when it would appear before the other in a dictionary, and greater than when it appears after the other. The comparison is also case sensitive, lowercase being greater than uppercase.
These string-related functions are found in the "string.h" header file.
Pointers
----------
Ahh, C's most important, but also most troublesome feature. Pointers. A pointer is a variable that holds the memory address of another object.
e.g. If a variable called point1, contains the memory address of a variable named point2. It is said that point1 points to point2.
To declare a pointer, use the form of
Code: Select all
type *var_name
e.g.
Code: Select all
int *point
C contains two special pointer operators, * and &. The & operator returns the address of the variable it precedes. The * operator returns the value stored at the address that it precedes.
Well I was expecting to write more about pointers, but I am too ridiculously lazy. Maybe I will create another tutorial that goes more in depth. Please excuse errors, this was a 20 minute tutorial :p