Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Gerard

Pages: [1]
1
C / C++ /C# / Re: [C] Pointers and Structs
« 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.

2
C / C++ /C# / [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

Pages: [1]