diff options
author | Jack Sangdahl <[email protected]> | 2025-08-27 12:45:33 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-08-27 12:45:33 -0600 |
commit | 4552bcf5a4a356415e248dee3b201f0cea7205bb (patch) | |
tree | 9868465918c6a6841b84cd14d66923b993dd9d62 /leetcode.c | |
parent | f549305fb5ca531d29dce6c2c091560f44190443 (diff) | |
download | leetcode-4552bcf5a4a356415e248dee3b201f0cea7205bb.tar.gz leetcode-4552bcf5a4a356415e248dee3b201f0cea7205bb.zip |
chore: Refactor two_d_arr_init function
Return a pointer to a new struct, rather than initialise objects in an
existing struct
Diffstat (limited to 'leetcode.c')
-rw-r--r-- | leetcode.c | 9 |
1 files changed, 5 insertions, 4 deletions
@@ -1,16 +1,17 @@ #include "leetcode.h" -void two_d_arr_init(struct two_d_arr *obj, int row_size, int col_size, - int arr[row_size][col_size]) { +struct two_d_arr *two_d_arr_init(int row_size, int col_size, + int arr[][col_size]) { + struct two_d_arr *obj = (struct two_d_arr *)malloc(sizeof(struct two_d_arr)); obj->row_size = row_size; obj->col_size = (int *)malloc(row_size * sizeof(int)); obj->arr = (int **)malloc(row_size * sizeof(int *)); for (int i = 0; i < row_size; i++) { obj->col_size[i] = col_size; obj->arr[i] = (int *)malloc(col_size * sizeof(int)); - for (int j = 0; j < col_size; j++) - obj->arr[i][j] = arr[i][j]; + memcpy(obj->arr[i], arr[i], col_size * sizeof(int)); } + return obj; } void two_d_arr_free(struct two_d_arr *obj) { |