passing a structure pointer by value

This is a discussion on passing a structure pointer by value within the C Programming forums, part of the General Programming Boards category; hello all, well I am getting really frustrated with something. I read on a post here that passing by value ...

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    passing a structure pointer by value

    hello all,
    well I am getting really frustrated with something. I read on a post here that passing by value is supported in C, and that bassing by referance is only supported by C++. I am programming an program with a .CPP extension, but my application is using the C standard library and what I would consider at least 95% of C style code (I've yet to learn alot of C++).

    anyways I also read here that passing structures by value can be beneficial over passing by reference with larger structures. I am just wondering how I can get the contents of the structure in my function. I keep trying different ways, and I keep getting errors. anyways here is what my code looks like:

    Code:
    typedef struct s {
       int i;
       char a;
    } S, *PS;
    
    void somefunc(PS);
    
    int main(void)
    {
       S var;
       PS pvar = &var;
       pvar->i = 10;
       pvar->a = 'b';
    
       somefunc(pvar);
    
       return 0;
    }
    
    void somefunc(PS ps)
    {
       /* now, I would like to declare a local variable of s, but I don't
       know if I should use the S or PS type. I am assuming I should 
       use a referance to assign the local variable the contents of ps, 
       but I am also having problems with this. */
    }
    my structure is larger than that, that is just an example (if it was that small, I wouldn't bother passing by pointer). as you can see I am totally unsure of things locally in somefunc(), and like I said I have tried just about every possible way I can think of and keep getting errors. if someone could please show me how things should look in somefunc(), it would be GREATLY appreciated.

    thank you very much in advance!

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    You can just use the ps pointer that somefunc() receives directly:
    Code:
    void somefunc(PS ps)
    {
      printf("%d - %c\n", ps->i, ps->a);
    }
    The -> operator is the equivalent of doing (*ps).i or (*ps).a
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    > I read on a post here that passing by value is supported in C, and that bassing by referance is only supported by C++.
    True.

    > anyways I also read here that passing structures by value can be beneficial over passing by reference with larger structures.
    That's the first time I heard something like that. It's not likely to be true, since a pointer to a struct is the size of the struct itself. It wouldn't save you much memory or time. In a previous thread I posted the way to mimic a reference in C, you might want to read that thread. Passing structure by reference or pointer?
    In a way you are still passing by value, but whereas one pointer can get to be very big, a pointer to a pointer is not.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Quote Originally Posted by citizen
    since a pointer to a struct is the size of the struct itself.
    A pointer to a struct is the size of a pointer.
    Code:
    itsme@itsme:~/C$ cat structexample.c
    #include <stdio.h>
    
    int main(void)
    {
      struct foo { int i; char a; };
    
      struct foo bar;
      struct foo *whee;
    
      printf("sizeof bar: %u\n", sizeof(bar));
      printf("sizeof whee: %u\n", sizeof(whee));
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./structexample
    sizeof bar: 8
    sizeof whee: 4
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>I am programming an program with a .CPP extension<<

    Generally, cpp files are treated as c++ source and compiled accordingly; similarly, 'c' source files are compiled as c. Unless your compiler is for 'c' only or you have supplied extra compiler switches, this will probably be compiled as c++ and not c.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  6. #6
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    thanks for your reply itsme86. I got it working fine now.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Code:
    void somefunc(PS ps)
    {
          S s = *ps;
    }
    The contents of *ps are copied by value into s

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-13-2009, 02:31 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  4. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  5. passing pointer to structure to a function
    By samual_van in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2005, 07:18 PM

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