Help in passing structure elements through function

This is a discussion on Help in passing structure elements through function within the C Programming forums, part of the General Programming Boards category; hello. I am studying C from "C programming-A modern approach" by 'KN King'. I am stuck at structures topic while ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 11:56 AM
  2. passing structure to function
    By danieldcc in forum C Programming
    Replies: 4
    Last Post: 10-10-2011, 12:17 PM
  3. Passing Function to Structure
    By C-bob in forum C Programming
    Replies: 11
    Last Post: 07-22-2011, 08:36 PM
  4. Passing structure to function
    By Sereby in forum C Programming
    Replies: 4
    Last Post: 07-28-2004, 11: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21