Thread: A few questions for coding experience and passing struct through function

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    11

    Question A few questions for coding experience and passing struct through function

    Hello everyone,
    I had been working on my assignment to create a C program, I wrote out the whole things which about more than 200 lines and the coding words look like a pile of cluster spilled everywhere. I had found some error but it's quite confusing and time consuming on making it work.
    So yeah, anyone had a very effective ways or steps to test out only a part or segments of the code easily? Like copy paste into new C file and run it?

    Also, I would like to ask about passing the structure value to other function.
    like this:
    Code:
    //assumed I had typedef structure with tag "Test"
    function(Test info[]);
    function2(Test info[]);
    main()
    {
       Test info[10];
       function(info);
    }
    
    function(Test info[])
    {
       function2(info);
    }
    Can I do this if I only want 1 structure to store all and change the values from function() and function2()?

    So yeah, that's is my question.
    any answer, tips or helps is appreciated.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can pass an array of structs, just like you can pass any other array to a function. Arrays are passed by copying the address (pass by reference), so yes, you can change the values of a passed array, in any function to pass the array to.

    I didn't understand the "1 structure" part, but you can pass a pointer to a struct, and have it pointing at any struct in the array - and pass it then into any function you like. Sort of like passing a piece of a pie at the Thanksgiving table, but not passing the whole pie. OK, so I'm hungry.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by Blane View Post
    So yeah, anyone had a very effective ways or steps to test out only a part or segments of the code easily? Like copy paste into new C file and run it?
    Unfortunately, most of this comes with experience. Start small and work your way up - begin by writing "hello world" (this is still how all my applications start off) and build from there by taking small steps. The more experienced you get, the bigger your steps will get and the fewer errors per step you'll get.


    Quote Originally Posted by Blane View Post
    Can I do this if I only want 1 structure to store all and change the values from function() and function2()?
    I'm not quite sure what you're asking - in what you've written, there will only be one Test array, and that will be passed around "by reference", so any modifications that function() and function2() make to it will change the original array.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    11
    Quote Originally Posted by Adak View Post
    You can pass an array of structs, just like you can pass any other array to a function. Arrays are passed by copying the address (pass by reference), so yes, you can change the values of a passed array, in any function to pass the array to.

    I didn't understand the "1 structure" part, but you can pass a pointer to a struct, and have it pointing at any struct in the array - and pass it then into any function you like. Sort of like passing a piece of a pie at the Thanksgiving table, but not passing the whole pie. OK, so I'm hungry.
    Quote Originally Posted by JohnGraham View Post
    Unfortunately, most of this comes with experience. Start small and work your way up - begin by writing "hello world" (this is still how all my applications start off) and build from there by taking small steps. The more experienced you get, the bigger your steps will get and the fewer errors per step you'll get.




    I'm not quite sure what you're asking - in what you've written, there will only be one Test array, and that will be passed around "by reference", so any modifications that function() and function2() make to it will change the original array.
    I see, glad to hear that I'm doing right in passing the structure to the function, thanks you guys~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing struct to function
    By silentkarma in forum Windows Programming
    Replies: 15
    Last Post: 03-07-2008, 12:57 AM
  2. passing struct to function by reference
    By ironfistchamp in forum C++ Programming
    Replies: 11
    Last Post: 07-03-2006, 02:05 PM
  3. passing struct to function help
    By staticalloc in forum C Programming
    Replies: 4
    Last Post: 10-06-2004, 08:30 AM
  4. Passing Pointer-to-struct to a function!!
    By Lau in forum C Programming
    Replies: 5
    Last Post: 11-18-2002, 12:59 PM
  5. passing struct to function
    By blight2c in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2002, 12:52 AM