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

  1. #1
    Registered User
    Join Date
    May 2012

    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:

    #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;
    		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++){
    		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);
    //one funcion for both requests, based on choice
    void distance(int choice, dots u, int size){
    	float dis;
    	if (!choice) {
    		int a, b;
    		printf("Type the two points (1 - %d): ", size);
    		scanf("%d %d", &a, &b);
    		a--; b--;
    		dis = DISTANCE;
    		printf("Distance from %d to %d = %f\n", (a+1), (b+1), dis);
    	else {
    		int a, b;
    		for (a=0; a<size; a++){
    			dis = DISTANCE;
    			printf("Distance from %d to %d = %f\n", (a+1), (b+1), dis);
    void menu() {
    	int choice, num;
    	dots universe;
    	printf("First we need some POINTS!\n");
    	universe = build(&num);
    	readpoints(universe, num);
    		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");
    						choice = 1;
    //simple main
    int main(){
    	return 0;

  2. #2
    Registered User
    Join Date
    Jun 2011
    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. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    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:
    	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:
    #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)+  \
    And you would call it like this: DISTANCE(u,a,b);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    May 2012
    oh, that makes much more sense than what i was doing...

    i will also redefine the macro, thanks!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    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:
    #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))
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > 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)"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A vector of structs problem
    By Swordsalot in forum C++ Programming
    Replies: 4
    Last Post: 01-05-2011, 02:46 AM
  2. Sorting a vector of structs....
    By ropitch in forum C++ Programming
    Replies: 7
    Last Post: 07-10-2009, 11:49 PM
  3. Sorting Vector of structs.
    By Zosden in forum C++ Programming
    Replies: 41
    Last Post: 10-22-2008, 12:28 AM
  4. Sorting Vector of Structs in STL
    By creativeinspira in forum C++ Programming
    Replies: 5
    Last Post: 08-07-2007, 01:54 AM
  5. Vector of Structs Question
    By gamer4life687 in forum C++ Programming
    Replies: 6
    Last Post: 09-09-2005, 10:13 PM