How to use pointers in this example?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Winton
    New Member
    • Jan 2011
    • 22

    How to use pointers in this example?

    Hi im not sure to how to use a pointer for this script, i am trying to call the value from the previous from the previous function and use it in the main function as shown;

    Code:
    void prioriestimate ()
    {
    int T = 1;	
     int i = 0;
     int j = 0;
     int k = 0;
    double A[6][6] =    {{1, 0, 0, T, 0, 0},
    		     {0, 1, 0, 0, T, 0},
    		     {0, 0, 1, 0, 0, T},
    		     {0, 0, 0, 1, 0, 0},
    		     {0, 0, 0, 0, 1, 0},
    		     {0, 0, 0, 0, 0, 1}};
        
    double A_t[6][6] =  {{1, 0, 0, 0, 0, 0},
    		     {0, 1, 0, 0, 0, 0},
    		     {0, 0, 1, 0, 0, 0},
    		     {T, 0, 0, 1, 0, 0},
    		     {0, T, 0, 0, 1, 0},
    		     {0, 0, T, 0, 1, 0}};
     
    double P_k[6][6] =  {{1, 0, 0, 0, 0, 0},
    		     {0, 1, 0, 0, 0, 0},
    		     {0, 0, 1, 0, 0, 0},
    		     {0, 0, 0, 1, 0, 0},
    		     {0, 0, 0, 0, 1, 0},
    		     {0, 0, 0, 0, 1, 0}}; 
    
    {
    
     double a[6][6];
     double a_p[6][6]; 
           for(i = 0; i < 2; i++) 
               for( j = 0; j < 4; j++)
                   for( k = 0; k < 3; k++)  
                       a[i][j] +=  A[i][k] * A_t[k][j];
    
    
    
    	for(i = 0; i < 2; i++) 
               for( j = 0; j < 4; j++)
                   for( k = 0; k < 3; k++)  
                       a_p[i][j] +=  a[i][k] * P_k[k][j];
    
    }
    }
    int main(double Q[6][6], int i, int j, int k)
    {
    Pk = a_p + Q;
            for(i = 0; i < 2; i++) 
               for( j = 0; j < 4; j++)
                   for( k = 0; k < 3; k++)  
    		Pk[i][j] += a_p[i][k] + Q[k][j]
    return;
    }
    }
    Any help would be grateful
    Last edited by Niheel; Jan 17 '11, 01:30 AM.
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Code:
    int main(double Q[6][6], int i, int j, int k)
    Main can't have parameters like these ones.It's either parameterless or it got two parameters which it recievies from the command line.Secondly, those variables(Pk,a_ p,A,A_t..) are local and exist only inside
    Code:
    void prioriestimate ()
    function. If you want to return variable from a function you either need to change return type to the type of variable you want to return(in case you want to return a single variable only) or pass pointers to the type of variables you want to return in parameters list. Also you actually need to call the function you want from main.

    For example:

    Code:
    int returnByType()
    {
        int a =5;
        return a; //you can only return a single variable
    }
    
    void returnByPointerReference(int *a, int *b,int *c)
    {
        int x = 5;
        
        *a = x;
        *b = x*x;
        *c = x+10;
    }
    
    int main() 
    {
       int a,b,c,d;
    
       d = returnByType(); //calling the function which returns value by its return type from main
    
       returnByPointerReference(&a,&b,&c); //return trough pointers, note the usage of address-of operator to pass the address of variable, and not its contents
    
     return 0;
    }
    Additionaly read this.

    Regards.

    Comment

    • vivek nandan
      New Member
      • Jan 2011
      • 9

      #3
      You can use following conventions,
      X[i][j] is equivalent to
      *(X+i*col+j) , where i varies for row and j varies for column.

      Comment

      • Chris Winton
        New Member
        • Jan 2011
        • 22

        #4
        cheers thanks for the response, helps alot.

        Comment

        Working...