Reputation: 11
I need help please; this is the sample code:
#include <stdio.h>
const int n = 4;
const int m = 4;
void display(int arr[n][m]){
for(int i=0; i<n; i++) {
for(int j=0;j<m;j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
int *constrast(int arr[n][m])
{
int temp[n][m];
int max = 25;
int min = 10;
int uBound = 255;
int lBound = 0;
for(int i=0; i<n; i++) {
for(int j=0;j<m;j++) {
temp[i][j] = ((arr[i][j] - max) *((int)(uBound-lBound)/(max-min))+uBound;
}
}
return temp;
}
int main()
{
int disp[4][4] = {
{10, 11, 12, 13},
{14, 15, 16, 17},
{18, 19, 20, 21},
{22, 23, 24, 25}
};
printf("Image Before Stretching:\n");
display(disp);
printf("Image After Stretching:\n");
display(constrast(disp));
return 0;
}
This is the error message I get after attempting to compile:
contrast.c: In function ‘display’:
contrast.c:6:4: error: ‘for’ loop initial declarations are only allowed in C99 mode
for(int i=0; i<n; i++) {
^
contrast.c:6:4: note: use option -std=c99 or -std=gnu99 to compile your code
contrast.c:7:7: error: ‘for’ loop initial declarations are only allowed in C99 mode
for(int j=0;j<m;j++) {
^
contrast.c: In function ‘constrast’:
contrast.c:21:4: error: ‘for’ loop initial declarations are only allowed in C99 mode
for(int i=0; i<n; i++) {
^
contrast.c:22:7: error: ‘for’ loop initial declarations are only allowed in C99 mode
for(int j=0;j<m;j++) {
^
contrast.c:23:82: error: expected ‘)’ before ‘;’ token
temp[i][j] = ((arr[i][j] - max) *((int)(uBound-lBound)/(max-min))+uBound;
^
contrast.c:24:7: error: expected ‘;’ before ‘}’ token
}
^
contrast.c:28:4: warning: return from incompatible pointer type [enabled by default]
return temp;
^
contrast.c:28:4: warning: function returns address of local variable [-Wreturn-local-addr]
contrast.c: In function ‘main’:
contrast.c:44:1: warning: passing argument 1 of ‘display’ from incompatible pointer type [enabled by default]
display(constrast(disp));
^
contrast.c:4:6: note: expected ‘int (*)[(sizetype)m]’ but argument is of type ‘int *’
void display(int arr[n][m]){
^
Upvotes: 1
Views: 62
Reputation: 13134
int *constrast(int arr[n][m]) { int temp[n][m]; int max = 25; int min = 10; int uBound = 255; int lBound = 0; for(int i=0; i<n; i++) { for(int j=0;j<m;j++) { temp[i][j] = ((arr[i][j] - max) *((int)(uBound-lBound)/(max-min))+uBound; } } return temp; }
Besides the type of temp
which is int[4][4]
doesn't match the function type which is int *
you shall never return the address of a local variable since such variables are gone when the function ends and a pointer to it won't be valid any longer.
You cannot return an array from a function in C. A workaround would be to either wrap the array in a struct
or to allocate the memory dynamically. Even better: Pass the output array as a parameter.
Upvotes: 1
Reputation: 339
When you are trying to execute row return temp;
, it returns only the pointer to the array and then goes out of the function. But that array was allocated in system stack when your code went in int *constrast(int arr[n][m])
function. On return, it deletes that variable and you are trying to work with a bad pointer.
As a solution, you can pass result array pointer as a parameter too, or use global variables.
Upvotes: 0