0

As usual, Wikipedia's article on structs is less than clear. It gives the syntax for structs as this:

[typedef] struct [struct_name]
{
    type attribute;
    type attribute2;
    /* ... */
    [struct struct_name *struct_instance;]
} [struct_name_t] [struct_instance];
  • What would the typedef keyword do here?
  • What does the [struct_name] mean? (Is it the name you're giving to the new struct data type?)
  • What does the [struct_name_t] mean?
  • what does the [struct_instance] mean? (Is it creating a single instance of the struct?)
  • I presume [struct struct_name *struct_instance;] creates a pointer in the struct which would point to a second instance of the struct). Correct?

I would greatly appreciate an example: Say I have three files: main.c, sub.c and sub.h. I want to declare an instance of a struct in sub.h, and instantiate and use it it in sub.c. Say I want a Song type struct, with members char name[20] and char artist[10], and say I want to make an instance, mySong, {"Me singing", "Me"}, how would this look in sub.c and sub.h?

Thanks

Jodes
  • 14,118
  • 26
  • 97
  • 156

4 Answers4

3

•What would the typedef keyword do here?

It would allow you to create a typedef of your structre, just like any other type. This allow you to not have to type struct xxx struct_name everytime. You don't need this, hence the []

•What does the [struct_name] mean? (Is it the name you're giving to the new struct data type?)

Yes, if you chose too. You can also make a nameless struct so you don't need to give it a name.

•What does the [struct_name_t] mean?

That's the typedef'd name, if you chose to typedef the struct

•what does the [struct_instance] mean? (Is it creating a single instance of the struct?)

Yes, it's for creating one or more instance(s) of the sturct

•I presume [struct struct_name *struct_instance;] creates a pointer in the struct which would point to a second instance of the struct). Correct?

Right, this would be usefull for a "next" type pointer in a linked list.

struct example:

typedef struct foo{
    int count;
    struct foo *next;
} foo_t myfoo;

is an example of that filled in; this allows you to declare a new struct via:

 foo_t new_foo_struct;

because of the typedef and typedef'd name. If you omit those like this:

struct foo{
    int count;
    struct foo *next;
} myfoo;

Now you'd have to use the struct key word for every instance, such as:

 struct foo new_foo_struct;

to break it up over more than 1 file:

/*sub.h*/
typedef struct{
char name[20];
char artist[10];
}song;

Then in the source:

/*sub.c*/
#include "sub.h"

/*this needs to go into a function or something...*/
song mysong;
strcpy(mysong.name, "Mesinging");
strcpy(mysong.artist, "Me");
Mike
  • 47,263
  • 29
  • 113
  • 177
  • Excellent, thank you. My question remaining however, is how I would declare it in a .h file, and then specify it in a .c file? Both the type and the instance – Jodes May 13 '13 at 12:27
2

That article is just wrongly mixing different concepts, rectified this now. A struct is declared through

struct tagname {
  ... fields ...
};

that's just it, only that the tagname part is optional in some contexts.

In addition you may

  • declare an alias for the struct type through typedef
  • or a variable of the struct type

"in one go", but I don't think that it is good style and should be separated.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
1
sub.h
------
typedef struct{
char name[20];
char artist[10];
}song;


sub.c
----
song mysong={"Me Singing","Me"};
Dayal rai
  • 6,548
  • 22
  • 29
0
typedef struct struct_name
{
   char name[20];
   char artist[10];
}struct_name_t structInstance; 

typedef - this means that you are creating a new type (struct_name_t)

So, in C code you can create an instance like this:

struct_name_t myVariable;

or you can explicitly write:

struct struct_name myVariable;

The structInstance at the end means that you want to create an instance of your struct at the same moment you defined it (and the name of that variable is structInstance). It's not something you will use all the time, but it is useful in some situations.

If you want to create an instance of your struct and assign/initialize members at the creation time, you can do it like this:

struct_name_t myVariable = { "Foo", "bar" };

The 'name' member will contain "Foo" and the artist member will contain "bar".

Note: If you write this:

struct_name_t myVariable = { 0 };

That will fill your entire struct with zeroes!

user1764961
  • 673
  • 7
  • 21