Passing struct

This is a discussion on Passing struct within the C Programming forums, part of the General Programming Boards category; Hi, I have the example code: Code: #include <stdio.h> #include <stdlib.h> struct x { int a; int b; }; void ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    30

    Passing struct

    Hi,

    I have the example code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct x
      {
             int a;
             int b;
      };
    
    void funcX(struct x ST)
    {
         printf ("INT: %i\n", ST.a);
    }
    
    void funcChange(struct x ST)
    {
         ST.a = 10;
         printf ("INT: %i\n", ST.a);
    }
    
    
    int main(int argc, char *argv[])
    {
      struct x ST = {1,2};
      
      funcX(ST);
      
      funcChange (ST);
      printf ("INT: %i\n", ST.a);
      system("PAUSE");	
      return 0;
    }
    I'm wanting to pass my struct to funcChange and change the value in the original struct. How do I do this? The output I get from the above is:
    Code:
    INT: 1
    INT: 10
    INT: 1
    Press any key to continue . . .

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    You need to pass it as a pointer I think.

    When you pass the struct by value it creates a new copy of it.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Thanks for the response, I figured that would be the case. But how do I pass a struct as a pointer?

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Got it thanks to

    http://cboard.cprogramming.com/archi...hp/t-5569.html

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct foo {
    int bar;
    };
    
    void func1 ( struct foo by_value ) {
    by_value.bar = 1;
    }
    void func2 ( struct foo *by_ptr )
    {
    // this is perhaps preferred
    by_ptr->bar = 11;
    
    // but you can write either of these as well :)
    // (*by_ptr).bar = 1;
    // by_ptr[0].bar = 1;
    }
    
    int main ( ) {
    struct foo v;
    func1( v );
    printf ("BAR: %i\n", v.bar);
    func2( &v );
    printf ("BAR: %i\n", v.bar);
    system("PAUSE");
    
    return 0;
    }

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    If are you going to use pointers, I do suggest reading
    http://cpwiki.sf.net/A_pointer_on_pointers
    Pointers are tricky monsters and can be dangerous if misused.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  2. passing struct
    By Doxygen in forum C Programming
    Replies: 2
    Last Post: 10-26-2006, 03:05 AM
  3. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  4. Passing a struct
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 03-14-2005, 10:02 AM
  5. passing struct to function help
    By staticalloc in forum C Programming
    Replies: 4
    Last Post: 10-06-2004, 08:30 AM

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