Re: Pointer to structure Problems

Discussion in 'C Programming' started by Robert W Hand, Jul 17, 2003.

  1. On Thu, 17 Jul 2003 11:11:26 +0100, "Steven Graham" <>
    wrote:

    > What is
    >going on. BTW this little psudo-C code summarises my problem. (main is at
    >the bottom if you want to start following from there)

    <snipped pseudocode>

    Pseudocode rarely helps in these type of problems. I have fleshed out
    your code into something compilable by Comeau. I've included the
    output below.

    typedef struct
    {
    char item1;
    int item2;
    } A_STRUCT;

    void function2(A_STRUCT *dest_struct)
    {
    //Put these into the strict that was called by reference
    dest_struct->item1 = 'A';
    dest_struct->item2 = 1000;

    printf("f2: %c\n", dest_struct->item1); //Yey it changed!
    }

    void function1(void)
    {
    A_STRUCT my_struct = {'B', 1};
    printf("f1: %c\n", my_struct.item1);
    // This shows my original non-junk values

    //now populate the struct
    function2(&my_struct);

    printf("f1: %c\n", my_struct.item1);
    // It has have changed!!
    }



    int main(void)
    {
    function1();
    return 0;
    }

    Output:
    f1: B
    f2: A
    f1: A

    It seems to work fine here. I would avoid the undefined behavior that
    you invoked by not initializing your structure. Is there a difference
    between your real code and my code?

    Best wishes,

    Bob
    Robert W Hand, Jul 17, 2003
    #1
    1. Advertising

  2. Robert W Hand

    Steve Graham Guest

    Not sure what was cauing the problem, my program wasn't a simle DOS app
    (custom startup code, loaded from a boot sector etc), what I have done is
    create a global struct in the headder file and just using that in all
    functions. Not as ellagant but it works. There could be an underlining
    problem with this but I can't find the cause.

    Thanks for your suggestions and your help
    Steven Graham
    Steve Graham, Jul 17, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. milkyway

    structure and pointer problems?

    milkyway, Dec 8, 2005, in forum: C Programming
    Replies:
    2
    Views:
    451
    Ron Natalie
    Dec 8, 2005
  2. milkyway

    structure and pointer problems?

    milkyway, Dec 8, 2005, in forum: C Programming
    Replies:
    0
    Views:
    260
    milkyway
    Dec 8, 2005
  3. Replies:
    6
    Views:
    371
    Jack Klein
    Aug 27, 2006
  4. Mike

    Memory Structure Pointer Problems

    Mike, Sep 25, 2006, in forum: C Programming
    Replies:
    2
    Views:
    322
  5. A
    Replies:
    27
    Views:
    1,596
    Jorgen Grahn
    Apr 17, 2011
Loading...

Share This Page