Are these two structs equivilant? - c

I'm a bit new to structs and just wondering if these two structs are equivalent. I dont know if this is relevant, but im using these structs to build a stack.
Is this:
struct dataT
{
int m;
};
struct stack
{
int top;
struct dataT items[100];
} st;
equivalent to this?
struct stack
{
int top;
int items[100];
} st;
Arent these basically the same? The second method only uses one struct.
If im wrong, could you please tell me why it is not.

It will make a difference if you ever decide to change the dataT structure. If you know that it will always be only a wrapper for int, then it is a pretty useless structure. But if it may change, then the top one will probably be easier to maintain.

Related

Initializing size of struct array after declaration in C?

I've done some searching around, but nothing I've found has answered my question specifically regarding structs:
In my program I have two structures: one contains data, the other contains an array of the first structure (used to make returning struct arrays easier).
I do not know the size of the aforementioned structure array until running a few lines of code. From what I've found there isn't a way to define the size of an array (or any type) after it has already been declared, although I would like to know if this holds true for structs.
If not, is there way to define the size of a previously declared array within a struct without using malloc? Using malloc with struct arrays seems a bit complicated and I haven't been able to wrap my head around it.
typedef struct
{
char **data;
} struct1;
typedef struct
{
struct1 data[];
} struct2;
int main(int argc, char **argv)
{
//Pretend there is code here that finds the size I need
size = 5;
struct2 Info;
Info.data = new struct1[size]; //Clearly this won't work, but does C offer anything comparable?
return 0;
}

Using a struct inside a struct

How can I use struct A to modify the data inside a struct B. Which has no name, just a type.
struct A {
struct B;
};
struct B {
int data;
};
Since this is for school, I can't change the code above. I can only use it. I tried something like this for my main but it doesn't work
int main (){
struct A myStruct;
myStruct.B.data = 3;
return 0;
}
Thanks in advance.
Edit: Sorry I was just trying to post this as fast as possible that's why I didn't post this with proper c syntax. Anyway, it's my fault for not being clear enough on my question.
I'm aware that my main doesn't work I just want to know if it's ever possible to access the data inside struct B without declaring a name for it inside struct A as I have above. This is the code I was given by a teacher, so I didn't want to modify the structs because I thought maybe she wants us to brainstorm a way to use it the way she wrote it.
The way iharob explains it works perfectly by declaring struct B before struct A, and actually giving a name to struct B.
Is it simply not possible to access that data inside struct B without giving it a name?
The code you posted is not even c code, it would not compile.
Your main mistake is that you don't need to use the struct name to access the member. This should be good
struct B
{
int data;
};
struct A
{
struct B member;
};
int main(void)
{
struct A instance;
instance.member.data = 3;
return 0;
}
I assume that you posted some sample code, don't do that. Post the actual code that has issues. The code you posted is completely invalid because some one of the definitions lack the type, you can't declare structs without using struct in c except if you typedef it. So please post the actual code the next time.
And don't build such complicated structs with struct members unless you really know what you are doing.

How to program these structures?

I have three objects, which (at the moment) I am representing as structs:
a Dataset
a DatasetWindow
a MovingWindow
and a variable windowSize
There may be multiple Dataset's and each should have it's own DatasetWindow.
Ok, fair enough, to me that sounds like I make a DatasetWindow struct and put it as a member of a Dataset struct
There would be just one MovingWindow, but it should know about all the Dataset's.
Ok, so far it seems pretty simple. I create another struct for MovingWindow and it has a pointer to Dataset. (an array of datasets).
So so far, I have something like this:
typedef struct {
int *buffer;
int someOtherMember;
} DatasetWindow;
typedef struct {
int someMember;
DatasetWindow *window; //Pointer to a DatasetWindow obj.
} Dataset;
typedef struct {
int offset;
int someMember;
Dataset *datasets; //Array of Dataset
} MovingWindow;
The part I am having trouble with is this:
MovingWindow should know windowSize, as should each DatasetWindow.
But Dataset should preferably not need to know what windowSize is.
I don't know how to arrange my structures to support this?
You can modify the definitions of structs MovingWindow and DatasetWindow to incorporate a member (pointer or a interger variable) holding the value of your windowSize.

C - Functions Structs

So I am still pretty new to C programming. I have learned Python though, so I am familliar to some of the codes.
For instance when I create a function in python, I am able to make it general and usable for different classes.
I want to do something similar here. I have two structs which look practically the same. I want to use the same function for both structs, but ofcourse I cant send in the struct name as an argument into the function. What do I do instead?
For now dont worry about what the function does. Its the principle of being able to use two structs in the same function that counts for me. If this is a totally wrong perspective, then I am sorry but this was my first thought when coming upon this problem.
typedef struct{
int number;
struct node *next;
}struct_1;
struct node *head;
typedef struct{
int number;
struct node *next;
}struct_2;
void main()
{
int number1 = 10;
int number2 = 20;
function(number1);
function(number2);
}
void function(int x, struct) // Here is where I want to be able to use 2 different structs for the same function
{
struct *curr, *head;
curr=(node1*)malloc(sizeof(node1));
printf("%d", curr->number);
}
You could have two instances of one structure.
The function can accept either instance and process it as needed.
typedef struct{
int number;
struct node *next;
}mystruct;
void function(int x, mystruct *eachstruct);//prototype
int main()
{
int number1 = 10;
int number2 = 20;
//declare two instances of mystruct
mystruct list_1 = { 0, NULL};
mystruct list_2 = { 0, NULL};
// call the function with one or the other instance of mystruct
function(number1, &list_1);
function(number2, &list_2);
}
void function(int x, mystruct *eachstruct)
{
//do stuff in function
eachstruct->number = x;
if ( eachstruct->next == NULL)
{
//do more stuff
}
}
C does not use duck typing as Python does so you cannot pass one structure that looks like other, completely unrelated structure as if it was this other structure.
Unfortunately C cannot do what you want.
Your options are:
Refactor the code to use the same struct type for all items.
Pass the fields of interest in the structs directly to the functions
Write code to marshal the similar structs to a common struct.
Play fast and loose with the type system and arrange shared elements the same way in the two different structs and cast your pointers.
If you just want a linked list check out how code re-use is achieved in the linux kernel
Answer: No, you cannot do it directly. Welcome to static typing.
There is a way to achieve something similar by using our beloved void * and some castings but, believe me, it is not what you want to do. If you really want to do it ask directly for it. You have been warned.

What is the point of creating an array of a size of 1?

I've seen some C code that creates a structure and within the structure there are a number of arrays. Some of those arrays are of size one. So why bother making it an array? Why not just a single int?
I'm talking about something like this:
struct Foo
{
uint8_t Bar[1];
uint32_t BigBar[4];
};
Why not make it just
struct Foo
{
uint8_t Bar;
uint32_t BigBar[4];
};
The answer is that it is good programming habit to do so from two reasons:
In case the programmer decides to change the Bar into an array at some point, there is not a lot of code changing. All needed to be done is to change the constant from 1 to ARRAY_SIZE (it is even better to have the constant defined as one actually)
Using fields which are constructed the same are less prone to mistakes than fields which are different. Thinking programmers are ones who make mistakes :)
Cheers
My guess is for an easier return of a pointer:
it will be easier to return the pointer to the variable:Bar
if you will want to use it as a pointer you would be able to pass Bar instead of &Bar if it was an int
if I had an instance of the struct defined:
struct Foo aaa;
you would be able to define:
int *pInt = aaaa.Bar;
instead of:
int *pInt = &(aaaa.Bar);
Consistency with other structs?
struct alien {
int heads[3];
...
};
struct human {
int heads[1];
...
};

Resources