Newsgroups: comp.os.msdos.djgpp From: m Subject: Re: C Newbie - Pointers to Structures Problem Content-Type: text/plain; charset=us-ascii Message-ID: <351A78AF.7213E520@removethis.ul.ie> Sender: usenet AT ul DOT ie Content-Transfer-Encoding: 7bit Organization: University of Limerick References: <01bd582b$a4841700$81efd4cf AT gregbl DOT ihug DOT co DOT nz> Mime-Version: 1.0 Date: Thu, 26 Mar 1998 15:47:59 GMT Lines: 97 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk See program below... manuel. Greg Bell wrote: > 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 > #include > #include > > 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 > { // As you have it temp is an uninitialised pointer, so when you// use it (ie. temp->x = a->x) you are writing to a random? // memory location which gives you a segmentation fault. // The way to do it is to make temp a POINT rather than a POINTPTR. // POINT temp; // temp.x = a->x; temp.y = a->y; // ... > 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; > }