# Thread: continue with structures and such

1. ## continue with structures and such

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;
}```

2. Hi.

The function you described does indeed do that.

Here's a tester for it:

Code:
```
typedef struct node
{
char c;
int n;
struct node *next;
};

void flip(struct node *a, struct node *b);

int main()
{

node *p, *q;
node x, y;

x.n = 1;
y.n = 2;

p = &x;
q = &y;

printf("x = %i\ny = %i\n", x.n, y.n);
printf("p = %i\nq = %i\n", p->n, q->n);

getch();

flip(p, q);

printf("x = %i\ny = %i\n", x.n, y.n);
printf("p = %i\nq = %i\n", p->n, q->n);

getch();

return 0;
}

void flip(struct node *a, struct node *b)
{

struct node temp;
temp = *a;
*a = *b;
*b = temp;

}```
<< end of code marker fixed by Salem >>

I hope that helps

This linked list stuff is something I really need to get a grip on too. I've been putting it off for the most part...

3. hi Sebastiani
you wrote

void flip(struct node *a, struct node *b)
{

struct node temp;
temp = *a;
*a = *b;
*b = temp;

}

dont you think the better way would
be to use a temp pointer and switch the pointers around rather than copying the data

you know the struct in question was quite small but we could easily have to do this for a larger struct and if it is repeated a lot like in a sort or something the pointers switch should work faster

so i would

void flip(struct node *a, struct node *b)
{

struct node *temp;
temp = a;
a = b;
b = temp;

}

4. That's right, you just swapped the pointers, but the question was concerning changing the values the pointers pointed to. The code I posted changes the values, but does not make the first pointer point to the second ones data, and vice versa.

Perhaps mine could be called "swap_values()" and the one you posted "swap_pointers()".

5. ## BIG THANK-U

thanks Sebastiani, your program helped explain a lot !!

6. This is getting kinda extra-cirricular, but here goes... this is the code I'm looking at
Code:
```void flip(struct node *a, struct node *b)
{
struct node *temp;
temp = a;
a = b;
b = temp;
}```
This code will not work. Here's how the call would go...

1. Pass the function two memory addresses
2. The function makes copies of these addresses, and calls them a and b.
3. The function changes the values of these copies (not to be confused with changing the values of the memory pointed at these copies)
4. The function ends.

So all you've done is changed copies of the node *s. Still, the right idea, but the function would have to be passed node **s and the body of the function would have to change correspondingly (not to mention, the call to the function).