Recursive struct in C - c

#include <stdio.h>
typedef struct mystruct
{
void (*ExitFnPtr)(mystruct);
int a;
}mystruct;
int main()
{
mystruct M;
printf("Hello, World!\n");
return 0;
}
Hi all, does anyone know how to solve the recursive struct error listed above?

There is nothing recursive with that.
Your problem is just that the definition of mystruct is not known until the end of the struct's definition.
Try
typedef struct mystruct
{
void (*ExitFnPtr)(struct mystruct ms);
int a;
} mystruct;
struct mystruct is the same as mystruct (you just typedef it), but known at that point in time.
You could also do a forward declaration if you don't want to change your original code (although it's not as readable as the above:
typedef struct mystruct mystruct;
typedef struct mystruct
{
void (*ExitFnPtr)(mystruct ms);
int a;
} mystruct;

Related

struct in c with an attribute that is a pointer to the same struct

i need to create a struct with an attribute that is a pointer to the same struct.
i'm trying this solution but not work:
typedef struct
{
int number;
void *other;
}mystruct;
extern mystruct first[];
extern mystruct second[];
mystruct first[] = {{1,NULL},{2,second}};
mystruct second[] = {{3,NULL},{4,first}};
mystruct *wrap;
wrap = (mystruct *)first[1].other;
int main(void){
printf("%d\n",first[0].number);
printf("%d\n",second[0].number);
printf("%d\n",wrap[1].number);
}
can someone help me?
best regards and thankyou
In C, you can name the struct before using it and typdefing it:
typedef struct mystruct_
{
int number;
struct mystruct_ *other;
} mystruct
I'm not entirely sure but are you looking for some sort of linked-lists or precisely speak Self Referential structure
struct list {
int something;
struct list *use_this_to_point_to_similar_type;
};
Here is another good reference what-is-self-referencing-structure-in-c
just a little bit simplification, and moving few instructions here and there, below code is a loosely written example of possibly what you are looking forward to achieve
#include<stdio.h>
struct mystruct
{
int number;
struct mystruct *other;
};
struct mystruct first[] = {{1,NULL},{2,NULL}};
struct mystruct second[] = {{3,NULL},{4,NULL}};
struct mystruct *wrap;
int main(void)
{
first[1].other = second;
second[1].other = first;
wrap = first[1].other;
printf("%d\n",first[0].number);
printf("%d\n",second[0].number);
printf("%d\n",wrap[1].number);
return 0;
}
your first and second don't need to be extern as they are allocated within your program. you can declare and init. var prior to the main. but the rest you must move into the main function:
int main(void){
wrap = (first[1].other);
printf("%d\n",first[0].number);
printf("%d\n",first[1].number);
printf("%d\n",second[0].number);
printf("%d\n",wrap[1].number);
return 0;}

"struct" before already-declared struct

I am still new in C. I know you can just use the already-declared struct as new data type such as int, double, etc. However, I encounter a struct written like this:
struct AdjListNode
{
int dest;
int weight;
struct AdjListNode* next;
};
In this struct, the data type of "next" pointer is struct AdjListNode*. What does struct have to do with the already-declared AdjListNode*? Thanks!
What does struct have to do with the already-declared AdjListNode*?
The answer is that the c syntax requires it.
You do not get a type AdjListNode by writing struct AdjListNode { ... };
AdjListNode is a struct tag and you always have to use struct AdjListNode when declaring variables.
See this simple example (without pointer inside the struct):
#include <stdio.h>
struct sSomeName
{
int x;
};
int main(void) {
struct sSomeName var; // OK, variable of type struct sSomeName
struct sSomeName* pVar; // OK, pointer to variable of type struct sSomeName
// sSomeName var2; // ERROR: unknown type name 'sSomeName'
var.x = 5;
pVar = &var;
printf("%d\n", pVar->x);
return 0;
}
So if you want to add a pointer inside the struct, you must write struct sSomeName just as you have to do inside main, i.e. like:
struct sSomeName
{
int x;
struct sSomeName* p;
};
Using typedef
If you want a type named AdjListNode you must use typedef.
A typedef example could look like:
#include <stdio.h>
typedef struct sSomeName sSomeName;
struct sSomeName
{
int x;
sSomeName* p;
};
int main(void) {
sSomeName var;
sSomeName* pVar;
var.x = 5;
var.p = NULL;
pVar = &var;
printf("%d\n", pVar->x);
printf("%p\n", (void*)pVar->p);
return 0;
}
Here with that declaration pointer to structure is declared. This is basically used for implementing linked list or for other data structures like tree.
It does'nt mean that struct is re-declared. It is similar to declare a struct variable.
The structure is created as follows: typedef struct AdjListNode. Example:
#include <stdio.h>
#include <stdlib.h>
typedef struct AdjListNode
{
int dest;
int weight;
struct AdjListNode* next;
}AdjListNode;
typedef struct Nodo{
char *nombre;
int *edad;
struct Nodo *siguiente;
}Nodo;
int main(int argc, char **argv) {
AdjListNode *nodo=malloc(sizeof(AdjListNode));
nodo->dest=1;
nodo->weight=2;
nodo->next=NULL;
printf("Nodo-->dest: %d", nodo->dest);
free(nodo);
}

How to handle circular dependencies in typedef'd structures

I have two modules, a and b.
a.h:
#ifndef A_H_
#define A_H_
#include "b.h"
typedef struct {
b_t *b;
...
} a_t;
#endif // A_H_
b.h:
#ifndef B_H_
#define B_H_
#include "a.h"
typedef struct {
a_t *a;
...
} b_t;
#endif // B_H_
How do I alter this so that it will compile? (I want to keep two separate compilation units.)
edit: I forgot to make the structure members pointers.
Use forward declarations:
a.h:
struct b_t;
typedef struct a_t {
struct b_t *b;
} a_t;
b.h:
struct a_t;
typedef struct b_t {
struct a_t *a;
} b_t;
[This answer only applies to the original question, where pointers were not used as the struct members].
that's impossible. it's naturally not possible to have such structure.
let's say:
struct a {
struct b b;
int i;
};
struct b {
struct a a;
int i;
};
what do you expect from sizeof(struct a)? this struct will explode, it's impossible to compile.
however, it can be compiled if you have them turn to pointers:
struct a;
struct b {
struct a *ap;
};
struct a {
struct b *bp;
};
this code indeed compiles: http://ideone.com/GKdUD9.
Exactly this isn't possible. You can't have a struct be its own member, not even transitively. This is however possible when you use a pointer. Use a structure declaration:
typedef struct a_struct *a_t;
typdef struct {
a_t a;
} *b_t;
typedef struct a_struct {
b_t b;
} a_t;
[This answer only applies to the original question, where pointers were not used as the struct members].
You can't.
Essentially you have
typedef struct {
b_t b;
} a_t;
typedef struct {
a_t a;
} b_t;
and no forward declaration can help you. You have an impossible structure here: the sizeof would be infinite.

Error in the use of linked-lists in C

The function doesnt work. The rest of the code is okay. It finds the maximum in the lists (its also the last element of the list) and then doesn't quit the iteration, instead of that, the program crashes. I got a hint, that suggests, that I have problems wit the us of "()". Maxhelye means the max_pos
typedef short int shorti;
typedef struct szelveny{
int szsorszam;
int lsorszam;
int het;
shorti talalat;
int tnyeremeny;
}szelveny; //szelveny-->ticket, szsorszam-->ticketnumer, //lsorszam-->lotterynumber,het-->week, tnyeremeny-->prize
typedef struct szelveny_element{
szelveny szelveny;
struct szelveny_element *next;
}szelveny_element,*szelveny_pointer;
typedef struct lottozo{
int lsorszam;
shorti het;
int sszelveny;
int nyeremeny;
} lottozo; //lottozo-->lottery
typedef struct lottozo_element{
lottozo lottozo;
struct lottozo_element *next;
} lottozo_element,*lottozo_pointer;
typedef struct het{
shorti het;
lottozo_pointer lhead;
szelveny_pointer szhead;
} het;
typedef struct het_element{
het het;
struct het_element *next;
}het_element,*het_pointer;
szelveny_pointer szelvenyek=0;
lottozo_pointer lottozok=0;
het_pointer hetek=0;
int maxnyeremenyhelye2(int ahet) //maxprizeposition, ahet-->week got as parameter
{
int max=0,maxhelye=-1;
het_pointer hp;
for(hp=hetek;hp!=0;hp=hp->next)
if(hp->het.het==ahet)
{
lottozo_pointer lp;
for(lp=hp->het.lhead;lp!=0;lp=lp->next)
{
if(lp->lottozo.nyeremeny>=max)
{
max=lp->lottozo.nyeremeny;
maxhelye=lp->lottozo.lsorszam;
}
}
return maxhelye;
}
}
Your function int maxnyeremenyhelye2(int ahet) does not always return a value - didn't your compiler warn you about this? The line
return maxhelye;
should be moved down below the following brace.

error: dereferencing pointer to incomplete type

Looked through many other SO posts related to this, but none were able to help me. So, I have the following structs defined:
typedef struct
{
int created;
double data;
int timeLeft;
int destination;
}dataPacket;
typedef struct
{
dataPacket *array;
int currIndex;
int firstIndex;
int nextTick;
int maxLength;
int length;
int stime;
int total;
}packetBuffer;
typedef struct{
int mac;
struct wire *lconnection;
struct wire *rconnection;
int numRecieved;
struct packetBuffer *buffer;
int i;
int backoff;
}node;
typedef struct{
float length;
float speed;
int busy;
struct dataPacket *currPacket;
struct node *lnode;
struct node *rnode;
}wire;
And then I'm trying to use the following function:
int sendPacket(node *n, int tick)
{
if(n->buffer->length > 0)
{
if(n->backoff <= 0)
{
if (n->lconnection->busy != 0 || n->lconnection->busy != 0)
{
n->i++;
n->backoff = (512/W * genrand()*(pow(2,n->i)-1))/TICK_LENGTH;
}
else
{
n->lconnection->busy = 1;
n->rconnection->busy = 1;
n->lconnection->currPacket = n->buffer[n->buffer->currIndex];
n->rconnection->currPacket = n->buffer[n->buffer->currIndex];
}
}
else
{
n->backoff--;
}
}
}
I'm getting the error described in the title everytime I try to access a member of buffer, lconnection, or rconnection.
struct packetBuffer *buffer;
You've defined a type packetBuffer (a typedef for an otherwise anonymous struct).
You haven't defined struct packetBuffer.
In the absence of an existing type struct packetBuffer, the compiler treats it as an incomplete type, assuming that you'll complete it later. The declaration
struct packetBuffer *buffer;
is perfectly legal, but you can't dereference buffer unless the type struct packetBuffer is visible.
Just drop the struct keyword.
(My personal preference is to drop the typedef and consistently refer to struct types as struct whatever, but that's a matter of style and taste.)
The following:
typedef struct {
int x;
char *y;
...
} my_struct;
creates an identifier for an anonymous structure. In order, for a structure to refer to an instance of itself, it must not be "anonymous":
typedef struct my_struct {
int x;
char *y;
struct my_struct *link
....
} my_struct_t;
This means that my_struct_t is now the type struct my_struct and not just an anonymous struct. Also, note that struct my_struct can be used within its own structure definition. That is not possible with anonymous structs.
As a final complication, the my_struct in struct my_struct is in a differenct "namespace" than the my_struct_t. This is sometimes used to to simplify (or confuse) things in code like this:
typedef struct my_struct {
int x;
char *y;
struct my_struct *link
....
} my_struct;
Now I can use my_struct anywhere in my code instead of struct my_struct.
Finally, you could separate the typedef from the structure definition to achieve the same effect:
struct my_struct {
int x;
char *y;
struct my_struct *link;
....
};
typedef struct my_struct my_struct;
As noted in David R.Hanson's C Interfaces and Implementations, "This definition is legal because structure, union, and enumeration tags occupy a same name space that is separate from the space for variables, functions, and type names."

Resources