简体   繁体   中英

typedef required in struct declaration

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 ie 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?

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;
}

It should be:

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

or even better:

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

在C语言中,数据类型的名称是“ struct termstr”,而不仅仅是“ termstr”。

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!

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!

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM