Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Riyad Group Title

i want to know about structure function in c programming

  • 2 years ago
  • 2 years ago

  • This Question is Open
  1. A.Avinash_Goutham Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • 2 years ago
  2. kevissen Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • 2 years ago
  3. msamido Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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...

    • 2 years ago
  4. agdgdgdgwngo Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    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!

    • 2 years ago
  5. fluffybunny Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    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...

    • 2 years ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.