Struct delcaring a struct within itself question

This is a discussion on Struct delcaring a struct within itself question within the C Programming forums, part of the General Programming Boards category; edit : ^^^ Title should be referring to a struct pointer, ty Stiltskin Code: struct foo{ int num; char *word; ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    11

    Struct delcaring a struct within itself question

    edit : ^^^ Title should be referring to a struct pointer, ty Stiltskin


    Code:
    struct foo{
            int num;
            char *word;
            struct foo *ptr;
    };
    void func1(struct foo);
    void func2(struct foo*);
    void func3(struct foo);
    
    int main(){
    ...
    ...
     a.ptr = &a;
            a.num = 50;
            a.word = "mylastword";
            func3(a);
            printf("4 %d %s\n", a.num, a.word);
    ...
    }
    
    
    void func3(struct foo a)
    {
            if(a.num > a.ptr->num)
              { a.num = 500; }
            else
              { a.num = a.ptr->num + 1; }
    
            a.word = "myotherword";
            a.ptr->word = "yetanotherword";
            printf("3  %d %s\n", a.num, a.word);
    }
    Could someone explain to me what happened at the line with a.ptr = &a. It seems the compiled version showing the solution is using a.ptr = a.num so therefore a.num = 50+1

    the printf prints 3 51 myotherword


    did a.ptr take the following lines value?
    Last edited by illidari; 12-03-2010 at 01:01 PM.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    You have two printf statements in the code you posted; you seem to be asking about the one in func3, because the output from the printf in main will be different.

    The a in func3 is a local (exists only within func3) copy of the a in main. But the value of its pointer is the same as the value of the pointer in main's a, so it points to main's a.

    So, inside func3, the statement a.word = "myotherword"; changes the string in the local copy of a. But the statement a.ptr->word = "yetanotherword"; changes the string in main's a, not the string in func3's a.

    The if statement condition is false because num is the same in the original and the copy. The else statement: a.num = a.ptr->num + 1; changes only the num that belongs to func3's a, assigning to it the value of num in main's a plus 1.

    edit: By the way, your struct doesn't "declare a struct within itself". It declares a struct pointer.
    Last edited by R.Stiltskin; 12-03-2010 at 11:04 AM.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    11
    I believe I am following what you are saying but I am still confused on how the a.ptr-> word got a value equivalent to the a.num

    I can change a.num to another number and the if statement would continue to fail and it prints a.ptr->num+1.

    That number it is printing is always the value of a.num+1.


    Or I guess a better way to ask is :
    In the main part of the code, what is this line doing :

    a.ptr = &a;


    Much appreciated

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,207
    a.ptr is a pointer to a struct foo. A pointer can contains the address of an object (i.e. a pointer to a struct foo can contain the address of an object of type struct foo).

    The line a.ptr = &a sets a.ptr to contain the address of a. After that (a.ptr)->word is equivalent to (&a)->word which is also equivalent to a.word.

    And this is not a struct declaring (or containing) a struct within itself. A struct is allowed to contain a pointer.

    One analogy is a house containing a bit of paper describing the street address of the house. The street address identifies the house, but is not the house.
    Last edited by grumpy; 12-03-2010 at 02:03 PM.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. general question about how struct works.
    By nullifyed in forum C Programming
    Replies: 14
    Last Post: 06-21-2010, 06:03 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM

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