Ask your own question, for FREE!
Computer Science 7 Online
OpenStudy (riyad):

i want to know about structure function in c programming

OpenStudy (anonymous):

it's used to define ur own data type.............like it's a boz or collection of different datatypes.....

OpenStudy (anonymous):

http://www.cprogramming.com/tutorial/c/lesson7.html

OpenStudy (anonymous):

i did not had a chance to study it ...... but i had some tutorial videos that they can help u... maybe u can give me ur email i will send them...

OpenStudy (anonymous):

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: struct node /* Node for singly-linked lists. */ { int datum; 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: struct list /* Singly-linked list. */ { struct node *head; struct node *tail; size_t len; }; 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; } return node; } void free_node(struct node *node) { free(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; } else { int i; 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; } return list; } 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!

OpenStudy (anonymous):

looks pretty much like a c++ struct struct structname { type datatypename; //.. //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 strucname struct_instance; //assigning values struct_instance.datatypename=//whatever appropriate value, don't try to assign a string to an int... //getting values out x= struct_instance.datatype 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...

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!