This might be a pretty basic question but I am a newbie in C so please bear with me.
I am going through some documentation for a firmware written in C where I came across this:
typedef struct {
int x;
int arr[10];
} sx;
Then I have a function where a pointer to that type is being passed.
int f(sx xtype *sp, int i){
return sp -> arr[i];
}
I am not sure where about two things:
From my Java experience I think the first snippet is equivalent to a superclass much like Object class in Java and then the second snippet is using a subclass of sx. However I am not sure. Can anyone please confirm?
From my little C experience so far, I think more easy-to-understand way to pass a pointer to the type would be as follows:
typedef struct xtype {
int x;
int arr[10];
} sx;
int f(sx *sp, int i){
return sp -> arr[i];
}
But I just suspect I am missing something from this picture.
Thanks in advance!
This is unusual syntax. I suspect that
xtypeis a macro defined somewhere and could look something like thisAt any rate, the declaration of a variable
sx xtype *spdoes not say thatsprefers to a subtypextypeofsxor this is the worst pre-processor magic I’ve seen in a long time.As for your second example, this doesn’t model sub-classing, either. What this:
says is that
struct Ais the same asB. It’s atypedef: It just defines a new type, a short rephrased version of the longer name.It has nothing to do with inheritance.