# Thread: Vector os structs not working how it is supposed to...

1. ## Vector os structs not working how it is supposed to...

got to write this little program, that reads some points coordinates and do some stuff with it, can't figure out why it doesn't work for more than two points. here is the code:

Code:
```#include <stdio.h>#include <stdlib.h>
#include <math.h>
#define DISTANCE sqrt((u[a].x-u[b].x)*(u[a].x-u[b].x)+(u[a].y-u[b].y)*(u[a].y-u[b].y)+(u[a].z-u[b].z)*(u[a].z-u[b].z))

typedef struct{
int x, y, z;
} point;

typedef point* dots;

//building the structures

dots build(int* num) {
int size;
dots Temp;

do{
printf("Type number of points: ");
scanf("%d", &size);
if(size<=0) puts("Invalid Number.");
}while (size<=0);

Temp = (dots) malloc (sizeof(dots)*size);
if (Temp == NULL) exit(1);
*num = size;
return Temp;
}

//reading the points

void readpoints(dots u, int size){ //u stands for Universe
int i;

for(i=0; i<size; i++){
system("cls");
printf("Point %d: ",  (i+1));
printf("\nType x coordinate: ");
scanf("%d", &u[i].x);
printf("\nType y coordinate: ");
scanf("%d", &u[i].y);
printf("\nType z coordinate: ");
scanf("%d", &u[i].z);
}
}

//printing the points out

void printp(dots u, int size){
int i;

printf("Typed points:\n");
for(i=0; i<size; i++)
printf("Point %d: {%d, %d, %d}\n", (i+1), u[i].x, u[i].y, u[i].z);
system("PAUSE");

}

//one funcion for both requests, based on choice

void distance(int choice, dots u, int size){
float dis;
if (!choice) {
int a, b;
dis=0;
printf("Type the two points (1 - %d): ", size);
scanf("%d %d", &a, &b);
a--; b--;
dis = DISTANCE;
system("cls");
printf("Distance from %d to %d = %f\n", (a+1), (b+1), dis);
}
else {
int a, b;
for (a=0; a<size; a++){
dis=0;
b=a+1;
dis = DISTANCE;
system("cls");
printf("Distance from %d to %d = %f\n", (a+1), (b+1), dis);
}
}
system("PAUSE");
}

//menu

void menu() {
int choice, num;
dots universe;
printf("First we need some POINTS!\n");
universe = build(&num);
readpoints(universe, num);
do{
system("cls");
printf("Type\n");
printf("1: Retype points.\n");
printf("2: Print typed points.\n");
printf("3: Calculate distance between two points.\n");
printf("4: Calculate distance one by one(1-2, 2-3...)\n");
printf("5: Exit\nChoice: ");
scanf("%d", &choice);
switch (choice){
case 1: universe = build(&num); readpoints(universe, num); break;
case 2: printp(universe, num); break;
case 3:
case 4: distance((choice-3), universe, num); break;
case 5: choice = 0; break;
default: 	printf("Invalid entry");
system("PAUSE");
choice = 1;
break;
}
}while(choice);
}

//simple main

int main(){
menu();
return 0;
}```

2. Code:
`Temp = (dots) malloc (sizeof(dots)*size);`
You're allocating memory based on the size of a pointer to a structure, not the size of the structure itself.

3. First and foremost you have to get rid of the "dots" typedef. It makes your program harder to read by hiding the fact that it's a pointer. Just use point* everywhere you use dots.

Your malloc call in build is wrong. It's allocating space for size POINTERS to point, but you want size point's, like this:
Code:
`	Temp = malloc (sizeof(point) * size);`
Your macro is a little unusual. It'd be more normal, and potentially more useful, to parameterize it like this:
Code:
```#define DISTANCE(u,a,b) sqrt((u[a].x-u[b].x)*(u[a].x-u[b].x)+  \
(u[a].y-u[b].y)*(u[a].y-u[b].y)+  \
(u[a].z-u[b].z)*(u[a].z-u[b].z))```
And you would call it like this: DISTANCE(u,a,b);

4. oh, that makes much more sense than what i was doing...

i will also redefine the macro, thanks!

5. Incidentally, it is usually good practice to parenthesize identifiers used in a function-style macro in case the identifier is replaced with an expression and precedence changes the meaning of the code. The brackets in the array index notation serve the same purpose, but for u:
Code:
```#define DISTANCE(u, a, b) sqrt(((u)[a].x - (u)[b].x) * ((u)[a].x - (u)[b].x) + \
((u)[a].y - (u)[b].y) * ((u)[a].y - (u)[b].y) + \
((u)[a].z - (u)[b].z) * ((u)[a].z - (u)[b].z))```

6. > i will also redefine the macro, thanks!
It would be better to redefine it as a proper function.

The only reason for writing macros is performance optimisation, and you're nowhere near that kind of issue yet. Most modern C compilers support inline functions to begin with.

The first rule of optimisation-club is "Don't do it!"
The second rule of optimisation-club is "Don't do it! (yet)"

Popular pages Recent additions