Hi,
I'm learning about pointers from a c programming book, and I tried to do some of the exercise problems.
In the book, every 'program' is written as one function with a main. But I'm trying to combine multiple 'programs' as functions, then call them in main. Trouble is, a lot of these functions take pointers as args, so I'm not sure how to call them in main with actual values.
I put these functions inside of main [to try to get around the problem] but it cannot compile. I'm getting 'need ; before {' errors in main, but ofc I checked and don't need any ;'s before {'s.
My code:
Code:
int division(int numerator, int denominator, int *dividend, int *remainder) {
printf("address stored in dividend: %u\n",dividend);
printf("address stored in remainder: %u\n",remainder);
if (denominator == 0)
return(0);
*dividend=numerator/denominator;
*remainder=numerator%denominator;
return(1);
}
main() {
functioncall1();
functioncall2();
functioncall3();
functioncall4() {
int x, y, d, r;
x=9;
y=2;
printf("address of d: %u\n",&d);
printf("address of r: %u\n",&r);
division(x, y, &d, &r);
printf("%d/%d = %d with %d remainder\n",x,y,d,r);
printf("x=%d\n",x);
}
functioncall5() {
double array[10];
double *d_ptr;
double value;
int i,offset;
for (i=0; i<10; i++)
array[i]=(double)i+10.0; /* access memory using array index and fill cells with #s*/
d_ptr=&(array[0]); /* access memory location of first array cell using pointer*/
while (1) {
printf("Address(hex)\tAddress(base10)\tValue\n");
for (i=0; i<10; i++)
printf("%p\t%u\t%lf\n",&(array[i]),&(array[i]),array[i]);
printf("Enter offset value (0 0 to quit): ");
scanf("%d %lf",&offset,&value);
if (offset == 0 && value == 0.0)
break; /* break out of loop */
if (offset < 0 || offset > 9) {
printf("Offset out of bounds\n");
continue; /* go back to start of loop */
}
array[offset]=value; /* using array syntax */
}
return (0);
}
}