/*simple way to understand structure pointer*/
#include<stdio.h>
struct data 1
{
float *e;
float *f;
};
struct data
{
int *a;
int *b;
struct data 1 *d1;
};
int main()
{
struct data d;
d.d1 = malloc(1);
d.a = malloc(1);
d.b = malloc(1);
d.d1 = malloc(1);
d.d1 - > e = malloc(1);
d.d1 -> f = malloc(1);
*d.a = 10;
*d.b = 5;
printf(“%d %d\n”,*d.a,*d.b);
printf(“%d\n”, sizeof(d));
printf(“%d\n”,sizeof(d.d1));
printf(“%f”,*d.d1->e);
return 0;
}
STRUCTURES
structure is a user defined data type available in c
programming,it allows us to combine data items of different kinds and
helps us to construct a complex data type in a meaningful way.
Structure is used to represent a record. Suppose we
want to keep track of books in a library. For this we have to track
some attributed(title,author,subject,book id) of each book. To
define a structure we must use the struct statement. This statement
defines a new data type with more then one member for our program .
the format for struct statement is :
struct [structure tag]
{
member definition;
member definition;
....
}[one or more structure variables];
the structure tag and structure variable is optional and
each member definition is a normal variable definition such as int
i; or float j or any other variable definition we can also declare
structure variable separately like:
struct structure tag structure variable1,structure
variable2... ;
a structure member has no meaning on its own so we
cannot access it directly. In order to access a structure member,the
member name must be linked with the structure variable using dot
(.)operator also called period or member access operator .we can also
use structure pointer operator if we are declaring a pointer
variable for that structure .
Structures can also be nested within other structure in
C programming and they can also be passed as a function argument like
any other variable.
Bit
fields:-
bit fields allow the packing of data in a structure.
This is specially useful when memory is limited . Suppose our
structure contains some true/false variables then we are going to
store either 0 or 1 in these variables. so C programming offers us a
better way to utilize the memory space in these situations . We can
define the width of variable which tells the C compiler that we are
going to use only those number of bits for example :
struct
{
unsigned int cgpa : 3;
}student;
the above structure definition instructs C compiler that
cgpa variable is going to use only
3 bits to store the value,if we try to use more then 3
bits then it will not allow us to do so.
It looks like you're new here. If you want to get involved, click one of these buttons!