Thread: assignments with structs

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    assignments with structs

    a friend of mine and I are disagreeing on the following statements


    Which are LEGAL?
    Which are ILLGAL? why?

    assume following:

    Code:
    struct node {
       char c;
       struct node * next;
    };
    
    struct node *p, *q, *r;          <----------- pointers
    
    struct node x, y, z;                <-----------non pointers
    (a) p = (struct node *)malloc(sizeof(struct node)); legal
    (b) *q = (struct node *)malloc(sizeof(struct node)); illegal
    (c) x = (struct node *)malloc(sizeof(struct node)); illegal
    (d) p = r; legal
    (e) q = y; illegal
    (f) r = NULL; legal
    (g) z = *p; illegal friend says legal here

    (h) p = *x; illegal
    (i) free(y); illegal
    (j) free(*p); illegal
    (k) free(r); legal
    (l) *q = NULL; illegal
    (m) *p = *x; illegal
    (n) z = NULL; legal friend says illegal here
    Sue B.

    dazed and confused


  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Hi sue
    You sure like structures!!

    I think (g) is illegal also as you need to specifiy which element of node you want to assign the value of p.

    I think (n) is illegal as z is not a pointer therefore cant be NULL. You would need to specify which element.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    (G) would be legal.

    What you are doing is dereferenceing a pointer to a structure, and assigning that contained value to the left hand variable.

    Consider the following:

    struct mystruct x,y,*z;

    z = &y; /* ok, z == y now */

    x = y; /* perfectly valid */

    Thus:

    x = *z; /* also perfectly valid */

    Recall that by dereferencing a pointer, you are saying "give me the value stored at this location".

    (n) is not legal.

    You cannot just initialize a structure instance all in one shot like that. Consider the following:

    struct mystruct { int x; char y[1024]; float z; };

    Given an instance of 'mystruct', called 'abc', you cannot just:

    abc = 1;

    Which is what you would in effect be doing by assigning it NULL.

    However, you can do:

    int x = NULL;

    That is legal.


    Quzah.

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Hang on a minute I don't get that
    You say (g) is legal but what (g) is saying is z = the value stored in p, but what element of p? And to what element of z are we assigning it to.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    > (g) z = *p; illegal friend says legal here

    ILLEGAL. Levels of indirection must be the same.

    --Garfield the Great
    1978 Silver Anniversary Corvette

  6. #6
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    No, (g) is legal, the pointer has been de-referenced.
    zen

  7. #7
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    more with structures and dynamic variables

    you know, I think some of this stuff with structures is starting to sink in the ole noggin. I should say it is much more confusing than learning all the rules and syntax of the printf function call.

    Anyhow, I have more to ask about structures....just so I know I am on the same page as the instructor (he's a transplant from Russia - so I'll never be on the same page, but ..........)

    Instruction: Function is to interchange values in the dynamic variables to which p and q point, so that after the function is performed, *p will have the value formerly in *q and vice versa.
    (a switcherooni so to speak).

    so here is the structure again :

    Code:
    struct node {
       char c;
       struct node * next;
    };
    
    struct node *p, *q, *r;          
    struct node x, y, z;
    and here is my code for the function described:
    explain where I am wrong.
    do i have to allocate memory to *p and / or *q to have this work???
    Code:
    void Fn1(struct node *p, struct node *q)
    {
        struct node temp;
        temp = *p;
        *p = *q;
        *q = temp;
    }
    Sue B.

    dazed and confused


  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Oh yeah, it's legal. I don't know what I was thinking.
    1978 Silver Anniversary Corvette

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Instruction: Function is to interchange values in the dynamic
    > variables to which p and q point, so that after the function is
    > performed, *p will have the value formerly in *q and vice versa.
    > (a switcherooni so to speak).

    ...

    > do i have to allocate memory to *p and / or *q to have this
    > work???

    What are you swapping, values? Or making the pointers point
    at the already allocated nodes, so that p points to q and vice
    versa?

    If the latter:

    void swap( Node *p, Node *q ) { Node *r = p; p = q; q = r; }

    No allocation needed.

    Garfield said:

    Levels of indirection must be the same.
    But they are the same. Dereferencing a pointer is the same level
    of indirection as an actual instance of the object.

    Thus:

    int x, *y;

    y = &x;

    *y = 5; <------ this is the same thing as doing...
    x = 5; <------ this.

    Therefore, the opposite is also true:

    int z;
    z = *y;
    z = x;

    In either case here, z would be 5.

    Quzah.



    Quzah.
    Last edited by quzah; 11-03-2001 at 11:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  2. Replies: 3
    Last Post: 04-06-2007, 05:10 PM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 05:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM