- Pointers, arrays and Data Structures
- C - Pointer to Pointer
- C - Pointer to Pointer with Example
- C - Multidimensional Arrays in C
- Two dimensional (2D) arrays in C programming with example
- C Books and PDF's to check out and use as reference.
- memset : A function that fills memory with a constant byte.
- Prototype:
char *_memset(char *s, char b, unsigned int n);
- The
_memset()
function fills the firstn
bytes of the memory area pointed to bys
with the constant byteb
. - Returns a pointer to the memory area
s
. - Read more on memset here
- FYI: The standard library provides a similar function:
memset
. Runman memset
to learn more. - Compile the code this way:
gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c 0-memset.c -o 0-memset
- Prototype:
- memcpy : A function that copies the memory area.
- Prototype:
char *_memcpy(char *dest, char *src, unsigned int n);
- The
_memcpy()
function copiesn
bytes from the memory areasrc
to the memory areadest
. - Returns a pointer to
dest
- FYI: The standard library provides a similar function:
memcpy
. Runman memcpy
to learn more. - Compile the code this way:
gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c 1-memcpy.c -o 1-memcpy
- Prototype:
- strchr : A function that locates a character in a string.
- Prototype:
char *_strchr(char *s, char c);
- Returns a pointer to the first occurrence of the character
c
in the strings
, orNULL
if the character is not found. - FYI: The standard library provides a similar function:
strchr
. Runman strchr
to learn more. - Read more here on strchr.
- Compile the code this way:
gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-strchr.c -o 2-strchr
- Prototype:
- strspn : A function that gets the length of a prefix substring.
- Prototype:
unsigned int _strspn(char *s, char *accept);
- Returns the number of bytes in the initial segment of
s
which consist only of bytes fromaccept
. - FYI: The standard library provides a similar function:
strspn
. Runman strspn
to learn more. - Compile the code this way:
gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c 3-strspn.c -o 3-strspn
- Prototype:
- strpbrk : A function that searches a string for any of a set of bytes.
- Prototype:
char *_strpbrk(char *s, char *accept);
- The
_strpbrk()
function locates the first occurrence in the strings
of any of the bytes in the stringaccept
. - Returns a pointer to the byte in
s
that matches one of the bytes inaccept
, orNULL
if no such byte is found. - FYI: The standard library provides a similar function:
strpbrk
. Runman strpbrk
to learn more. - Compile the code this way:
gcc -Wall -pedantic -Werror -Wextra -std=gnu89 4-main.c 4-strpbrk.c -o 4-strpbrk
- Prototype:
- strstr : A function that locates a substring.
- Prototype:
char *_strstr(char *haystack, char *needle);
- The
_strstr()
function finds the first occurrence of the substringneedle
in the stringhaystack
. - The terminating null bytes ('\0') are not compared.
- Returns a pointer to the beginning of the located substring, or
NULL
if the substring is not found. - FYI: The standard library provides a similar function:
strstr
. Runman strstr
to learn more. - Compile the code this way:
gcc -Wall -pedantic -Werror -Wextra -std=gnu89 5-main.c 5-strstr.c -o 5-strstr
- Prototype:
- Chess is mental torture : A function that prints the chessboard.
- Prototype:
void print_chessboard(char (*a)[8]);
- Compile the code this way:
gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 7-main.c 7-print_chessboard.c -o 7-print_chessboard
- Prototype:
- The line of life is a ragged diagonal between duty and desire : A function that prints the sum of the two diagonals of a square matrix of integers.
- Prototype:
void print_diagsums(int *a, int size);
- Format: see main code.
- You are allowed to use the standard library.
- Note: that in the following example we are casting an
int[][]
into anint*
.- This is not something you should do. The goal here is to make sure you understand how an array of array is stored in memory.
- Compile the code this way:
gcc -Wall -pedantic -Werror -Wextra -std=gnu89 8-main.c 8-print_diagsums.c -o 8-print_diagsums
- Prototype: