Structures and pointers question

This is a discussion on Structures and pointers question within the C Programming forums, part of the General Programming Boards category; If I have the structure below: Code: 10 struct 11 { 12 int cin; 13 int mo; 14 int next_step; ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Structures and pointers question

    If I have the structure below:
    Code:
         10 struct
         11      {
         12         int cin;
         13         int mo;
         14         int next_step;
         15         int tested_cin;
         16      } coil_info;
    and in SQL declare it states:
    Code:
         335         EXEC SQL BEGIN DECLARE SECTION;
         336          coil_info coil;
         337         EXEC SQL END DECLARE SECTION;
    when (&coil) and (coil) is passed to functions like below what is the difference between the two?

    update_coil(coil)
    then using coil->cin

    update_coil(&coil)
    then using coil->cin

    The two would have the same value correct?

    One passed address of pointer and other address of the actual data? I see no difference. Any help is appreciated.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    coil IS the struct, while &coil is its address. coil is not in any way, shape, or form an address. coil->cin should not compile.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    update_coil(coil)
    then using coil->cin
    You only pass a copy of the object coil: the value of cin after the call is still
    the same value than before the call.

    update_coil(&coil)
    then using coil->cin
    This time you pass the object itself (through its address) so the function
    can modify its content: cin contains the updated value after the call.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-23-2008, 03:22 AM
  2. Pointers to structures - Beginner question
    By RobJ in forum C Programming
    Replies: 6
    Last Post: 04-10-2006, 05:57 PM
  3. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 05:31 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Freeing pointers in structures
    By jim50498 in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 11:53 AM

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