As you know..., I have been trying to implement the Euclid Algorithm (finds the Greates Common Divisor)in C using functions, structures, and pointers...

(Euclid Algorithm (extended))

Well, I realized that there is no necesity of pointers, so I did it without them.

Here is the code:

ans when running:Code://1206_2_03school.c #include<stdio.h> #include<math.h> typedef struct euclid{ int m; int a; int b; }Euclid; Euclid ext_gcd(int m, int n){ Euclid euclidp, euclidp2; if(n==0) { euclidp.m=m; euclidp.a=1; euclidp.b=0; printf("ext_gcd(%d,%d)=%d=(%d)(%d)+(%d)(%d)\n",m,n,euclidp.m,m,euclidp.a,n,euclidp.b); return euclidp; } euclidp=ext_gcd(n, m%n); euclidp2.m=euclidp.m; euclidp2.a=euclidp.b; euclidp2.b=(euclidp.a)-floor(m/n)*(euclidp.b); printf("ext_gcd(%d,%d)=%d=(%d)(%d)+(%d)(%d)\n",m,n,euclidp2.m,m,euclidp2.a,n,euclidp2.b); return euclidp2; } int main(){ printf("Euclid Algorithm (Extended)\n"); ext_gcd(954,285); return 0; }

but i still have a doubt..Code:i222-151-15-224:~/Desktop nacho$ gcc 1206_2_03school.c i222-151-15-224:~/Desktop nacho$ ./a.out Euclid Algorithm (Extended) ext_gcd(3,0)=3=(3)(1)+(0)(0) ext_gcd(12,3)=3=(12)(0)+(3)(1) ext_gcd(87,12)=3=(87)(1)+(12)(-7) ext_gcd(99,87)=3=(99)(-7)+(87)(8) ext_gcd(285,99)=3=(285)(8)+(99)(-23) ext_gcd(954,285)=3=(954)(-23)+(285)(77) i222-151-15-224:~/Desktop nacho$

when I try to change the code in order to use pointers and try to compile it, there is a bus error.

Here is my code and the debbuger result.

debuggers result:Code:#include<stdio.h> #include<math.h> typedef struct euclid{ int m; int a; int b; }Euclid; Euclid ext_gcd(int m, int n){ Euclid *euclidp, *euclidp2; if(n==0) { euclidp->m=m; euclidp->a=1; euclidp->b=0; printf("ext_gcd(%d,%d)=%d=(%d)(%d)+(%d)(%d)\n",m,n,euclidp->m,m,euclidp->a,n,euclidp->b); return *euclidp; } *euclidp=ext_gcd(n, m%n); euclidp2->m=euclidp->m; euclidp2->a=euclidp->b; euclidp2->b=(euclidp->a)-floor(m/n)*(euclidp->b); printf("ext_gcd(%d,%d)=%d=(%d)(%d)+(%d)(%d)\n",m,n,euclidp2->m,m,euclidp2->a,n,euclidp2->b); return *euclidp2; } int main(){ printf("Euclid Algorithm (Extended)\n"); ext_gcd(954,285); return 0; }

(GNU debugger, and other data about my compiler, machine, etc is below)

Before I was told that my pointers were not initialized.. but... I still dont get it. How can I initialize them? using malloc? if malloc is the solution... can anyone tell me the answer please?Code:i222-151-15-224:~/Desktop nacho$ gcc 1206_2.c i222-151-15-224:~/Desktop nacho$ ./a.out Euclid Algorithm (Extended) Bus error i222-151-15-224:~/Desktop nacho$ gcc 1206_2.c -o 1206_2 i222-151-15-224:~/Desktop nacho$ gdb 1206_2 GNU gdb 6.3.50-20050815 (Apple version gdb-563) (Wed Jul 19 05:10:58 GMT 2006) Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-apple-darwin"...Reading symbols for shared libraries .. done (gdb) run Starting program: /Users/nacho/Desktop/1206_2 Reading symbols for shared libraries . done Euclid Algorithm (Extended) Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_PROTECTION_FAILURE at address: 0x00000000 0x00001d9b in ext_gcd () (gdb) quit The program is running. Exit anyway? (y or n) y

I feel that with this problem I have understood Structures, typesDefinitions..., functions that return a user defined type..., members, etc. but I still have a doubt about pointers...

I would like to get the same result as the first program. But with pointers.

Thanks