C - Pointers - Some examples

We can see with examples below that myString[0] == *myString == *(&myString[0]) == H. Why? Because myString[0] points the first character of the string “Hello World”, in this case “H”. Same for *myString that is the value of the first character of the string. Finally &myString[0] is the address of the first character (H), and with the * before we retrieve the value of this address, in this case “H”. ...

January 26, 2011 · Mi-K

C - Library functions - Using free()

The free() function is the sister of the malloc() function. As malloc() returns the address of the first byte of the allocated memory, free() accepts, as only parameter, the address of this first byte. /* ** Made by BadproG */ #include <stdlib.h> #include <stdio.h> int main() { char *myString; myString = malloc(sizeof(myString)); if (myString == NULL) { printf("Can not malloc(), not enough space in the memory!\n"); exit(1); } else { printf("OK memory is allocated!\n"); free(myString); } return (0); }

January 25, 2011 · Mi-K

C - Unary operators - Using sizeof()

The sizeof() unary operator is often used, so it is important to know some tricks that we can do with it. It is used to calculate the sizes of datatypes and it takes one argument: the type. The main purpose of sizeof() is of course to allocate memory in conjuction with malloc. 1. Displaying size of types In the example below, we can note the 1 byte of the *myString. ...

January 25, 2011 · Mi-K

C - General Programming - Beep function

In this tutorial we will see a simple beep function. Let’s take an example of the beep function: /* ** Made by BadproG.com */ #define SOUND_IT(x) (x-'@') #include <stdio.h> void beep(void) { putchar(SOUND_IT('G')); fflush(stdout); } int main() { beep(); } Just compile and each time you will execute it, you will hear a simple beep.

January 20, 2011 · Mi-K