Skip to content

Commit

Permalink
+9-double-pointers/
Browse files Browse the repository at this point in the history
  • Loading branch information
hengxin committed Nov 27, 2024
1 parent 6a65fec commit 492ec02
Show file tree
Hide file tree
Showing 19 changed files with 882 additions and 0 deletions.
10 changes: 10 additions & 0 deletions 9-double-pointers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
add_executable(selection-sort-ints selection-sort.c)
add_executable(selection-sort-strings selection-sort-strings.c)

add_executable(scores scores.c)

add_executable(echo echo.c)

add_executable(pointers-malloc pointers-malloc.c)

add_executable(decl decl.c)
20 changes: 20 additions & 0 deletions 9-double-pointers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 10-double-pointers

## `selection-sort-strings.c`

- `const`

## `echo.c`

- Linux `echo`
- C standard
- `printf("%s\n", argv[i])`: printf the nullptr

## `scores.c`

- `student_score_table`: as a 2D array
- `Print`
- `int table[][COLS]` vs. `int (*table)[COLS]`
- `malloc`
- `int *`
- `int (*)[COLS]`
21 changes: 21 additions & 0 deletions 9-double-pointers/decl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Created by hengxin on 11/27/2024.

int main() {
char **argv;

int *names[10];

int (*musician_score_table)[10];

int *StrCpyStd(char *dest, const char *src);

int (*comp)(const void *left, const void *right);

int atexit(void (*func)(void));

void (*signal(int sig, void (*handler)(int)))(int);

char (*(*func(int num, char *str))[])();

char (*(*arr[3])())[5];
}
50 changes: 50 additions & 0 deletions 9-double-pointers/echo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Echo program (command-line) arguments.
*
* Created by hengxin on 11/27/2024.
*/

#include <stdio.h>

// arg: argument
// c: count
// v: vector

// argv[0]: the name of the program
// argv[1 .. argc - 1]: command line arguments
// argv[argc]: NULL
int main(int argc, char *argv[]) {
// for version with argv
// for (int i = 1; i < argc; ++i) {
// printf("argv[%d] = %s\n", i, argv[i]);
// }

// for version with pointers
// for (char **ptr = argv + 1; *ptr != NULL; ptr++) {
// printf("%s\n", *ptr);
// }

// while version
// char **ptr = argv + 1;
// while (*ptr != NULL) {
// printf("%s\n", *ptr);
// ptr++;
// }

// WRONG
// char **ptr = argv + 1;
// while (*ptr++ != NULL) {
// printf("%s\n", *ptr);
// }

char **ptr = argv;
// *++ptr
// *ptr++
// *--ptr
// *ptr--
while (*++ptr != NULL) {
printf("%s\n", *ptr);
}

return 0;
}
142 changes: 142 additions & 0 deletions 9-double-pointers/pointers-malloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Created by hfwei on 2024/11/27.

#include <stdio.h>
#include <stdlib.h>

#define LEN 3
#define ROW 3
#define COL 4

int main(void) {
// (1) One-Dimensional Array
// Visualization: https://tinyurl.com/pointers-malloc-int

// malloc
int *array = malloc(LEN * sizeof *array);
if (array == NULL) {
printf("malloc failed\n");
return EXIT_FAILURE;
}

// fill in
for (int i = 0; i < LEN; ++i) {
array[i] = i * i;
}

// print
for (int i = 0; i < LEN; ++i) {
printf("%d ", array[i]);
}
printf("\n\n");

// free
free(array);

// (2) Two-Dimensional Array
// Visualization: https://tinyurl.com/pointers-malloc-int-array

// malloc
int (*table)[COL] = malloc(ROW * sizeof *table);

// fill in
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
table[i][j] = i * j;
}
}

// print
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
printf("%d ", table[i][j]);
}
printf("\n");
}
printf("\n");

// free
free(table);

// (3) One-Dimensional Array of Pointers
// Visualization: https://tinyurl.com/pointers-malloc-arraypointers

// malloc and fill in
int *array_of_pointers[LEN];
for (int i = 0; i < LEN; ++i) {
array_of_pointers[i] = malloc((i + 1) * sizeof *array_of_pointers[i]);
for (int j = 0; j < i + 1; ++j) {
array_of_pointers[i][j] = i * j;
}
}

// print
for (int i = 0; i < LEN; ++i) {
for (int j = 0; j < i + 1; ++j) {
printf("%d ", array_of_pointers[i][j]);
}
printf("\n");
}
printf("\n");

// free
for (int i = 0; i < LEN; ++i) {
free(array_of_pointers[i]);
}

// (4) Two-Dimensional Array with Potentially Non-Contiguous Memory
// Visualization: https://tinyurl.com/pointers-malloc-int-pp

// malloc
int **matrix = malloc(ROW * sizeof *matrix);
for (int i = 0; i < ROW; ++i) {
matrix[i] = malloc(COL * sizeof *matrix[i]);
}

// fill in
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
matrix[i][j] = i * j;
}
}

// print
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
printf("\n");

// free
for (int i = 0; i < ROW; ++i) {
free(matrix[i]);
}
free(matrix);

// (5) Two-Dimensional Array with Contiguous Memory
// Visualization: https://tinyurl.com/pointers-malloc-int-p

// malloc
int *matrix_contiguous = malloc(ROW * COL * sizeof *matrix_contiguous);

// fill in
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
matrix_contiguous[i * COL + j] = i * j;
}
}

// print
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
printf("%d ", matrix_contiguous[i * COL + j]);
}
printf("\n");
}

// free
free(matrix_contiguous);

return 0;
}
78 changes: 78 additions & 0 deletions 9-double-pointers/scores.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Created by hengxin on 11/27/2024.

// Python Tutor Visualization:
// (1) https://tinyurl.com/scores-of-musicians
// (2) https://tinyurl.com/scores-of-musicians-malloc
// https://tinyurl.com/

#include <stdio.h>
#include <stdlib.h>

#define NUM_OF_MUSICIANS 4
#define NUM_OF_SCORES 3

void Print(const int table[][NUM_OF_SCORES], int num_of_musicians);

int main() {
// C, Java, Python scores of several musicians

// TODO: (1) initialize scores with a 2D array
// const int scores[NUM_OF_MUSICIANS][NUM_OF_SCORES] = {
// { 0, 10, 20 },
// { 10, 20, 30 },
// { 20, 30, 40 },
// { 30, 40, 50 },
// };

// TODO: (2) Dynamically allocate memory for scores
// malloc here
int (*scores)[NUM_OF_SCORES] = malloc(NUM_OF_MUSICIANS * sizeof(*scores));
if (scores == NULL) {
return 0;
}

// fill in data here
for (int i = 0; i < NUM_OF_MUSICIANS; ++i) {
for (int j = 0; j < NUM_OF_SCORES; ++j) {
scores[i][j] = i * j;
}
}

// print it here
Print(scores, NUM_OF_MUSICIANS);

// ptr_scores here
int (*ptr_scores)[NUM_OF_SCORES] = scores;
printf("ptr_scores[3][2] = %d\n",
(*(ptr_scores + 3))[2]);

// do not forget to free it
free(scores);

return 0;
}

// table: int table[][COL]
// int table[]: int *table
// int table[][COL]: int (*table)[COL]
void Print(const int table[][NUM_OF_SCORES], int num_of_musicians) {
for (int i = 0; i < num_of_musicians; i++) {
for (int j = 0; j < NUM_OF_SCORES; j++) {
// table[i][j]: *(*(table + i) + j)
// table: int (*)[COL]
// table + i: int (*)[COL]
// *(table + i): int *
// *(table + i) + j: int *
// *(*(table + i) + j): int
printf("table[%d][%d]: %d\n\n",
i, j, table[i][j]);

printf("table: %p\n", table);
printf("table + %d: %p\n", i, table + i);
printf("*(table + %d): %p\n", i, *(table + i));
printf("*(table + %d) + %d: %p\n", i, j, *(table + i) + j);
printf("*(*(table + %d) + %d): %d\n", i, j, *(*(table + i) + j));
}
printf("\n\n");
}
}
Loading

0 comments on commit 492ec02

Please sign in to comment.