Hi Guys, This chapter on pointers and arrays is definitely challenging me. I took advice from this forum and purchased a book on C and am working through the exercises. Basically, my while loop won't finish even though the condition hasn't been met to exit.
First, I get compile warnings which I can't figure out how to resolve:
Code:
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\work\~\chapt-12-ex12.c||In function 'find_two_largest':|
warning: initialization discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]|
warning: assignment to 'int *' from 'int' makes pointer from integer without a cast [-Wint-conversion]|
warning: assignment to 'int *' from 'int' makes pointer from integer without a cast [-Wint-conversion]|
||=== Build finished: 0 error(s), 3 warning(s) (0 minute(s), 1 second(s)) ===|
Here's the code:
Code:
#include <stdio.h>
//C Programming - a modern approach, 2nd edition by K.N. King
// IMPORTANT: must use pointer arithmetic, not subscription
void find_two_largest(const int *a, int n, int *largest, int *second_largest);
int main(void){
int b[] = {5,4,3,2,1,8,6,9,7};
int num = (sizeof(b)/sizeof(b[0]));
int val1=999, val2=999;
int *lgst=&val1, *slgst=&val2;
find_two_largest(b, num, lgst, slgst);
printf("in main: largest = %d and 2nd largest = %d\n",*lgst, *slgst);
}
void find_two_largest(const int *a, int n, int *largest, int *second_largest){
*largest = a[0];
int *last = &a[n-1];
//printf("last = %d\n",last); // last prints mem location. *last prints value (7)
*second_largest = a[0];
//while(a<&a[n]){ // can't use as both a values increase due to a++
while(a<=last){ // compares memory location
printf("1st in while: a = %d and last = %d\n",a, last); //prints memory locations
printf("before if *largest = %d and *a = %d\n",*largest, *a);
if(*largest<*a){ // compares actual values
printf("if success!\n");
second_largest=*largest;
largest=*a;
printf("in if: largest = %d and second_largest = %d\n",largest, second_largest);
a++; // this is working as expected 11/25 @ 6AM
}else{
printf("if failed!\n");
a++;
}
}
return;
}
Here's what I see on the CLI:
<<< I CAN'T SEEM TO ADD AN IMAGE>>>
Code:
1st in while: a = 6421984 and last = 6422016
before if *largest = 5 and *a = 5
if failed!
1st in while: a = 6421988 and last = 6422016
before if *largest = 5 and *a = 4
if failed!
1st in while: a = 6421992 and last = 6422016
before if *largest = 5 and *a = 3
if failed!
1st in while: a = 6421996 and last = 6422016
before if *largest = 5 and *a = 2
if failed!
1st in while: a = 6422000 and last = 6422016
before if *largest = 5 and *a = 1
if failed!
1st in while: a = 6422004 and last = 6422016
before if *largest = 5 and *a = 8
if success!
in if: largest = 8 and second_largest = 5
1st in while: a = 6422008 and last = 6422016
Process returned -1073741819 (0xC0000005) execution time : 2.794 s
Press any key to continue.
Notice on the last line of out put before "Process returned" the line is:
Code:
1st in while: a = 6422008 and last = 6422016.
6422008 is below 6422016 which means it should have kept looping instead of exiting.
The above is why I can't just 'printf' my way to figuring out what I did wrong. Thank you for your insight.