Thread: Help in passing structure elements through function

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    24

    Help in passing structure elements through function

    hello. I am studying C from "C programming-A modern approach" by 'KN King'. I am stuck at structures topic while he tells about a function, using which we can pass & assign the elements of structure. I am not able to complete it.

    Code:
    #include<stdio.h>
    #include<string.h>
    struct part_build (int num,char *name, int on_hand)
    {
    struct part p;
    
    p.number=num;
    strcpy(p.name,name);
    p.on_hand=on_hand;
    return p;
    }
    
    int main(void)
    {
    struct part part1;  //IT WASN'T THERE IN BOOK, BUT WRITING IT HERE AVOIDS AN ERROR
    part1=part_build(1,"tyre",22);
    }
    I am new to structures and it is spinning me since yesterday... Thanks...

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Something like this?

    Code:
    #include <stdio.h>
    
    struct test
    {
        int d;
    };
    
    void fillStruct(struct test * t)
    {
        t->d = 10;
    }
    void printStruct(struct test * t)
    {
        printf("t->d = %d\n",  t->d);
    }
    int main()
    {
        struct test t1 = {0};
    
        fillStruct(&t1);
    
        printStruct(&t1);
       return 0;
    }
    Most questions are already answered in the FAQ FAQ > All about structures - Cprogramming.com
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    24
    Thanks alot vart!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access vector<structure> elements in a function, how?
    By KeithS in forum C++ Programming
    Replies: 5
    Last Post: 10-25-2011, 10:56 AM
  2. passing structure to function
    By danieldcc in forum C Programming
    Replies: 4
    Last Post: 10-10-2011, 11:17 AM
  3. Passing Function to Structure
    By C-bob in forum C Programming
    Replies: 11
    Last Post: 07-22-2011, 07:36 PM
  4. Passing structure to function
    By Sereby in forum C Programming
    Replies: 4
    Last Post: 07-28-2004, 10:05 PM
  5. Passing a structure to a function?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2001, 04:28 AM