1

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]){
      ^
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Possible duplicate of [Returning an array using C](https://stackoverflow.com/questions/11656532/returning-an-array-using-c) – Eugene Sh. Sep 24 '18 at 19:56
  • As the error messages suggest you should compile the code as C99 if you want to use features introduced in C99 by using the compiler switch `-std=c99`. – Swordfish Sep 24 '18 at 20:01

2 Answers2

1
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.

Swordfish
  • 12,971
  • 3
  • 21
  • 43
0

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.

AJIOB
  • 329
  • 5
  • 12
  • `temp` decays to a pointer to an _array_ (as you indicate in your answer), but the function returns a pointer to `int`, leading to undefined behavior since `int *` and `int (*)[m]` are not compatible types. A local array is not "deleted" when it goes out of scope, but its lifetime has ended and further attempts to access it will lead to undefined behavior (yet the object may still exist in memory). Also, note that stacks are implementation details, not part of the C language. – ad absurdum Sep 24 '18 at 21:01