pointer to struct in struct in c - 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;

Related

Error: request for member 'time_c' in something not a structure or union in C language

I have a problem with some of my functions. I want to get a member ob a struct, out of another struct. The normal call is functionel, but embedded in a function, it doesn't work. Is there any idea why and how I can solve that?
The following function is an example. It should get the time out of a struct, embedded in another struct via the time-converting function "FUN_1"
FUN_1((time_t *)&ptr_to_s_20b_parse_entries->s_28b_meta->time_c);
My structs are:
struct s_28b_meta {
int version;
__time32_t time_c;
__time32_t time_m;
uint32_t i_next;
int hash_value;
int len_database_name;
int *ptr_database_name;
};
struct s_20b_parse_entries {
int *s_28b_meta;
int *s_8b_keys;
int **a_db_entries;
size_t n_db_entries;
int i_next;
};
The decleration:
struct s_28b_meta *ptr_temp;
struct s_20b_parse_entries *ptr_to_s_20b_parse_entries;
In struct s_20b_parse_entries, you have the s_28b_meta field defined as int *. This is not a pointer to struct so you can't use the -> operator on it, which is why you're getting the error.
It should be defined as a struct s_28b_meta *.

How to make cross pointers in structs

So here's my problem, I want to create a graph-database. My concept idea is a node (which will be of type "Vrtc" in the code. It will cointain an (int) id, a (char*) string (called "str") and a list (l_est) of paths in and out.
The list will contain an "array" of pointers to nodes, the amount of nodes in the list, and the maximum number of nodes.
typedef struct Vrtc
{
int id;
char *str;
l_est *pathsIn;
l_est *pathsOut;
} Vrtc;
typedef struct l_est{
Vrtc **Vert;
int qtdElem;
int maxElem;
} l_est;
As you may know, there'll be a problem of "Unknow type/Type undeclared inside the node. I was hoping anyone could give a tip of how to do it, if it is possible.
You have to use a forward declaration:
typedef struct l_est l_est;
typedef struct {
int id;
char *str;
l_est *pathsIn, *pathsOut;
} Vrtc;
typedef struct l_est {
Vrtc **Vert;
int qtdElem, maxElem;
} l_est;
The first type definition of struct l_est is a so-called "incomplete type". This type may not be used for variables or with sizeof but it may be used to declare pointer.
The incomplete type may later be overridden by a "complete type" without complaints of the compiler.
Similar solutions can be found in other languages (e.g. Pascal).

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

Why does "struct T* next" compile when T isn't an existing type?

I am using MinGW on Windows. I am building linked list and I am confused with this.
#include <stdio.h>
#include <stdlib.h>
typedef struct Data
{
int x;
int y;
struct BlaBla * next; /*compiles with no problem*/
}List;
int main(void)
{
List item;
List * head;
head = NULL;
return 0;
}
I now that struct can't have struct variable(object, instance of that struct), but can have pointer of that struct type. Didn't know that pointer can be pointer of unexisting type. struct BlaBla * next;(not for linked list, it must be struct Data * next but mean general talking)
Yes, you can, because then the compiler, upon encountering the unknown type name for the first time, assumes that there's somehwere a struct type definition with this name. Then it will forward-declare the struct name for you, let you use it as a pointer, but you can't dereference it nor can you do pointer arithmetic on it (since it's an incomplete type).
The compiler will accept code such as your example:
typedef struct Data
{
int x;
int y;
struct BlaBla * next; /*compiles with no problem*/
}List;
This is okay because the size of pointers is known to the compiler, and the compiler is assuming that the struct will be defined before it is dereferenced.
Because the compiler acts this way, it's possible to do this:
typedef struct Data
{
int x;
int y;
struct Data * next; /* points to itself */
} List;
However, if you were to include the struct inline, like this:
typedef struct Data
{
int x;
int y;
struct BlaBla blaStruct; /* Not a pointer. Won't compile. */
}List;
The compiler can't work out how big struct Data is because it doesn't know how big struct BlaBla is. To get this to compile, you need to include the definition of struct BlaBla.
Note that, as soon as you need to access the members of struct BlaBla, you will need to include the header file that defines it.
It depends on what you mean by "unexisting". If you haven't even declared BlaBla, you'll get an error.
If you've declared it but not yet defined it, that will work fine. You're allowed to have pointers to incomplete types.
In fact, that's the normal way of doing opaque pointers in C.
So, you might think that this is invalid because there's no declaration of struct BlaBla in scope:
typedef struct Data {
struct BlaBla *next; // What the ??
} List;
However, it's actually okay since it's both declaring struct BlaBla and defining next at the same time.
Of course, since definition implies declaration, this is also okay:
struct BlaBla { int xyzzy; };
typedef struct Data {
struct BlaBla *next; // What the ??
} List;
In order to declare a variable or field of a given type, pass one as a parameter, or copy one to another of the same type, the compiler has to know how many bytes the variable or field occupies, what alignment requirements it has (if any), and what other pointer types it's compatible with, but that's all the compiler needs to know about it. In all common dialects of C, a pointer to any structure will always be the same size and require the same alignment, regardless of the size of the structure to which it points or what that structure may contain, and pointers to any structure type are only compatible with other pointers to the same structure type.
Consequently, code which doesn't need to do anything with pointers to a structure except allocate space to hold the pointers themselves [as opposed to the structures at which they point], pass them as parameters, or copy them to other pointers, doesn't need to know anything about the structure type to which they point beyond its unique name. Code which needs to allocate space for a structure (as opposed to a pointer to one) or access any of its members must know more about its type, but code which doesn't do those things doesn't need such information.

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;

Resources