Author Topic: [C] Pointers and Structs  (Read 3251 times)

0 Members and 1 Guest are viewing this topic.

Offline Gerard

  • ZX 81
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
[C] Pointers and Structs
« 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

Offline Gerard

  • ZX 81
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
Re: [C] Pointers and Structs
« Reply #1 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.

Offline LittleWhite

  • Senior Member
  • Amiga 1200
  • ********
  • Posts: 418
  • Karma: 31
  • It's me!
    • View Profile
Re: [C] Pointers and Structs
« Reply #2 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)
The demoscene will never die, never!

Offline Xetick

  • Atari ST
  • ***
  • Posts: 132
  • Karma: 80
    • View Profile
    • Plane9
Re: [C] Pointers and Structs
« Reply #3 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.
Plane9 - Home of the Plane9 3d screensaver/music visualizer
Challenge Trophies Won: