1. ## 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 2. 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. 3. (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; 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. 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. 5. > (g) z = *p; illegal friend says legal here

ILLEGAL. Levels of indirection must be the same.

--Garfield the Great 6. No, (g) is legal, the pointer has been de-referenced. 7. ## 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;
}``` 8. Oh yeah, it's legal. I don't know what I was thinking. 9. > 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. Popular pages Recent additions 