typedef struct clarification - c

Can anyone explain me what is the difference between this:
typedef struct{
char a[10];
int b;
char c[8];
...
}test;
and this:
typedef struct test{
char a[10];
int b;
char c[8];
...
}test;
Thanks

typedef struct{
char a[10];
int b;
char c[8];
...
}test;
The above defines an anonymous struct and immediately typedefs it to the type alias test.
typedef struct test{
char a[10];
int b;
char c[8];
...
}test;
This however, creates a struct named struct test as well as adding a typedef for it.
In the first case, you will not be able to forward declare the struct if you need to.
There's also a philosophy (which I happen to agree with to a point), that typedefing all structures by default makes code less readable, and should be avoided.

Having "test" in two different places is a bit confusing. I usually write code like this:
typedef struct test_s {
...
} test;
Now I can either use type struct test_s or just test. While test alone is usually enough (and you don't need test_s in this case), you can't forward-declare pointer to it:
// test *pointer; // this won't work
struct test_s *pointer; // works fine
typedef struct test_s {
...
} test;

With the first version you can only declare:
test t;
With the second versijon you can choose between:
struct test t;
test t;

Short answer: They're the same (in your code)
Long answer: Why put test between typedef struct and {? Is that pointless?
This (struct name test) is pointless in your code
In this code however, it's not:
struct Node {
int data;
struct Node * next;
} head;

Related

Array type has incomplete element type 'struct instruction'

How do I make this struct instruction visible to struct cpu state? Like If I put cpu state struct first it doesn't work because some other arguments won't be visible to struct instruction but if I put it reverse again I have a problem.
struct cpu_state
{
int nextinstructionlinenumber;
char filename[200];
char RLO;
struct instruction instructionlist[INSTRUCTIONLIST_SIZE];
int instructionlistnumber;
struct variable variablelist[INSTRUCTIONLIST_SIZE];
int variablelistnumber;
};
struct cpu_state CPU;
struct instruction{
char name[LINE_CHAR_NUM];
void(*function)(struct cpu_state* pstate, struct line* pline);
};
You can create incomplete structure declarations provided they are just used for pointers. For example, the following order will work. Note that I created a dummy definition for struct variable since it was absent from the post. You can replace it with whatever you like:
struct variable {
int dummy_val;
};
struct cpu_state;
struct line;
struct instruction{
char name[LINE_CHAR_NUM];
void(*function)(struct cpu_state* pstate, struct line* pline);
};
struct cpu_state
{
int nextinstructionlinenumber;
char filename[200];
char RLO;
struct instruction instructionlist[INSTRUCTIONLIST_SIZE];
int instructionlistnumber;
struct variable variablelist[INSTRUCTIONLIST_SIZE];
int variablelistnumber;
};
struct cpu_state CPU;

Giving different names to a struct with same body

I have a code duplication situation where I have exact same body of struct but with different names. The body of struct is not small, hence there is risk of injecting error while modifying code in future. Following is just example to illustrate the problem:
struct read_data_on_disk {
int a;
char b;
};
struct read_data {
int a;
char b;
};
It is possible to define one of them, say read_data_on_disk and another be just defined as alias of it? I am looking for something like below:
typedef struct read_data_on_disk struct read_data; // this is wrong though
That's almost right. Try this:
struct read_data_on_disk {
int a;
char b;
};
typedef struct read_data_on_disk read_data;
But as dbush pointed out above, why have two structs if their content is identical?

Initialize typedef struct

Why does this work:
struct person {
char name[50];
short mental_age;
} p1 = {"Donald", 4};
But not this:
typedef struct {
char name[50];
short mental_age;
} PERSON p1 = {"Donald", 4};
Is there a way that I can make a typedef struct and initialize Donald when I define this struct?
typedefs are aliases for other types. What you're doing is creating a convenience typedef. Since the purpose of a typedef is to create type aliases, you can't define a variable using it.
You have to do this:
typedef struct {
// data
} mytype;
mytype mydata = {"Donald", 4};
The best way, that I know of, is to separate the strict definition from the typedef statement from the struct declaration, similar to:
struct sPerson
{
char name[50];
short mental_age;
};
typedef struct sPerson PERSON;
PERSON p1 = {"Donald", 4};

Dynamic array stack struct C

I have a couple of questions. I have a bit of a hard time understanding this code. What exactly is it doing?
For example:
What does typedef struct dynArrStruct do and why does it have dynArr at the end of it? I know the definition of typedef as "allows to created alias for a known data type" but that is jargon to me. Can someone try to put it in layman terms? Thank you!
Why are there 2 struct variables (a1/a2)?
Link to full code if needed:
http://www.cs.uic.edu/pub/CS211/CS211LectureNotesS13/dynArr.c
typedef struct dynArrStruct
{
double *location;
int length;
int currSize;
} dynArr;
int main (int argc, char**argv)
{
struct dynArrStruct a1;
dynArr a2;
int i;
//rest of code
}
What does typedef struct dynArrStruct do and why does it have dynArr at the end of it?
The typedef creates an alias to a type to save you some typing, or to improve readability. In this particular case, it creates an alias called dynArr for the struct dynArrStruct.
Without a typedef, i.e. with only this
struct dynArrStruct
{
double *location;
int length;
int currSize;
};
you would be forced to write struct dynArrStruct every time you need to declare a variable of that struct's type. With a typedef in place, you can write simply dynArr, and the compiler will interpret it as the struct dynArrStruct for you.
typedef struct dynArrStruct
{
double *location;
int length;
int currSize;
} dynArr;
Is a short form of two different pieces of code.
// define a struct by name dynArrStruct
struct dynArrStruct
{
double *location;
int length;
int currSize;
};
//Example of use
struct dynArrStruct d1;
and
// define an alias to "struct dynArrStruct" called dynArr
typedef struct dynArrStruct dynArr;
//Example of use
dynArr d2; //whoa so short!
In addition to dasblinkenlight's answer,
Why are there 2 struct variables (a1/a2)?
The code presented appears to be an example of poorly modularised code (a1) and well modularised code (a2). The modifications made to a1 are very similar to the modifications made to a2. However, the modifications made to a2 are abstracted out into functions (lines 53-55 correspond to the lines found in init, 57-58 correspond to the lines found in push and push), so that that functionality can be easily reused. An example of this reuse is lines 69-72.

C: pointer to struct in the struct definition

How can I have a pointer to the next struct in the definition of this struct:
typedef struct A {
int a;
int b;
A* next;
} A;
this is how I first wrote it but it does not work.
You can define the typedef and forward declare the struct first in one statement, and then define the struct in a subsequent definition.
typedef struct A A;
struct A
{
int a;
int b;
A* next;
};
Edit: As others have mentioned, without the forward declaration the struct name is still valid inside the struct definition (i.e. you can used struct A), but the typedef is not available until after the typedef definition is complete (so using just A wouldn't be valid). This may not matter too much with just one pointer member, but if you have a complex data structure with lots of self-type pointers, may be less wieldy.
In addition to the first answer, without a typedef and forward declaration, this should be fine too.
struct A
{
int a;
int b;
struct A *next;
};
You are missing the struct before the A*
typedef struct A {
int a;
int b;
struct A* next;
} A;
You can go without forward declaration:
struct A {
int a;
int b;
struct A *next;
};
Please, you're in C, not C++.
If you really must typedef a struct (and most programmers that I work with would not¹), do this:
typedef struct _A {
int a;
int b;
struct _A *next;
} A;
to clearly differentiate between _A (in the struct namespace) and A (in the type namespace).
¹typedef hides the size and storage of the type it points to ― the argument (and I agree) is that in a low-level language like C, trying to hide anything is harmful and counterproductive. Get used to typing struct A whenever you mean struct A.
typedef struct {
values
} NAME;
This is shorter way to typedef a struct i think its the easiest notation, just don't put the name infront but behind.
you can then call it like
NAME n;
NAME *n; // if you'd like a ptr to it.
Anything wrong with this approach?

Resources