i want to know about structure function in c programming
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
it's used to define ur own data type.............like it's a boz or collection of different datatypes.....
Not the answer you are looking for? Search for more explanations.
The struct keyword tells the compiler to bundle together a bunch of different data into one object, and as A.Avinash_Goutham mentioned, it's mainly used to define new types.
For example, let's say one needs to program a singly-linked list in C. By default, C doesn't have any primitives to provide linked lists (in constrast to languages like Lisp), nor does C's standard library (in contrast to languages like C++ or Java that provide a bunch of general collection types). With C structs, you can implement singly-linked lists yourself :)
One can start by defining the individual nodes of the singly-linked list. Without getting too much into the syntax details to accomplish this in C:
/* Node for singly-linked lists. */
struct node *next;
In the above struct definition you have a `node' object that contains two fields: a field to put data (of type int) in and another field that contains a `next' pointer to a node object.
Might want to go further and specify lists themselves:
/* Singly-linked list. */
struct node *head;
struct node *tail;
You can define functions to allocate or initialize those structs for you:
struct node *new_node(int datum, struct node *next)
struct node *node = malloc(sizeof(*node));
if (node != NULL)
node->datum = datum;
node->next = next;
void free_node(struct node *node)
You can also define functions to perform operations on your structs:
void append_node(struct list *list, struct node *node)
Add a node to the end of a singly-linked list.
Probably better to define insert and delete instead but I'm just making an example :D
list->tail->next = node;
//Now you can do this:
struct list *new_list(size_t len, int init)
Initialize a singly-linked list with `len' nodes, each initialized to `init', and
return a pointer to the list.
struct list *list = malloc(sizeof(*list));
if (list != NULL)
if (len == 0)
list->head = NULL;
list->tail = NULL;
list->head = new_node(init,NULL);
list->tail = list->head;
for (i = 1; i < len; ++i)
append_node(list, new_node(init, NULL));
list->len = len;
Things just go downhill after this point. :D As you can see, C structs help us define our own data structures. Though C structs aren't as powerful as the Classes and Objects you see in other languages (by comparison, C structs are just dumb data structures with no embedded logic), they get the job of grouping together variables related to particular objects quite well. Imagine implementing a linked list in C (or any data structure apart from array and list) without the use of structs!
looks pretty much like a c++ struct
//etc.. as many other data members as you like can be of an type, even classes
}; //DO NOT forget the semicolon
defining a struct in your program// NOTE: must declare the struct before you use it
struct_instance.datatypename=//whatever appropriate value, don't try to assign a string to an int...
//getting values out
also note struct stores values in the order they are declared... so you can assign a pointer to a struct and read off values as any type by using a void pointer and then converting it to t type pointer...