0
votes

I'm trying to create an array of struct elements, as shown below:

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

struct termstr{
double coeff;
double exp;
};

int main(){

termstr* lptr = malloc(sizeof(termstr)*5);

return 0;
}

When i compile this, i get errors as follows:

term.c: In function ‘main’:
term.c:11:1: error: unknown type name ‘termstr’
term.c:11:31: error: ‘termstr’ undeclared (first use in this function)

However, when i change my code to the following, it compiles as usual:

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

typedef struct termstr{
double coeff;
double exp;
}term;

int main(){

term* lptr = malloc(sizeof(term)*5);

return 0;
}

I've added typedef (with type name as term), changed the name of struct to termstr and am allocating memory with term* as the type of pointer.

Is typedef always required for such a situation i.e. for creating arrays of structs? If not, why was the first code giving errors? Is typedef also required to create and use a single instance of a struct?

7
No, you dont need typedef. You could also do without, but you'll need to add struct keyword: struct termstr *lptr = malloc(5 * sizeof *lptr); - wildplasser

7 Answers

2
votes

First type is not working because you have forgot struct keyword before termstr. Your data type is struct termstr but not just termstr. When you typedef, the resulting name is used as an alias for struct termstr.

Even you don't need to do that. Using typedef is better:

By the way don't forget to free the memory:

read why to use typedef?

Your working code should be:

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

struct termstr{
  double coeff;
  double exp;
};

int main(){

struct termstr* lptr = malloc(sizeof(struct termstr)*5);
free(lptr);
return 0;
}
1
votes

It should be:

struct termstr * lptr = malloc(sizeof(struct termstr)*5);

or even better:

struct termstr * lptr = malloc(sizeof(*lptr)*5);
1
votes

In C, the name of the data type is "struct termstr", not just "termstr".

1
votes

You can do something like this:

typedef struct termstr{
   double coeff;
   double exp;
} termstrStruct;

And then you can use only termstrStruct as the struct's name:

termstrStruct* lptr = malloc(sizeof(termstrStruct)*5);

It is not always required, you can simply write struct termstr.

Don't forget to free the allocated memory!

1
votes

Typedef is a convenient way of shortening this:

struct termstr* lptr = (struct termstr*)malloc(sizeof(struct termstr)*5);

to this:

typedef struct termstr* term;
term* lptr = (term*)malloc(sizeof(term)*5);

Casting the malloc is also a good idea!

0
votes

If you want to use the typename termstr on it's own you can use typedef: typedef struct { double a; double b; } termstr;

0
votes

In C you need to add the struct keyword as well, so either you use a typedef to link an alias with 'struct termstr' or you need to write something like

struct termstr* lptr = malloc(sizeof(struct termstr)*5);

In C++ however you can reference it as 'termstr' directly (read: the struct keyword isn't required there anymore).