Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "look into" a struct?

Tags:

c

struct

Take the FILE type defined in stdio.h for example: Is there any way to get the information about its fields(name, size, offset, etc) without taking a look at the header? Sometimes it'll be convenient to have such a function/macro to check the components of a struct.

like image 528
nalzok Avatar asked Apr 08 '16 15:04

nalzok


People also ask

How do you access part of a struct?

Array elements are accessed using the Subscript variable, Similarly Structure members are accessed using dot [.] operator. Structure written inside another structure is called as nesting of two structures. Nested Structures are allowed in C Programming Language.

How do you convert a struct to an array in Matlab?

C = struct2cell( S ) converts a structure into a cell array. The cell array C contains values copied from the fields of S . The struct2cell function does not return field names. To return the field names in a cell array, use the fieldnames function.

Can you make an array of structs in Matlab?

You can create an array of structures from a scalar structure by using the MATLAB repmat function, which replicates and tiles an existing scalar structure: Create a scalar structure, as described in Define Scalar Structures for Code Generation.


4 Answers

No.

There's no meta data associated with data structures in C, all of that is lost when compiling.

And it's perfectly possible, since FILE is opaque, that no public header actually has the definition. It could just be typedef struct __FILE FILE; in the library header, and then all the details be kept on the inside, possibly in code you don't even have the source to.

like image 117
unwind Avatar answered Oct 19 '22 10:10

unwind


The simple answer is no. You have to have the source code.

like image 37
Logicrat Avatar answered Oct 19 '22 11:10

Logicrat


In a C based structure, the data is stored in a way that is not "self defining" - you must know the structure definition to interpret the data. This reduces the size of the data to its bare minimum, and makes access faster, provided that your program understands the structure.

like image 23
Byron Jones Avatar answered Oct 19 '22 10:10

Byron Jones


The answer is no.

Whenever, i needed to find information about a struct's data member, Header File and comments over there were sufficient for me.

And you can't have a function/macro to check the components of a struct because there is no meta data associated with the variables and procedures in C.

like image 1
abhiarora Avatar answered Oct 19 '22 09:10

abhiarora