Why use pointers over normal variables?
Short answer is: Don’t. ๐ Pointers are to be used where you can’t use anything else. It is either because the lack of appropriate functionality, missing data types or for pure perfomance. More below…
When and where should I use pointers?
Short answer here is: Where you cannot use anything else. In C you don’t have any support for complex datatypes such as a string. There are also no way of passing a variable “by reference” to a function. That’s where you have to use pointers. Also you can have them to point at virtually anything, linked lists, members of structs and so on. But let’s not go into that here.
How do you use pointers with arrays?
With little effort and much confusion. ๐ If we talk about simple data types such as int and char there is little difference between an array and a pointer.
These declarations are very similar (but not the same – e.g., sizeof will return different values):
char* a = “Hello”;
char a[] = “Hello”;
You can reach any element in the array like this
printf(“Second char is: %c”, a[1]);
Index 1 since the array starts with element 0. ๐
Or you could equally do this
printf(“Second char is: %c”, *(a+1));
The pointer operator (the *) is needed since we are telling printf that we want to print a character. Without the *, the character representation of the memory address itself would be printed. Now we are using the character itself instead. If we had used %s instead of %c, we would have asked printf to print the content of the memory address pointed to by ‘a’ plus one (in this example above), and we wouldn’t have had to put the * in front:
printf(“Second char is: %s”, (a+1)); /* WRONG */
But this would not have just printed the second character, but instead all characters in the next memory addresses, until a null character ( ) were found. And this is where things start to get dangerous. What if you accidentally try and print a variable of the type integer instead of a char pointer with the %s formatter?
char* a = “Hello”;
int b = 120;
printf(“Second char is: %s”, b);
This would print whatever is found on memory address 120 and go on printing until a null character was found. It is wrong and illegal to perform this printf statement, but it would probably work anyway, since a pointer actually is of the type int in many environments. Imagine the problems you might cause if you were to use sprintf() instead and assign this way too long “char array” to another variable, that only got a certain limited space allocated. You would most likely end up writing over something else in the memory and cause your program to crash (if you are lucky).
Oh, and if you don’t assign a string value to the char array / pointer when you declare it, you MUST allocate sufficient amount of memory to it before giving it a value. Using malloc, calloc or similar. This since you only declared one element in your array / one single memory address to point at. So here’s a few examples:
char* x;
/* Allocate 6 bytes of memory for me and point x to the first of them. */
x = (char*) malloc(6);
x[0] = ‘H’;
x[1] = ‘e’;
x[2] = ‘l’;
x[3] = ‘l’;
x[4] = ‘o’;
x[5] = ‘