John Smith
John Smith

Reputation: 87

Typedef with arrays in C

Hello I am new to C programming. After using python for a long time, C seems infinitely harder (and well infinitely more fun)!

What I cannot wrap my head around is using typedef with arrays and in particular 2D Arrays.

typedef double vector_t[10];

As far as I understand this helps us use vector_t to initialise an array of doubles with 10 elements. So would initialising vector_t[10] initialise an Array of [10][10]?

Also what if I initialise vector_t[5]?

typedef vector_t second_t[5];

What will happen if I use second_t? Will the 2d array be array of [10][5] or an array of [5][10]?

Upvotes: 8

Views: 13808

Answers (2)

user8549610
user8549610

Reputation:

As far as I understand this helps us use vector_t to initialise an array of doubles with 10 elements.

That's not quite correct. To be precise, this typedef defines a custom type which enables one to declare an array, say, vector_t example[N]; which bona fide dimension will be N + 1 (since vector_t already assumes that a single element is itself a 1-dimensional array). Saying initialise means that you fill that memory with some data. In this particular case you may say memset(my_vec, 0, sizeof(my_vec)); to zeroise the array. If you have a simpler variable, say, int a;, then you may initialise it with, say, a = 1;.

If you declare vector_t another_example[10][10], this will actually give you a 3-dimensional array - 10 x 10 x 10.

what will happen if I use second_t? will the 2d array be Array[10][5] or Array[5][10]??

So, as you might understand from the beginning of my post, in the latter case, neither declaring second_t Array[10][5] nor second_t Array[5][10] will give a 2D array. Actually, this will be 4-dimensional array since second_t defines a single element as being already a 2D array.

For educational purposes I suggest that you simply start with something like

#include <stdio.h>
#include <stdint.h>

typedef uint8_t vector_t[10];

in order to be able to construct more complex types, then declare arrays (say, the variable will be called array) and, finally, do something like printf("The size is %lu bytes\n", sizeof(array)); in order to see the total size in bytes. Then it will be easy to see what the total size is taking into account that the very base type is uint8_t (1 byte).

Upvotes: 0

HTNW
HTNW

Reputation: 29193

If you use

second_t v;

That's exactly the same as

vector_t v[5];

And that's exactly the same as

double v[5][10]; /* NOT double[10][5] */

When you expand a typedef, imagine that you're substituting whatever's after the typedef for the typedef's name in its definition:

typedef something t[size];
t x;
/* subst [t := x] into the typedef definition */
something x[size];

second_t v;
/* [second_t := v] in definition */
vector_t v[5];
/* [vector_t := v[5]] in definition */
double v[5][10];

typedef int (*unary_op)(int); /* pointers to functions int => int */
typedef int (*consumer)(unary_op); /* pointers to functions (int => int) => int */
consumer f;
/* [consumer := f] in definition */
int (*f)(unary_op);
/* [unary_op := ] (replace unary_op with "", because there's no name) in definition.
   We must respect parentheses */
int (*f)(int (*)(int));
// Something like int func(unary_op op) { return op(5); }

Upvotes: 9

Related Questions