golb

C

Understand C pointer with examples

The program used :

#include <stdio.h>

int main(){
    int number = 7;
    int *pointerTOnumber = &number;
    printf("%%d number = %d\n", number);
    printf("%%p number = %p\n", number);
    printf("%%d &number = %d\n", &number);
    printf("%%p &number = %p\n", &number);
    printf("%%d pointerTOnumber = %d\n", pointerTOnumber);
    printf("%%p pointerTOnumber = %p\n", pointerTOnumber);
    printf("%%d &pointerTOnumber = %d\n", &pointerTOnumber);
    printf("%%p &pointerTOnumber = %p\n", &pointerTOnumber);
    printf("%%d *pointerTOnumber = %d\n", *pointerTOnumber);
    printf("%%p *pointerTOnumber = %p\n", *pointerTOnumber);
}

Note that %% is used to print %

The result:

%d number = 7                            // value of number in decimal form
%p number = 0x7                          // value of number in Hex since %p
%d &number = -1104070188                 // value of the address of number in decimal form
%p &number = 0x7ffdbe3139d4              // value of the address in Hex since %p
%d pointerTOnumber = -1104070188         // value of the address of number in decimal form
%p pointerTOnumber = 0x7ffdbe3139d4      // value of the address of number in Hex since %p
%d &pointerTOnumber = -1104070184        // value of the address of the pointer in decimal form
%p &pointerTOnumber = 0x7ffdbe3139d8     // value of the address of the pointer in Hex since %p
%d *pointerTOnumber = 7                  // value pointed to by the pointer (thus that of number) in decimal form
%p *pointerTOnumber = 0x7                // value pointed to by the pointer (thus that of number) in Hex since %p

Caption:

Compilation

Input Name Output
.c Preprocessor .i
.i Compiler .s
.s Assembler .o
.o Linker/Loader .out

Vocabulary

main Parameters in C

argv[0] will always contain the name of the program

int main(int argc, char *argv[]){
    for(int o=0; o < argc; o++){
        printf("Argument %d : %s\n", o, argv[o]);
    }
    return 0;
}

GCC useful options

Note

#include <stdio.h>

int main()
{
    // imagine you made a mistake a write 11
    // possible infinite loop if "a" and "i" and next to each other in memory
    unsigned char a[10], i;
    for (i = 0; i < 11; i++)
    {
        a[i] = 0;
    }
    // for (i = 0; i < 10; i++)
    // {
    //     printf("%d", a[i]);
    // }
    return 0;
}

from https://www.youtube.com/watch?v=443UNeGrFoM

C Donut

Valgrind

gcc main.c -o main.out -Wall -ggdb3
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --log-file=out.txt ./main.out # you can add args

exec family functions

system function

# include <stdlib.h>

int main() {
    int ret = system("echo 'Hello, World!'");
    if(ret == -1) {
        // Failed to execute system command
    } else if (WIFEXITED(ret) && WEXITSTATUS(ret) != 0) {
        // error
    }
}

char *star vs char table[]

// array of char, the stack is filled with {'t','a','b','l','e', '\0'}
char table[] = "Table";
// pointer to a static memory
char *star = "Star";

// that's why you cannot edit star
star[0] = 's'; // segfault