How to call more than 1 pointer in a c function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aljon
    New Member
    • Apr 2013
    • 2

    How to call more than 1 pointer in a c function?

    WELL, I'm pretty new to C, my program needed to have a function that will transpose or multiply matrices.. since a matrix (or array) of m x n, has a transposed matrix of n x m .. i would need to call 2 arrays/pointers into the Transpose function.. 1 with the original values and the other with the n by m size the array/pointer that i will put something from the original .. So how can the function know which function should be this or this ..

    for example,where A[i][j] is the orig. function

    Code:
    for(i=0; i<column; i++)
    {
      for(j=0; j<row; j++)
    {
    
      B[i][j]=A[j][i];
    }
    }
    Last edited by Rabbit; Apr 3 '13, 03:53 PM. Reason: Please use code tags when posting code.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The function argument names are used by the function. Just make sure your arguments are in the same order as the calling values.

    Comment

    • aljon
      New Member
      • Apr 2013
      • 2

      #3
      So you mean if i write for example Function(A,B,ro w,col) to call it.. then it would simply mean that the function would know that since A comes first it would be the original function as it is stated inside the function?

      Should i allocate a 2d pointer so that i may call the arrays/pointers into the function? The run error says that," assignmeent of read-only location" what's that supposed to mean? thanks a lot.

      Comment

      • vijay6
        New Member
        • Mar 2010
        • 158

        #4
        Hey aljon, for Transpose of a matrix you can use this code.

        Code:
        ...

        Note: 'm not tested this code. If you have any error while executing this code, post the error message here.
        Last edited by weaknessforcats; Apr 4 '13, 04:28 AM. Reason: removed untested code solution

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Most likely your error is an attempt to use a memory location that does not belong to your program. Often this is caused by using an uninitiaized pointer.

          I would allocate the two arrays and then call the function with the two pointers plus the rows and columns.

          From inside the function it's not obvious that the function should allocate the second array. If it does allocate the second array and return the address to the calling function then it is not obvious to the calling function that the second array needs to be freed.

          The general rule is "he who allocates is he who frees".

          Comment

          • vijay6
            New Member
            • Mar 2010
            • 158

            #6
            Hey weaknessforcats , I don't have 'C' compiler, that's why i posted the code without testing. But 'm sure that code won't give/show errors.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              @Vijay6
              You should not be trying to spoonfeed people anyway. Better point them in the right direction by explaining where their problem is.

              Comment

              • vijay6
                New Member
                • Mar 2010
                • 158

                #8
                Hey @r035198x, i gave the answer because he said "I'm pretty new to C".

                Comment

                Working...