Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: Gerard on February 19, 2011

Title: [C] Pointers and Structs
Post by: Gerard on February 19, 2011
Being fairly new with C but having a strong Java/C# background, I'd like to "test" out what pointers are all good for.

I've explained my question via some comments inside my code:


Code: [Select]
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

    // Some start value:
    int speed = 88;
 
    typedef struct {
        int *speed; // Reference to the other speed variable.
    } Motor;
 
    // Create a "new" motor:
    Motor a;
 
    // "Link" the speed variables to each other:
    a.speed = &speed;
 
    // Now I'd like both a.speed and speed to be equal to "100".
    a.speed = 100;
 
    // Simple test, result wanted: 100 100, result achieved: 100 88.
    printf("%i %i\n", speed, a.speed);
 
    system("PAUSE");
    return 0;
}

So, is it even possible to "assign by reference"? I understand why my above example doesn't work - but would like to know if it is possible at all.

- Gerard
Title: Re: [C] Pointers and Structs
Post by: Gerard on February 20, 2011
Happy days, I resolved the issue.

This is the working variant, if someone runs into the same issue:

Code: [Select]
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {


    // Some start value:
    int speed = 88;
 
    typedef struct {
        int* speed; // Reference to the other speed variable.
    } Motor;
 
    // Create a "new" motor:
    Motor a;
 
    // "Link" the speed variables to each other:
    a.speed = &speed;
 
    // Now I'd like both a.speed and speed to be equal to "100".
    *a.speed = 77;
 
    // Simple test, Result wanted: 100 100, result achieved 100 88
    printf("%i %i\n", speed, *a.speed);

    system("PAUSE");
    return 0;
}

Notice how I use the "star symbol" in both the "*a.speed = 70" and the "printf()" statements.
Title: Re: [C] Pointers and Structs
Post by: LittleWhite on February 20, 2011
Well, you found your error  :clap:

But I have to say ... that's not the more useful use of having pointers. I think I am using these on datagram reading (packet of bit) ... or for copying multiple surface (going thourgh the surface with different pixel formats)
Title: Re: [C] Pointers and Structs
Post by: Xetick on February 20, 2011
Still remember when I first tried to wrap my head around them way back. But they do have their place and it's certainly a good thing that you try to learn what they are. You have something like it in C# since classes are references (essentially pointers) while structs are "by value". You can also send in data to functions that changes your original value "ref" and "out" and that's also essentially pointers.

Your problem, as you discovered, is that this code
Code: [Select]
a.speed = 100;
sets the pointer to point to address 100 in memory.

Someone looking at this should know that it is for test only because it is bad do write the code like that. When you come back a few weeks later your going to go insane figuring out why you nice speed variable changes value on its own.