www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/25/15:46:02

From: "Greg Bell" <gregbl AT ihug DOT co DOT nz>
Newsgroups: comp.os.msdos.djgpp
Subject: C Newbie - Pointers to Structures Problem
Date: 25 Mar 1998 20:25:35 GMT
Organization: Part Time Productions
Lines: 82
Message-ID: <01bd582b$a4841700$81efd4cf@gregbl.ihug.co.nz>
NNTP-Posting-Host: p1-max15.auck.ihug.co.nz
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hi,

I'm a newbie to this newsgroup and have just started programming in C/C++.
At the moment I am learning how to use pointers. I want to write a small
test application which draws a line from point 1 to point 2. Now I have the
algorithm for doing that worked out but I am using a small structure to
hold a new type which consists of two integers x and y (the coordinates of
the point). I have written a swap routine which uses pointers to allow me
to swap two sets of points over. The problem is that although the program
(included below) works it exits from TC3 with a Null Pointer Assignment
error and from DJGPP with a rather cryptic SIGSEV error which includes
register values and other such stuff. I use TC3 at the technical institute
that I attend and DJGPP at home (they are both quite similar esp. when
using RHIDE). Anyway if anyone can enlighten me it would be much
appreciated.

Thanks in advance

Greg Bell

Program Follows:

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

typedef struct co_ordinates
{
    int x, y;
}
POINT;

typedef struct co_ordinates *POINTPTR;

POINT a,b,c,d;
POINT point_difference();
void swap_points();

void main(void)
{
   clrscr();
   a.x = 1;
   a.y = 1;
   b.x = 5;
   b.y = 5;
   c = point_difference(a,b);
   printf("point a is at %d, %d\n",a.x,a.y);
   printf("point b is at %d, %d\n",b.x,b.y);
   printf("the difference between point a and point b is %d,
%d\n",c.x,c.y);
   printf("Now I will swap the points\n");
   swap_points (&a,&b); //Send the addresses of points a and b to
swap_points
   printf("point a is now at %d, %d\n",a.x,a.y);
   printf("point b is now at %d, %d\n",b.x,b.y);
   c = point_difference(a,b);
   printf("the difference between point a and point b is now %d,
%d\n",c.x,c.y);

}

POINT point_difference(POINT a, POINT b)
{
   POINT c;
	c.x = a.x - b.x;
   c.y = a.y - b.y;
   return(c);
}

void swap_points(POINTPTR a, POINTPTR b) //Assign pointer types to a & b
{
   POINTPTR temp; //Create a temporary address holder
   temp->x = a->x; //Shift point a to temp
   temp->y = a->y;
   a->x = b->x; //Shift point b to a
   a->y = b->y;
   b->x = temp->x; //Shift temp to point b
   b->y = temp->y;
}



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019