Pointer to a structure that has not been declared [duplicate] - c

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
undefined C struct forward declaration
How is it possible to declare a pointer to structure even when I do not declare a structure?
#include<stdio.h>
int main(){
struct s{
struct p *ptr;
};
}
Why does the above compile successfully?

It's possible because the compiler doesn't need to know anything about the structure if it only deals with a pointer to it.
This is a commonly used technique and is usually called an “opaque pointer”.

Look into the below link
http://cboard.cprogramming.com/cplusplus-programming/100298-opaque-pointer.html

Related

Is it possible to pass a struct to a function without creating a variable? [duplicate]

This question already has an answer here:
Is it possible to pass a structure variable as a function argument without previously defining it?
(1 answer)
Closed 3 years ago.
In C++ this code is valid
struct foo{
int x;
};
int bar(foo f);
bar({1});
However I get an error when I try to do something similar in C. Is there way a to pass a struct to a function without actually creating a variable for it?
You need a compound literal for this:
bar((struct foo){1});

What is the meaning of _Space in C? [duplicate]

This question already has answers here:
Why should we typedef a struct so often in C?
(15 answers)
Closed 20 days ago.
I have seen this expression in the code of other developer and I cannot get the meaning of it, the code line is:
typedef struct _Space Space;
So, for the syntax I reckon that _Space is a kind of variable or something similar but I do not know what kind of variable it is (integer, string, Boolean, etc).
Anyone have any idea??
struct _Space refers to a definition of a struct _Space elsewhere. The typedef ... Space means you can refer to a struct _Space as simply Space, saving you some typing.
For example, consider the difference in brevity and clarity between
struct _Space mySpace; // Oh god! memories
vs
Space mySpace;

Using typedef keyword in c [duplicate]

This question already has answers here:
How to create a typedef for function pointers
(3 answers)
Closed 5 years ago.
I was going through one of the lecture slide taught in the class which included a typedef keyword in it. As opposed to normal use of typedef keyword where we usually put a alias for particular data, there was no alias put up for that example. Following is the example:See the typedef in the slide
Is the use of typedef correct, or am I interpreting differently?
It is a function pointer. And it is perfectly valid syntax.
Ptr is declared as a pointer to a function where the function takes two int arguments and return an int. Here you can use Ptr as the alias to the typedefed type.

understanding structs and pointers and typedefs [duplicate]

This question already has answers here:
Is it a good idea to typedef pointers?
(15 answers)
Closed 8 years ago.
i would like to understand when creating a pointer to a struct is it necessary to declare a typedef struct declaration of this nature:
what i mean is
typedef struct _something{
int okay;
}blah;
typedef struct _something *finger;
what is the reason that it is declared in this way , why not just declare a pointer this way
blah *arm;
so if somebody can help me understand this and which is used in which case and why and its advantages?
When using typedef struct _something, it means that each time you write _something it's as if your are writing struct _something. The typedef command just tells to replace what is written. So in other words, it avoids you to write struct _something.
Hoping it helps

c99 Dereferencing pointer to incomplete type [duplicate]

This question already has an answer here:
Dereferencing pointer to incomplete type
(1 answer)
Closed 9 years ago.
I have this in input_lib.c:
#include "input_lib.h"
struct edge {
int from;
int to;
};
input_lib.h contains my typedef:
typedef struct edge edge;
Then in another file which includes input_lib.h, I declare a variable and attempt to use it:
edge *my_edge = read_next_edge();
printf("%d, %d", my_edge->from, my_edge->to);
I get the error at the printf line:
error: dereferencing pointer to incomplete type
read_next_edge() returns an edge* that is allocated by:
edge *next_edge = malloc(sizeof(struct edge));
My C is a little rusty so I assume I've just made a small mistake somewhere, but I'm having a really hard time figuring out where. I would really appreciate if someone could explain where my mistake is, and why I'm getting it. Thanks
Your struct definition should be in input_lib.h
If you are expecting to access the internals of the structure outside of input_lib.c, then the structure definition needs to be in input_lib.h. If you are expecting code outside of input_lib.c to use access functions to get at the information, then you can leave the structure opaque (incomplete) but you need to call the functions to get the information.
Either way works; choose one and stick with it.

Resources