Hi!
I am trying to make a function, which transposes matrix. When declaring it, i get this error, which i mention in the title. Ive tried to find the solution myself, but i am quite new to C, so i dont even understand what does this error mean. (I know what is pointer, but dont know how to fix the error.) please could anyone explain what causes this error? And please have a look at my code, i am afraid that this wont be the only error. So if someone will find any obvious errors, please let me know.
Thanks.

#include <stdio.h>
#include <stdlib.h>

#define MAX_ROW      50
#define MAX_COL      50

int transp(int i, int j, int m, int n, int temp, int mat[i][j]);

int main(void) 
{

int i, j, m, n, temp;
int mat[MAX_ROW][MAX_COL];

    // variable dim  is set to smaller value of defined
    // maximal number of rows and columns


    int dim = (MAX_ROW < MAX_COL)? MAX_ROW : MAX_COL;


    // input of matrix dimension

    do {

        printf("input nuber of rows < %d   :", dim);
        scanf("%d", &m);
        printf("input number of cols < %d:", dim);
        scanf("%d", &n);

    }   while (m < 1 || m > dim || n < 1 || n > dim);


    // input of elements

    printf("\ninput of elements:\n");
   for (i = 0; i < m; i++) {
      for (j = 0; j < n; j++) {

         printf("input element [%d][%d] : ", i, j);
           scanf("%d", &mat[i][j]);

        }
    }  


    // printing matrix before transposing

    printf("\n\matrix before transposing:\n");
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {

         printf("%3d", mat[i][j]);

      }
        printf("\n");
    }


    // print after transposing
    // number of rows becomes number of columns ...

    printf("\nMatrix after transposing:\n");
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {

              [ICODE]printf("%3d", transp(i, j, m, n, temp, mat[i][j])); [/ICODE]// i get the error on this line
        }
        printf("\n");
    }
} //end of main

/************function transp****************//////

int transp (int i, int j, int m, int n, int temp, int mat[i][j])

   {
for ( i=0; i<m; ++i ) 

    {
        for ( j=i+1; j<n; ++j )  
        {
               temp = mat[i][j];
                mat[i][j] = mat[j][i];
                mat[j][i] = temp;
                return (mat[j][i]);
        }
    }

Recommended Answers

All 14 Replies

> int transp(int i, int j, int m, int n, int temp, int mat[j]);
C doesn't have variable sized arrays, so this should be int transp(int i, int j, int m, int n, int temp, int mat[MAX_ROW][MAX_COL]); Ditto for the implementation as well.

You might only use the top-left quarter of the array, but that doesn't matter to this function.

> printf("%3d", transp(i, j, m, n, temp, mat[j]));
Would be printf("%3d", transp(i, j, m, n, temp, mat) ); > return (mat[j]);
You're always going to return immediately here.
Perhaps you need to transpose the whole matrix, then print each cell?

Well, thanks for your reply. I did what you suggested, and ran the code. But transposing does not work at all. It prints array cointaining only elements [1][2] and [2][1]. Why?

Did you do all that I said?
Like remove the return from the middle of transpose?

Post your new code.

Post your new code.

well this is my new code. Now all it does is print array od "m" varibiles...(number of rows)

#include <stdio.h>
#include <stdlib.h>

#define MAX_ROW      50
#define MAX_COL      50

int transp(int i, int j, int m, int n, int temp, int mat[i][j]);

int main(void) 
{

int i, j, m, n, temp;
int mat[MAX_ROW][MAX_COL];

    // variable dim  is set to smaller value of defined
    // maximal number of rows and columns


    int dim = (MAX_ROW < MAX_COL)? MAX_ROW : MAX_COL;


    // input of matrix dimension

    do {

        printf("input nuber of rows < %d   :", dim);
        scanf("%d", &m);
        printf("input number of cols < %d:", dim);
        scanf("%d", &n);

    }   while (m < 1 || m > dim || n < 1 || n > dim);


    // input of elements

    printf("\ninput of elements:\n");
   for (i = 0; i < m; i++) {
      for (j = 0; j < n; j++) {

         printf("input element [%d][%d] : ", i, j);
           scanf("%d", &mat[i][j]);

        }
    }  


    // printing matrix before transposing

    printf("\n\matrix before transposing:\n");
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {

         printf("%3d", mat[i][j]);

      }
        printf("\n");
    }


    // print after transposing
    // number of rows becomes number of columns ...

    printf("\nMatrix after transposing:\n");
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {

             printf("%3d", transp(i, j, m, n, temp, mat) );  
        }
        printf("\n");
        getch();
    }
} // koniec main

/************************function transp********************//////


int transp(int i, int j, int m, int n, int temp, int mat[MAX_ROW][MAX_COL])
   {
for ( i=0; i<m; ++i ) 

    {
        for ( j=i+1; j<n; ++j )  
        {
               temp = mat[i][j];
                mat[i][j] = mat[j][i];
                mat[j][i] = temp;
           
        }
    }
}

> printf("%3d", transp(i, j, m, n, temp, mat) );
call tansp() before your last pair of loops to print the matrix.

Then just print mat[j] in the loop.

But you might have to rethink your transp() function, and all those parameters you're passing.

Thank you very much, now it works perfectly.

>But you might have to rethink your transp() function, and all those parameters you're passing.

Why do you think i should rethink whole function? Could you be more specific?

>Why do you think i should rethink whole function? Could you be more specific?
You are passing copies of arguments that don't need to be passed as parameters?
Indexes like i and j don't need to be part of arguments in a function; they can be local declared to the function. That applies to variable temp as well.
In general, if your function has more than four parameters, perhaps you need to rethink what it is been done in it.

>In general, if your function has more than four parameters, perhaps you need to rethink what it is been done in it.

okay, i did what i`ve been advised, though i dont know whats the point of it . My code worked perfectly before this change.
Does it cause any problems, when function has some unnecessary parameters? Please explain...

my function now looks like this:

int transp(int m, int n, int mat[MAX_ROW][MAX_COL])
{
 int i;
 int j;
 int temp;
    for ( i=0; i<m; ++i ) 
    {
        for ( j=i+1; j<n; ++j )  
        {
               temp = mat[i][j];
                mat[i][j] = mat[j][i];
                mat[j][i] = temp;
           
        }
    }
}

And i would like to ask another "question" ...

My code (posted above several times), HAS TO contain pointers...
I did some research about pointers, and i think i quite understood what they are, and basic stuff like that, but i haven`t figured out how to use them in code to make something more effecitive or make the code shorter.....

So I`d like know, where could i effectively use pointers in my code.
Thank you for your suggesstions.

>My code worked perfectly before this change.
>Does it cause any problems, when function has some unnecessary parameters? Please explain...
"Worked perfectly" it is a matter of perception.
It did what I expected at this time would be a more accurate to say.

There's always cost when passing arguments in a function. Nevertheless, functions makes code more manageable, readable and maintainable. Passing many unnecessary arguments in a function defeat those purposes.

>My code (posted above several times), HAS TO contain pointers... int transp(int m, int n, int mat[MAX_ROW][MAX_COL]) I will give you a couple of pointers (no pun intended)
You see int m, int n, int mat? Those are passed to the function as copies of the actual variables. Which means, after the function has completed its work, those values will disappear, as well as the making of that copies have taken some resources.
Wouldn't be great if instead of copies, it could be just a reference to the actual data? No more use of memory that the necessary, and in case that it is required the variable pointer passed, could be changed and retain a persisted value.

>Wouldn't be great if instead of copies, it could be just a reference to the actual data?
Thanks for reply.

As far as i understood your post, i could do something like:

int transp(int *ptr_m, int *ptr_n, int *ptr_mat[MAX_ROW][MAX_COL])
{
*ptr_m=&m;
*ptr_n=&n;
*ptr_mat[MAX_ROW]
[MAX_COL])=&mat[MAX_ROW]
[MAX_COL])
 int i;
 int j;
.
.
.
}

instead my actual declaration of transp() function, for purpose of memory save.

Well, this sounds really great. I`ve already tried it. But compiler gave me an error (conflicting types for transp). I suppose pointer cannot be used as an argument.
Where in the function exactly should i "declare" the pointers?
When i declare them inside function, (like this):

int transp(int m, int n, int mat[MAX_ROW][MAX_COL])
{
int *ptr_m=&m;
int *ptr_n=&n;
.
.

Then I think they are uselles, because same memory space is needed. (function has same arguments passed as it had without pointers).

So where exactly should i declare them?

Thanks

snipped

snipped

After some thoughts, I am inclined to believe it was a proper action.
E-mail to the publisher:

Trying to help in a C programming forum I searched for a suitable example that would show some light on the matter.
I came across this page <snipped for obvious reason> which I linked as a good explanation of it, without much thought of it.
Days later the link was deleted by the administrator. Reason: legality of link questioned . Which may me ponder of that matter and now I am prompted to write you, asking you if that site has the authorization to post that book in its entirety. No acknowledgment is mentioned in the site.

One thing I observed in your code, you are calling transp(i, j, m, n, temp, mat) many times in the main() function. If the value of m and n are 50(let me consider) so that function will be called 50 * 50 = 2500 times and will be printed these many times. Do u really need those nested for loops in the main() to call this function?

--
Patel.

printf("\nMatrix after transposing:\n");
transp(m, n, mat); // function call
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
               printf("%3d", mat[i][j]);  
        }
        printf("\n");
    }
} //end of main

/************function transp****************//////

int transp (int m, int n, int mat[][])
{
      int i, j, temp;

      for ( i = 0; i < m; i++ ) 
     {
        for ( j = 0; j < n; j++ )  
        {
                temp = mat[i][j];
                mat[i][j] = mat[j][i];
                mat[j][i] = temp;
        }
    }
}

Try out this, and return statement is not required because array is used as a pointer.
Better u analyse your code, u will get idea I think.

--
Patel

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.