Thread: continue with structures and such

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

    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;
    }
    Sue B.

    dazed and confused


  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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...
    Last edited by Sebastiani; 11-03-2001 at 11:36 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User jasrajva's Avatar
    Join Date
    Oct 2001
    Posts
    99
    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;


    }
    jv

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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()".
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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

    BIG THANK-U

    thanks Sebastiani, your program helped explain a lot !!
    Sue B.

    dazed and confused


  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    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).
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed