Writing a program to find the address of the largest element in an array with 10 integers my code is:
int* Largest(int *array, int size);
int main()
{
int *Ptr, array[10];
int r, c, num = 1;
for(r = 0; r < 10; r++) {
array[r] = num + 1;
}
Ptr = Largest(array, 10);
printf("%p", Ptr);
return 0;
}
int* Largest(int *array, int size)
{
int *largest, r;
for(r = 0; r < size; r++) {
if(r = 0) {
largest = &array[0];
}
else {
if(array[r] > *largest) {
largest = &array[r];
}
}
}
return largest;
}
I dont get any errors or warning when compiling however the program does not do anything and gets stopped automatically by windows.
You probably didn't include headers you need at least stdio.h
You have an assignment which very likely shouldn't be
if(r = 0)
I think this should be
if (r == 0)
I have seen that some people prevent this kind of problem by doing
if (0 == r)
since this way the program will fail to compile if you use the =
operator, and anyway this is not a very good way of doing this, instead you should
larget = &array[0]; for (r = 1 ; r < size ; ++r)
and this will be more efficient obviously.
As for the program stopping, you can try to run the cmd.exe
window and execute your program directly from there.
Note : if (condition1) {} else if (condition2) {}
is valid in c, no need for if (condition1) {} else {if (condition2) {}}
.
when the control back from largest() then stack frame of is now clear in memory. then system looking for ptr containing address but it cannot find anything, so system terminate this program
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.