Making a Linked List - c

typedef struct{
char name[25];
int yearOfBirth;
int district;
char gender;
int age;
CitizenType *next;
}CitizenType;
When I try to make a Linked List in this format in Visual Studio, I get all sorts of errors relating to syntax. (102 in total) but when I comment out the CitizenType *next; I get no errors. I realize it has something to do with referencing the structure before it has been completely declared, but I have no idea how to fix this.

Declare the typedef before (and separately from) the structure.
typedef struct citizen_type CitizenType;
struct citizen_type {
...
CitizenType *next;
};

The problem is that
CitizenType
enters into the namespace of types only after the structure ends.
So you can use the new type only after its declaration.
You can use instead of it a struct name (giving a name to the struct), or declaring the type before to declare the structure with a name, as in previous post.

Try this:
typedef struct node{
char name[25];
int yearOfBirth;
int district;
char gender;
int age;
struct node *next;
}CitizenType;
Check this stack overflow answer for more information about self referenced structs in C. From the answer:
A CitizenType cannot contain another CitizenType as it becomes
a never-ending recursion.
Hope it helps!

Related

Why does this typedef code not create an instance

My understanding of the following structure code is that the last "s1" creates an instance of our new data type;
struct student
{
int age;
char *name;
}s1;
The addition of s1 is as if I had typed the following (after creating the structure type)
struct student s1;
However, if I use typedef, the code does not create an instance; instead it just makes s1 a synonym for struct student. Is this interpretation correct? I ask only because I find it odd that it doesn't work the same as the first block of code
typedef struct student
{
int age;
char *name;
}s1;
This doesn't create an instance of struct student called s1; it makes s1 the equivalent of typing struct student. What I find odd about this is that I thought typedef itself was enough to avoid typing struct student all the time. That is, if I had excluded s1 from the code block above, I could simply type
student s1;
instead of
struct student s1;
thanks to the addition of typedef.
If that is the case, then isn't this redundant:
typedef struct student
{
int age;
char *name;
} student;
I am already able to type simply student have C "substitute" in struct student simply by the first line of that last code block??
That's the way typedef works. By prepending it to what would otherwise be a variable definition, you create a declaration of a type alias. It's easier to see with a fundamental type:
int a; // a is a variable of type int
typedef int b; // b is a synonym for the type int
"What I find odd about this is that I thought typedef itself was enough to avoid typing struct student all the time. That is, if I had excluded s1 from the code block above, I could simply type student s1;"
No, that wouldn't work. Try it.
A typedef without a trailing name is syntactically correct, but meaningless.
int; // compiles, does nothing
typedef int; // compiles, does nothing

Pointer in typedef struct confusion

I am trying to define a typedef struct as follows:
typedef struct node{
int keys[2*MIN_DEGREE-1];
struct node* child[2*MIN_DEGREE];
int numkeys;
int isleaf;
} BNODE,*BNODEPTR;
Instead of using struct node* child[2*MIN_DEGREE] why can't I declare the struct as follows:
typedef struct node{
int keys[2*MIN_DEGREE-1];
BNODEPTR child[2*MIN_DEGREE];
int numkeys;
int isleaf;
} BNODE,*BNODEPTR;
I am little confused as to how the compiler resolves structs that has nested pointers to the same type. It will be great somebody helps me clear this up.
Thanks
You can't use BNODEPTR in the body of the structure like that because it either doesn't exist as a type at all until after the definition after the close brace of the structure body, or (worse) it refers to a different type altogether*.
You could use:
typedef struct node BNODE, *BNODEPTR;
struct node
{
int keys[2*MIN_DEGREE-1];
BNODEPTR child[2*MIN_DEGREE];
int numkeys;
int isleaf;
};
And there's another whole argument that says BNODEPTR is evil and you should only use BNODE and BNODE *, but that's a style issue, not a technical one.
Were it my code, it would probably be more like:
typedef struct Node Node;
struct Node
{
int keys[2*MIN_DEGREE-1];
Node *child[2*MIN_DEGREE];
int numkeys;
int isleaf;
};
In C++, the rules are slightly different and you would not need the typedef line (so Node would be known as a type from the open brace).
* This can only happen if the original BNODEPTR is defined at an outer scope and this one appears inside a function, but when it happens, it is really confusing!
Instead of using struct node* child[2*MIN_DEGREE] why can't I declare
the struct as follows: BNODEPTR child[2*MIN_DEGREE];?
At that point, compiler (yet) does not know what the symbol BNODEPTR is.

pointer to struct in struct in c

I have trouble with structures in c.
I have two structures like
typedef struct
{
char isim[256];
int deger;
struct ekstra *sonra;
}ekstra;
typedef struct
{
char *name;
int val;
struct ekstra *next;
}node;
/*and main is*/
int main()
{
int i;
node dizi[12];
for(i=0;i<12;i++)
{
dizi[i].name = malloc("asdasd"*sizeof(int));
strcpy (dizi[i].name,"asdasd");
/*and trouble starts here*/
**dizi[i].next = malloc(sizeof(ekstra));
printf("%s",dizi[i].next->isim);**
}
}
the error is
error: dereferencing pointer to incomplete type
How can I hold place for dizi[i].next?
struct ekstra is not the same as ekstra.
Your first struct typedef should be declared as follows:
typedef struct ekstra
{
char isim[256];
int deger;
struct ekstra *sonra;
}ekstra;
typedef struct... ekstra;
This means: "create a type that is called ekstra". From now on this type can be used just as any variable type (int, char etc).
struct ekstra *next;
This means: Somewhere in my program there is a struct of some type, I don't know what it contains, but I want to point to an element of that type. This is the meaning of incomplete type.
To fix your problems, simply replace this row with ekstra *next;.
More comments not directly related to the question:
dizi[i].name = malloc("asdasd"*sizeof(int));
This is pure nonsense code. It means: "Create a constant string literal in the ROM part of my program. In this constant string literal, store the letters "asdasd" and a null termination character. Then take the address of this ROM memory location, which is completely irrelevant to my application, convert it to an integer so that I get a 32-bit nonsense number. Then multiply this nonsense number with the sizeof an int, which doesn't make any sense to anyone either. Then take this completely nonsense result and allocate a random amount of dynamic memory based on this. Then watch the program crash.
I don't understand code line like
malloc("asdasd"*sizeof(int));
But, I think you problem should slove like this
dizi[i].next = (ekstra *)malloc(sizeof(ekstra));
and you struct define should like
typedef struct node{
int a;
int b;
struct node *next;
}node;

What's the benefit of a typedef in C?

typedef struct _VIDEO_STREAM_CONFIG_CAPS
{
GUID guid;
ULONG VideoStandard;
SIZE InputSize;
SIZE MinCroppingSize;
SIZE MaxCroppingSize;
int CropGranularityX;
int CropGranularityY;
int CropAlignX;
int CropAlignY;
SIZE MinOutputSize;
SIZE MaxOutputSize;
int OutputGranularityX;
int OutputGranularityY;
int StretchTapsX;
int StretchTapsY;
int ShrinkTapsX;
int ShrinkTapsY;
LONGLONG MinFrameInterval;
LONGLONG MaxFrameInterval;
LONG MinBitsPerSecond;
LONG MaxBitsPerSecond;
} VIDEO_STREAM_CONFIG_CAPS;
Why not define structure VIDEO_STREAM_CONFIG_CAPS directly instead of involving _VIDEO_STREAM_CONFIG_CAPS?
Quite simply (at least for me) because some people like to be able to treat user defined types as "primary" types.
Just like I wouldn't like to have to say:
struct int i;
I prefer:
VIDEO_STREAM_CONFIG_CAPS vscc;
to:
struct VIDEO_STREAM_CONFIG_CAPS vscc;
In fact, I usually get rid of the structure tag altogether, preferring:
typedef struct {
GUID guid;
ULONG VideoStandard;
:
} VIDEO_STREAM_CONFIG_CAPS;
The only time I genarally use the tag is if I have to refer to the type within the type definition itself, such as in linked lists:
typedef struct sNode {
char paylod[128];
struct sNode *next;
} tNode;
That's because, at the time of creating the definition, tNode doesn't yet exist but struct sNode does (you can think of it as a simple sequencing thing if that makes it easier - struct sNode gets created on line 1 above, tNode on line 4, which means on line 3 where you create the next pointer, you have to use the structure name).
In the case you cite, the structure tag is superfluous at least in the code shown. Whether some other piece of the code declares a variable with the structure name rather than the typedef name is unclear.
In c, you have to put struct in front of a declaration of a struct type. Without this typedef, you'd have to write struct VIDEO_STREAM_CONFIG_CAPS each time you wanted to use it. With the typedef, you can say just VIDEO_STREAM_CONFIG_CAPS as you would in c++.
struct a {};
struct a A;
OR
typedef struct a {} a;
a A;
In that case, each time a variable of type VIDEO_STREAM_CONFIG_CAPS is declared, the following syntax would be required:
struct VIDEO_STREAM_CONFIG_CAPS vscc;
With typedef struct it is:
VIDEO_STREAM_CONFIG_CAPS vscc;

Difference in declaring structures

I was using my structure like this. I don't like to typedef as I have told it can hide errors.
However, I was looking at some sample code and I have seen structures declared like this. And this is the normal way I declare them.
struct person
{
int age;
char name[32];
};
using like this:
struct person person_a;
person_a.age = 20;
etc.
However, I have seen structures declared like this:
struct
{
int age;
char name[32];
}person;
and
struct _person
{
int age;
char name[32];
}person;
What is the difference between all these different techniques, and how would you decide when it is the best to use each particular one.
Many thanks for any suggestions,
This:
struct
{
int age;
char name[32];
}person;
declares a variable person that is a struct. The struct has no name, but you still use the person variable the same way.
struct _person
{
int age;
char name[32];
}person;
This is identical to:
struct _person
{
int age;
char name[32];
};
struct _person person;
It declares a struct named _person and then makes a variable called person of type struct _person.
In both the last two structures, you've allocated storage for the structure called person.
What I mean is if you have something like this:
struct person
{
int age;
char name[32];
};
It's just a declaration; no variable allocation is there and hence cannot be used inside the code. You can start using this structure after you have declared as follows:
struct person p1;
Then, p1 can be utilized p1.age or p1.name, etc.
In terms of real-world code, instead of
struct _person
{
int age;
char name[32];
}person;
we usually see
typedef struct _person
{
int age;
char name[32];
} person_t;
In this case, we can save typing and more importantly the structure behaves like any other built-in type such as int, etc.
For example,
person_t p1;
person_t *p1;
And, so on.
For the first case (of second example), you will use this approach if you are sure that you do need another object for this structure. This looks clean and is fast to code :)
For the second case you have a ready object named 'person' and an option to create an object later on as
struct _person object2
This is to explain why you will use either approach. The difference between them has been explained well above.
There's no magic there really,
struct person
{
int age;
char name[32];
} var;
is the same thing as:
struct person
{
int age;
char name[32];
};
struct person var;
The 1st code just creates the person struct.
The 2nd code creates an instance of the person struct called person_a.
The 3rd code creates an unnamed struct and an instance of it called person.
The 4th code creates a struct named _person and an instance of it named person.
If the purpose of the structure definition is to declare a logical data structure that has meaning outside of the local module, then you would expect to see the first construct in a header file.
In real world C code, structures are often declared for purley pragmatic reasons that are local in scope. For example, formatting data that will be copied into a buffer. In these cases it's very handy and logical to merge the structure type and variable declaration. Also, using an anoymous declaration for the structure type avoids cluttering the namespace with a name that is not needed.
I do have one critisim of these examples: in C, names with a leading underscore are considered to be reserved for compiler vendors by convention. So I don't think the last example is consistent with best practice.

Resources