我必须编写一个函数来帮助我使用struct分配一个矩阵。 我今天开始研究结构。 所以我用这个结构和相对main编写了这段代码来证明函数:
#include <stdio.h> #include <stdlib.h> #include <math.h> typedef struct { int rows; int cols; float **row_ptrs; } Mat; Mat* Mat_alloc(int rows, int cols); int main(int argc, char **argv) { Mat *m1 = Mat_alloc(int rows, int cols); return 0; } Mat* Mat_alloc(int rows, int cols) { Mat matrice; matrice.rows = rows; matrice.cols = cols; float** matrice= (float**)malloc((matrice.rows)*sizeof(float*)); for(int i = 0; i < matrice.cols; i++) { matrice[i] = (float*)malloc((matrice.cols)*sizeof(float)); } matrice.row_ptrs = matrice; return matrice; }我知道我犯了一些错误。有人可以帮我解释我是怎么做到的?
I must write a function that should help me to allocate a matrix using a struct. I Began to study the structs today. So I wrote this code with that struct and relative main to prove the function:
#include <stdio.h> #include <stdlib.h> #include <math.h> typedef struct { int rows; int cols; float **row_ptrs; } Mat; Mat* Mat_alloc(int rows, int cols); int main(int argc, char **argv) { Mat *m1 = Mat_alloc(int rows, int cols); return 0; } Mat* Mat_alloc(int rows, int cols) { Mat matrice; matrice.rows = rows; matrice.cols = cols; float** matrice= (float**)malloc((matrice.rows)*sizeof(float*)); for(int i = 0; i < matrice.cols; i++) { matrice[i] = (float*)malloc((matrice.cols)*sizeof(float)); } matrice.row_ptrs = matrice; return matrice; }I know that I make some mistakes.Someone can help me to undestand how I can do it ?
最满意答案
chrisd1100给出了一个很好的答案,但这只是有点迂腐这是我的:
#include <stdio.h> #include <stdlib.h> #include <math.h> typedef struct { int rows; int cols; float **row_ptrs; } Mat; Mat* Mat_alloc(int rows, int cols); int main(void) { int i; int rows = 10; int cols = 10; Mat *m1 = Mat_alloc(rows, cols); for (i=0; i<cols; i++) { free(m1->row_ptrs[i]); } free(m1->row_ptrs); free(m1); return 0; } Mat* Mat_alloc(int rows, int cols) { Mat *m1 = malloc(sizeof(Mat)); m1->rows = rows; m1->cols = cols; m1->row_ptrs = malloc((m1->rows)*sizeof(float*)); for(int i = 0; i < m1->rows; i++) { m1->row_ptrs[i] = malloc((m1->cols)*sizeof(float)); } return m1; }chrisd1100 gave a good answer, but only to be a little bit pedantic this is mine:
#include <stdio.h> #include <stdlib.h> #include <math.h> typedef struct { int rows; int cols; float **row_ptrs; } Mat; Mat* Mat_alloc(int rows, int cols); int main(void) { int i; int rows = 10; int cols = 10; Mat *m1 = Mat_alloc(rows, cols); for (i=0; i<cols; i++) { free(m1->row_ptrs[i]); } free(m1->row_ptrs); free(m1); return 0; } Mat* Mat_alloc(int rows, int cols) { Mat *m1 = malloc(sizeof(Mat)); m1->rows = rows; m1->cols = cols; m1->row_ptrs = malloc((m1->rows)*sizeof(float*)); for(int i = 0; i < m1->rows; i++) { m1->row_ptrs[i] = malloc((m1->cols)*sizeof(float)); } return m1; }如何分配带结构的矩阵?(How can I allocate a matrix with structs? [closed])我必须编写一个函数来帮助我使用struct分配一个矩阵。 我今天开始研究结构。 所以我用这个结构和相对main编写了这段代码来证明函数:
#include <stdio.h> #include <stdlib.h> #include <math.h> typedef struct { int rows; int cols; float **row_ptrs; } Mat; Mat* Mat_alloc(int rows, int cols); int main(int argc, char **argv) { Mat *m1 = Mat_alloc(int rows, int cols); return 0; } Mat* Mat_alloc(int rows, int cols) { Mat matrice; matrice.rows = rows; matrice.cols = cols; float** matrice= (float**)malloc((matrice.rows)*sizeof(float*)); for(int i = 0; i < matrice.cols; i++) { matrice[i] = (float*)malloc((matrice.cols)*sizeof(float)); } matrice.row_ptrs = matrice; return matrice; }我知道我犯了一些错误。有人可以帮我解释我是怎么做到的?
I must write a function that should help me to allocate a matrix using a struct. I Began to study the structs today. So I wrote this code with that struct and relative main to prove the function:
#include <stdio.h> #include <stdlib.h> #include <math.h> typedef struct { int rows; int cols; float **row_ptrs; } Mat; Mat* Mat_alloc(int rows, int cols); int main(int argc, char **argv) { Mat *m1 = Mat_alloc(int rows, int cols); return 0; } Mat* Mat_alloc(int rows, int cols) { Mat matrice; matrice.rows = rows; matrice.cols = cols; float** matrice= (float**)malloc((matrice.rows)*sizeof(float*)); for(int i = 0; i < matrice.cols; i++) { matrice[i] = (float*)malloc((matrice.cols)*sizeof(float)); } matrice.row_ptrs = matrice; return matrice; }I know that I make some mistakes.Someone can help me to undestand how I can do it ?
最满意答案
chrisd1100给出了一个很好的答案,但这只是有点迂腐这是我的:
#include <stdio.h> #include <stdlib.h> #include <math.h> typedef struct { int rows; int cols; float **row_ptrs; } Mat; Mat* Mat_alloc(int rows, int cols); int main(void) { int i; int rows = 10; int cols = 10; Mat *m1 = Mat_alloc(rows, cols); for (i=0; i<cols; i++) { free(m1->row_ptrs[i]); } free(m1->row_ptrs); free(m1); return 0; } Mat* Mat_alloc(int rows, int cols) { Mat *m1 = malloc(sizeof(Mat)); m1->rows = rows; m1->cols = cols; m1->row_ptrs = malloc((m1->rows)*sizeof(float*)); for(int i = 0; i < m1->rows; i++) { m1->row_ptrs[i] = malloc((m1->cols)*sizeof(float)); } return m1; }chrisd1100 gave a good answer, but only to be a little bit pedantic this is mine:
#include <stdio.h> #include <stdlib.h> #include <math.h> typedef struct { int rows; int cols; float **row_ptrs; } Mat; Mat* Mat_alloc(int rows, int cols); int main(void) { int i; int rows = 10; int cols = 10; Mat *m1 = Mat_alloc(rows, cols); for (i=0; i<cols; i++) { free(m1->row_ptrs[i]); } free(m1->row_ptrs); free(m1); return 0; } Mat* Mat_alloc(int rows, int cols) { Mat *m1 = malloc(sizeof(Mat)); m1->rows = rows; m1->cols = cols; m1->row_ptrs = malloc((m1->rows)*sizeof(float*)); for(int i = 0; i < m1->rows; i++) { m1->row_ptrs[i] = malloc((m1->cols)*sizeof(float)); } return m1; }
发布评论