0

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?

2

7 Answers 7

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

It should be:

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

or even better:

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

Comments

1

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

Comments

1

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!

2 Comments

Although this is perfectly valid, I'd consider it code obfuscation naming different things the same.
Agreed. I don't know exactly what did he mean by this struct, but I edited it to have a different name.
1

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!

4 Comments

Casting the malloc is a terrible idea. IMO. (BTW: you'd need an extra asterix there)
Why casting malloc() in C is unnecessary and even more a bad idea: stackoverflow.com/a/605858/694576
Huh, my profs always said casting was better style. Well, you learn something new everyday!
General rule: when you have the choice to cast or not, casting is poor style. Casting means telling the compiler "no, you're wrong, I know better". If you don't have to tell the compiler this, don't; the compiler's job is to help you with its complaints.
0

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

Comments

0

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.