0

So I'm working on code for school but I need some help understanding this struct and typedef that is being created

struct matrix_2D_struct {
    int** data_arr;
    int rows;
    int cols;
};
typedef struct matrix_2D_struct * matrix_ptr;

Is this defining a struct of type matrix_2D_struct which contains a pointer to a pointer of type int called data_arr, an int called rows, and an int called cols?

also I don't understand the last line. I understand it's defining something and giving it an tag of matrix_ptr, but is it defining the alias of the struct as a pointer? I'm not sure exactly how to interpret that.

also, can I define the data_arr as a 2 dimensional array using calloc?

thank you for your time.

0

3 Answers 3

1

some times you see some definitions like this:

unsigned long long var;

if your program has a lot of unsigned long long it becomes too tedious. to go over it you shorten it by defining a new type based on that using typedef, as this for example:

 typedef unsigned long long ULL;

now you just write ULL var;

same thing with struct, instead of writing each time struct dummy var you define a new type based on struct dummy using typedef like this:

typedef struct dummy DUMMY;

DUMMY myVar; // the type of myVar is a struct dummy

same way with pointer, you can define a new type that is of type pointer , eg:

typedef struct dummy* P_DUMMY;

P_DUMMY myVarPtr; // the type of myVarPtr is a pointer to struct dummy;
1

Is this defining a struct of type matrix_2D_struct...

I would say a type named matrix_2D_struct that is a struct, rather than a struct that is a type, but otherwise yes.

which contains a pointer to a pointer of type int called data_arr, an int called rows, and an int called cols?

Yes.

also I don't understand the last line. I understand it's defining something and giving it an tag of matrix_ptr, but is it defining the alias of the struct as a pointer?

In C you usually pass data around as pointers. There are important reasons for this -- one, for instance, is so that you can pass structured data to a function, and the function can change the data's internals (rows, cols, even data_arr). Once you define a type for a struct, you usually define a type for a pointer to the struct. That's what matrix_ptr is. You will probably pass this around, instead of matrix_2D_struct.

also, can I define the data_arr as a 2 dimensional array using calloc?

The question's a bit vague, but I don't see why not. I also don't see why you can't allocate the memory using malloc().

1

matrix_ptr is an alias of struct matrix_2D_struct *.

2
  • and struct matrix_2D_struct * is a pointer to a struct of type matrix_2D_struct? Nov 1, 2015 at 6:08
  • @JustaRedShirt struct matrix_2D_struct * is a struct matrix_2D_struct pointer Nov 1, 2015 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.