How does struct works under the hood? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am curious about struct that we declare in C. What does it actually do ?
I know we can make nodes & pointers to those using struct but how does it function ? Like a while loop checks the condition & branches accordingly if equal or not equal. What does struct do under the hood ?

A struct type is a user-defined composite type. It is composed of fields or members that can have different types.
From struct-wiki:
A struct in the C programming language is a declaration that defines a list of variables to be placed under one name in a block of memory, allowing the different variables to be accessed via a single pointer.
For memory allocation of a struct: check out How are C struct members allocated?
For why to use struct, check out Why should we typedef a struct so often in C?

Its nothing more than multiple variables treated as one entity. There is not much magic behind this, the values simply appear behind each other in the order of declaration in the memory.

A C struct simply represents data in a specified way and doesn't do anything at all. It is used to represent a more complex data-type, such as a linked-list node.

Using struct a user can define its own required data type to handle complex data.
just like array where all elements in array are of same type but in struct each element can be defined in user's desired way.
so struct is used to define ""user-defined data types".

Related

Non-copyable struct in C? [duplicate]

This question already has answers here:
How do you implement a class in C? [closed]
(16 answers)
Closed 3 years ago.
After reading this page, I already know how to implement non-copyable
classes in C++.
(How do I make this C++ object non-copyable?)
Now I want implement non-copyable in C,
But I don't find similar code in C.
So I want to ask how to implement in C.
You can do this using opaque pointers. The idea is:
You define a struct somewhere and you define all of its operations in terms of a pointer to that struct. That would probably be a standalone compilation unit.
The consumers of your struct only get a declaration but not the full definition of that struct, which means that they don't know the layout or even the size of the struct. It follows that they are able to receive, store, and pass around any pointers to that struct, but not values of it.

Using scandir and sorting by size [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Writing a program in C that is similar to ls. My program needs to implement the -S flag, meaning it sorts whatever directory is specified (or the current one if not) by the size of its contents in bytes.
Obviously you can easily do an alphabetical sorting with scandir and alphasort (see https://www.cs.cf.ac.uk/Dave/C/node20.html). My question is whether you can use scandir with a comparator function that compares two entries on the size of their contents.
Alternatively, I could just specify NULL for the comparator function, have the unsorted array, and then sort it myself with the qsort() function.
Any advice much appreciated. Any clarifications needed, just say in the comments.
Converting comments to an answer.
Since the POSIX declaration of scandir() is
int scandir(const char *dir, struct dirent ***namelist,
int (*sel)(const struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **));
you can write a comparator function that uses stat() on each of the two pointed at directory entries and compares the sizes in any way you see fit. The difficulty is caching the stat data; there isn't an obvious way to do that, but if you have thousands of files, you probably don't want to be running two stat() system calls each time you make a comparison — that would be pretty much the ultimate in 'slow compare vs fast swap'.
I was thinking of that, but then I thought that a lot of work would be done for even a few hundred files. Any suggestions?
I'd probably go with a a simple comparator (alphasort() perhaps), and then when the data's returned by scandir(), create an array of a data structure that can cache the file names and sizes (and any other data that you want for your comparisons), and then use qsort() on the array of the data structure.

Structure to array in c [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am currently studying the crazyflie 2.0 drone Firmware. For those who do not know the drone, here is a link to the Website:https://www.bitcraze.io/crazyflie-2/
It is an open source Project.
Anyway, I have toruble understanding some part of the Firmware Code. Actually it might be very simple, but I am very new to programming in C.
struct CommanderCrtpValues
{
float roll;
float pitch;
float yaw;
uint16_t thrust;
} __attribute__((packed));
static struct CommanderCrtpValues targetVal[2];
You can find this Piece of Code at: https://github.com/bitcraze
crazyflie-firmware/modules/src/commander.c
I do not understand the last line. I believe that it is an assignment of a struct to an Array, named targetVal, but I am not sure. Could you explain what's really going on ?
This is creating a static array of 2 CommanderCrtpValues structures.
Since this is declared in the global scope its memory is going to be initialized as 0 (i.e. the all the fields will have the value 0) when the program starts, see Why are global variables always initialized to '0', but not local variables?. I do not think this detail is important in this particular case but it is used in other places of the firmware.
(Disclaimer: I am the author of this code.)
The code defines a structure named CommanderCrtpValues that has four fields (roll, pitch, yaw, thrust).
The last line defines a variable named targetVal that is an array of 2 elements. Each array element is a structure of CommanderCrtpValues.
The variable targetVal has not been initialized yet so the actual content of each element is undefined. Even though you know that they are CommanderCrtpValues structures, you don't know what values the fields contain.

How to define a list of: variables1,variables2, variablesN [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to do the following:
#define ConfigureCAN(index,value) CAN.MBOX##index=value
for (i=0;i<15;i++) ConfigureCAN (i,0);
This doesn't work. Is there an other way then:
ConfigureCAN (0,0);
ConfigureCAN (1,0);
...
Thanks.
Edit: Wil this work Or better is the indexing of the stuct correct?
#define ConfigureCAN(index,value) {struct MBOX *Mailbox;Mailbox = &can.MBOX0 + (index * sizeof(struct MBOX)); MBOX=value;}
No, you can't use a for loop index variable (which only gets its value at run-time) to build a compile-time symbol name like that. The symbol names are not available when the program runs, i.e. when the for loop executes, so it doesn't make any sense.
You can probably set up a (pointer) array to make accesses easier to loop, but then you're going to have initialize the array which will be similiar-looking (but enable better run-time performance, if that's your goal).
There is no easy way to do so in C.
Depending on the framework and hardware you use, there might be an alternative way to address your CAN structs:
If you need the MBOX### values more often in a loop, you can create an array with pointers at an appropriate place:
volatile int * MBOXPOINTERS[] = { &CAN.MBOX0, &CAN.MBOX1, /* go on on your own */ };
(replace volatile int by whatever is used in the respective header file)
If you have the device data sheet and you see that the fields are in a successive way, you mght eveb ne able to define an array situated at the appropriate address. But that is highly hardware dependent.

Why do you have to access struct members with an '->'? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Normally you access structure-members by a dot (Struct.Member), but when Struct is a pointer, you have to use Struct->Member
Is there a special reason for this? Because in both instances you're just referring to a piece of memory. And the compiler should be able to handle both operators, so is it just an enforced codestyle ?
It's because the language is defined that way.
There are languages that allow the prefix of the . operator to be either a structure or a pointer to a structure. No ambiguity is introduced because, as you say, the compiler knows what type the prefix is.
C just happens not to do that. I don't think there's any deep reason for it, it's just the way Dennis Ritchie decided to do it.
And using . for structures and -> for pointers also makes it clear to the reader whether the prefix is a pointer or not. For a relatively low-level language like C, that kind of detail can be important.
You can access the struct elements in . notation if the struct is a pointer. You just have to dereference the struct elements
So :
(*struct).element should work fine

Resources