Topic : C Tutorial
Author : Ryan Parsons
Page : << Previous 3  
Go to page :


a colon, not a semicolon!!!. Then when you want it to go there, type "goto" then the name of the label followed by a semicolon.

Part three


Ok, you've learned a lot of the very basics by now, so now you get to learn some of the slightly more advanced stuff. The book that I learned from introduced pointers at this point, but that just confused me and it doesn't help any at this point. Pointers will be introduced later, you will understand their uses then and be able to implement them better.
Lesson one
This lesson will introduce arrays and little bit about strings; and since somehow I forgot to explain a for loop, you'll learn that too. If you get confused about strings at any point, just remember: a string is just an array of characters, it might have more functions to help manipulate them, but it is an array all the same.
An array is basically a list of of the same data types. You can make it a really wide range of sizes (in some compilers that I've seen, they limit the size to 1000 members) at least 2 to at least 1000. An array of integers would look like this: int array[10]; This would give you an array of 10 different int values that are all referenced by the same name. array[0] would be the first value, array[1] would be the second and so on. So to assign a value to an array member you would simply type array[0] = 3; or whatever you needed. Basically its just like any other variable. If you needed to assign all the elements a value when you declare the array, then it would look like int array[5] = { 0, 1, 2, 3, 4};.
Here's a simple program to explain arrays a little bit better:


#include stdio.h

int main(void)
{
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, i;
char string[20] = "This is a string";

for(i = 0; i < 10; i++)
printf("array[%d] is %d\n", i, array[i]);
for(i = 9; i >= 0; i--)
{
  array[i] = i;
  printf("array[%d] = %d", i, array[i]);
}

printf("%s\n", string);
printf("Enter a string\n");
gets(string);
printf("string now reads:\n%s\n", string);

return 0;
}


Ok, now comes the explanation. First it starts off declaring an array of ints and assigns them all values. Then it declares an array of character (string) and assigns the string the value "This is a string". That is a slightly different way than the int, but since its a string each letter occupies one element in the array. The compiler wouldn't be able to tell where the first number ended and the second started if you assigned it the same way as a string.

Please, notice that this tutorial is not finished yet!

Page : << Previous 3