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?

user720694
  • 2,035
  • 6
  • 35
  • 57
  • possible duplicate of [What's the syntactically proper way to declare a C struct?](http://stackoverflow.com/questions/4698600/whats-the-syntactically-proper-way-to-declare-a-c-struct) – Oliver Charlesworth Jun 15 '13 at 14:57
  • 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 Jun 15 '13 at 14:58

7 Answers7

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;
}
Community
  • 1
  • 1
pinkpanther
  • 4,770
  • 2
  • 38
  • 62
1

It should be:

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

or even better:

struct termstr * lptr = malloc(sizeof(*lptr)*5);
alk
  • 69,737
  • 10
  • 105
  • 255
1

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

Thomas Padron-McCarthy
  • 27,232
  • 8
  • 51
  • 75
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!

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • 3
    Although this is perfectly valid, I'd consider it code obfuscation naming different things the same. – alk Jun 15 '13 at 15:01
  • Agreed. I don't know exactly what did he mean by this struct, but I edited it to have a different name. – Maroun Jun 15 '13 at 15:03
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!

jh314
  • 27,144
  • 16
  • 62
  • 82
  • 2
    Casting the malloc is a terrible idea. IMO. (BTW: you'd need an extra asterix there) – wildplasser Jun 15 '13 at 15:01
  • 2
    Why casting `malloc()` in C is unnecessary and even more a bad idea: http://stackoverflow.com/a/605858/694576 – alk Jun 15 '13 at 15:20
  • Huh, my profs always said casting was better style. Well, you learn something new everyday! – jh314 Jun 15 '13 at 15:21
  • 1
    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. – Alex Celeste Jun 15 '13 at 16:27
0

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

urzeit
  • 2,863
  • 20
  • 36
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).

amo-ej1
  • 3,279
  • 26
  • 35