Pointer Problems

Hey all,

Trying to convert some structs from being accessed by value to accessed by pointer, and I'm being told:
[Error] request for member 'point' in something not a structure or a union

Do I need to reference the variable 'point[]' as a pointer as well?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* 
 * File:    Distance between multiple points
 * Project: Personal
 * Author:  Christopher Bowers
 * Version: Version 1.0 | Basic frame made
 *                  2.0 | Changing to pointers to pass # of points
 */

#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCESS and rand()
#include <math.h> // for sqrt()
typedef struct {
    double x;
    double y;
} Point;

#define NUM_POINTS 4
typedef struct {
    Point point[NUM_POINTS];
} Coord;

// How to exit on '\n' ?
// Use of if statements on failed condition?
int getPoints(Coord p1) {
    int i = 0;
    printf("Enter x value (1) >> ");
    for (i = 0; i < NUM_POINTS && scanf("%lf", (&p1).point[i].x); i++) {
        printf("Enter y value (%d) >> ", i + 1);
        scanf("%lf", (&p1).point[i].y);
        if(i < 3)
            printf("Enter x value (%d) >> ", i + 2);
    }
    return i;
}

double distCalc(Coord p1, const int numPoints) {
    int i = 0;
    double dist = 0;
    double diff1 = 0;
    double diff2 = 0;
    for (i = 0; i < numPoints; i += 2) {
        diff1 = (&p1).point[i].x - (&p1).point[i+1].x;
        diff2 = (&p1).point[i].y - (&p1).point[i+1].y;
        dist += sqrt(diff1 + diff2);
    }
    return dist;
}

int main(void) {
    int i = 0;
    int pointsInput = 0;
    double distSum = 0;
    Coord p;
    puts("Distance calculator V.1");
    pointsInput = getPoints(p);
    distSum = distCalc(p, pointsInput);
    printf("Total distance is: %.4f", distSum);
    return EXIT_SUCCESS;
}
I believe that you try to achieve something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
typedef struct {
    double x;
    double y;
} Point;

#define NUM_POINTS 4
typedef struct {
    Point point[NUM_POINTS];
} Coord;

int getPoints(Coord* p1) {
    int i = 0;
    for ( ; i < NUM_POINTS &&
            2 == scanf(" %lf %lf", &(p1->point[i].x), &(p1->point[i].y)); ++i )
    {
    }
    return i;
}

int main(void) {
    int pointsInput = 0;
    double distSum = 0;
    Coord p;
    puts("Distance calculator V.1");
    pointsInput = getPoints(&p);
    //distSum = distCalc(&p, pointsInput);
    printf("Total distance is: %.4f", distSum);
    return EXIT_SUCCESS;
}
Topic archived. No new replies allowed.