Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6231969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:01:59+00:00 2026-05-24T10:01:59+00:00

Motive: Passing struct to functions such as we can change it in any function

  • 0

Motive: Passing struct to functions such as we can change it in any function and retrieve any value in any function.

I picked up this code from: http://cboard.cprogramming.com/c-programming/126381-passing-structure-reference-through-4-functions.html#post942397

I tweaked it a little and here it is:

struct_passing.h

typedef struct thing {
    char *x;
}thing_t;

struct_passing.c


#include <stdio.h>
#include "struct_passing.h"

void f4(thing_t *bob) {
    bob->x = "changed";
}

void f3(thing_t *bob) {
    f4(bob);
        printf("inside f3 x: %s\n", bob->x);
}

void f2(thing_t *bob) {
    f3(bob);
}

void f1(thing_t *bob) {
    f2(bob);
}

int main(void) {
    thing_t foo;
    foo.x = "same";
    printf("Before: %s\n", foo.x);
    f1(&foo);
    printf("After: %s\n", foo.x);
    return 0;
}

It works as expected on **gcc version 4.4.3 on ubuntu

$ gcc -o struct struct_passing.c 
$ ./struct
Before: same
inside f3 x: changed
After: changed

But on gcc version 4.2.1 on freebsd, I cannot retrieve the changed value of “bob->x”. I do not get inside f3 x: changed. I get garbage instead.

Why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:02:00+00:00Added an answer on May 24, 2026 at 10:02 am

    Everything looks just fine to me. It’s always instructive to run a small sample program like this in the debugger, to see what’s really happening. If you don’t know gdb, now is a great time to start. I’ve created struct_passing.[ch] from your code, let’s have a look:

    [wes@eeegor ~]$ gcc -v
    Using built-in specs.
    Target: i386-undermydesk-freebsd
    Configured with: FreeBSD/i386 system compiler
    Thread model: posix
    gcc version 4.2.1 20070719  [FreeBSD]
    

    Yup, same compiler. Compile for debugging:

    [wes@eeegor ~/src]$ cc -g -o struct struct_passing.c
    

    And run it:

    [wes@eeegor ~/src]$ gdb struct
    GNU gdb 6.1.1 [FreeBSD]
    Copyright 2004 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "i386-marcel-freebsd"...
    

    OK, we probably want to know what’s happening from main() on, so…

    (gdb) b main
    Breakpoint 1 at 0x80484c0: file struct_passing.c, line 21.
    (gdb) run
    Starting program: /usr/home/wes/src/struct
    
    Breakpoint 1, main () at struct_passing.c:21
    21      int main(void) {
    

    We’ll mostly use (s)tep to step into functions, and (n)ext to step over functions we don’t want to see the inside of, like printf(). We may also occasionally (p)rint something.

    (gdb) n
    main () at struct_passing.c:23
    23          foo.x = "same";
    

    We haven’t yet executed this line, so if we look at foo, it will probably contain garbage:

    (gdb) p foo
    $1 = {x = 0x80482c5 "\203Ä\fÃ"}
    

    Yup, as expected, x points to some garbage string. That’s because foo, and by extension foo.x, got created on the stack in the main() function, and the stack just has whatever random junk was left around before.

    (gdb) n
    24          printf("Before: %s\n", foo.x);
    (gdb) n
    Before: same
    25          f1(&foo);
    (gdb) s
    f1 (bob=0xbfbfec40) at struct_passing.c:18
    18          f2(bob);
    (gdb) p bob
    $2 = (thing_t *) 0xbfbfec40
    

    So we step into f1() and see that we have correctly passed the address of foo into the function. Notice how the gdb (p)rint function always knows the type of what it’s printing? In this case, seeing the address of the structure isn’t very useful, so:

    (gdb) p *bob
    $3 = {x = 0x804857c "same"}
    

    Oh, good, the structure still looks like it should. Let’s keep going until we change it:

    (gdb) s
    f2 (bob=0xbfbfec40) at struct_passing.c:14
    14          f3(bob);
    (gdb) p *bob
    $4 = {x = 0x804857c "same"}
    (gdb) s
    f3 (bob=0xbfbfec40) at struct_passing.c:9
    9           f4(bob);
    (gdb) p *bob
    $5 = {x = 0x804857c "same"}
    (gdb) s
    f4 (bob=0xbfbfec40) at struct_passing.c:5
    5           bob->x = "changed";
    

    Remember, we haven’t executed line 5 yet, so “bob” should still be the same:

    (gdb) p *bob
    $6 = {x = 0x804857c "same"}
    

    Yup, so let’s execute line 5 and look again:

    (gdb) n
    6       }
    (gdb) p *bob
    $7 = {x = 0x8048563 "changed"}
    

    So “bob” did get changed. Let’s keep going and see if it’s still changed up in main():

    (gdb) n
    f3 (bob=0xbfbfec40) at struct_passing.c:10
    10              printf("inside f3 x: %s\n", bob->x);
    (gdb) n
    inside f3 x: changed
    11      }
    (gdb) n
    f2 (bob=0xbfbfec40) at struct_passing.c:15
    15      }
    (gdb) n
    f1 (bob=0xbfbfec40) at struct_passing.c:19
    19      }
    (gdb) n
    main () at struct_passing.c:26
    26          printf("After: %s\n", foo.x);
    (gdb) n
    After: changed
    27          return 0;
    

    So we got what we expected. At this point, we’re about to return from main(), it’s best to just let the debugger continue so you don’t half to walk through the tail end of the C runtime startup code:

    (gdb) c
    Continuing.
    
    Program exited normally.
    (gdb) Quit
    (gdb)
    

    Let’s exit the debugger and run it normally to make sure we get the same output. It would be really weird if we didn’t…

    (gdb) q
    [wes@eeegor ~/src]$ ./struct
    Before: same
    inside f3 x: changed
    After: changed
    

    Oh, good, the stars are still shining. I’m not sure what happened in your case, let’s try compiling as you did and run it:

    [wes@eeegor ~/src]$ gcc -o struct struct_passing.c
    [wes@eeegor ~/src]$ ./struct
    Before: same
    inside f3 x: changed
    After: changed
    

    So now, let’s improve your bug report. Give us the output of ‘uname -a’, ‘gcc -v’, and ‘ld -v’ so we can find out exactly what system you’re using. Also read the ‘Joel on Software’ article(s) about how to write a bug report. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am beginner to java and was trying out this code puzzle from the
my question is probably identical and have same motive as this here i ain't
How do you use the email->attach function? I can't figure what is happen, cos
I was wondering what is the design motive behind extension methods in C#
I guess this question of mine is pretty basic but since Ive never done
I have this table structure <table id=latest class=debt> <thead> <tr> <th width=50>Credor</th> <th width=50>Devedor</th>
I have never designed a database/data-model/schema from scratch, especially for a web-application. In some
I'm trying to stop a submit form with Javascript/jQuery, with a: $('form#checkRepair').submit(function() { return
I want to have something like MOTIVE { GET_PLAYER, GET_FLAG } . And in
Is there any way, in C++, to define nested macros/constants within a class, in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.