• Welcome to Dark Bit Factory & Gravity.
 

[C] Pointers and Structs

Started by Gerard, February 19, 2011

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

Gerard

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:


#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

Gerard

Happy days, I resolved the issue.

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

#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.

LittleWhite

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)
The demoscene will never die, never!

Xetick

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

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.
Plane9 - Home of the Plane9 3d screensaver/music visualizer
Challenge Trophies Won: