1

I'm confused on how to use pointer notation to access a struct used with malloc. I can use array notation but what's the syntax on pointer?

  #include "stdio.h"
#include "stdlib.h"

using namespace std;

typedef struct flightType  {

    int altitude;
    int longitude;
    int latitude;
    int heading;

    double airSpeed;

} Flight;

int main() {

    int airbornePlanes;
    Flight *planes;

    printf("How many planes are int he air?");
    scanf("%d", &airbornePlanes);

    planes =  (Flight *) malloc ( airbornePlanes * sizeof(Flight));
    planes[0].altitude = 7;  // plane 0 works

    // how do i accessw ith pointer notation
    //*(planes + 1)->altitude = 8; // not working
    *(planes + 5) -> altitude = 9;
    free(planes);

}
runners3431
  • 1,425
  • 1
  • 13
  • 30
  • I'm receving error - main.cpp|34|error: request for member 'altitude' in '(planes + 120u)', which is of non-class type 'Flight*'| ||=== Build finished: 1 errors, 0 warnings ===| – runners3431 Oct 23 '12 at 16:57
  • 1
    If this is a c code, please use stdio instead of iostream and namespace. Those are of C++. – halfelf Oct 23 '12 at 16:57

3 Answers3

2

Basically x->y is shorthand for (*x).y.

The following are equivalent:

(planes + 5) -> altitude = 9

and

(*(planes + 5)).altitude = 9;

and

planes[5].altitude = 9;

A more concrete example:

Flight my_flight; // Stack-allocated
my_flight.altitude = 9;

Flight* my_flight = (Flight*) malloc(sizeof(Flight)); // Heap-allocated
my_flight->altitude = 9;
johv
  • 4,424
  • 3
  • 26
  • 42
  • When do i use -> or periods? – runners3431 Oct 23 '12 at 16:55
  • When i use *(planes +5) - i'm getting - |error: invalid type argument of 'unary *'| ||=== Build finished: 1 errors, 0 warnings ===| – runners3431 Oct 23 '12 at 16:58
  • I'm sorry, I missed a pair of parentheses. If you think that's ugly, then you know why you got the -> syntax ;) – johv Oct 23 '12 at 17:07
  • You cannot add 5 to the current pointer address in `planes` to arrive at the 5th element. That adds 5 to the address of in planes, which is not the correct offest to reach a class that holds 4 ints each. You'd have to add `5 * 4 * size of(ints)` to planes to get to the 5th element. That's why you need to derefence planes first, then use the array to then get or set the fields in the 5th element. The compiler then does all the math for us. – StarPilot Oct 23 '12 at 17:13
  • 1
    @StarPilot Ooh, this is not correct! You might like to read here: http://stackoverflow.com/q/394767/694576 – alk Oct 23 '12 at 17:18
  • 1
    @StarPilot Pointer arithmetics would take the size of a Flight struct into account. – johv Oct 23 '12 at 17:21
  • I must be having a senior moment then. Pointer arithmetic information noted. – StarPilot Oct 23 '12 at 17:24
1

You don't need the -> notation for this, because you are already dereferencing the pointer using the asterisk. Simply do:

*(places + 5).altitude = 5;

The -> is shorthand for "dereference this struct pointer and access that field", or:

(*myStructPointer).field = value;

is the same as

myStructPointer->field = value;

You can use either notation, but (should) not both at the same time.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
0

You need an extra parenthesis after dereferencing the pointer.

(*(planes + 5)).altitude = 9;
shinkou
  • 5,138
  • 1
  • 22
  • 32