Thread: Problem with pointer to a pointer variable

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Problem with pointer to a pointer variable

    Hi all.

    I am using a program called HPGCC which converts C files into HP files for the HP programming calculator. However, I think my problem has to do with C programming, not HP.

    I have the following main:

    Code:
    int main(void){
    
        double m[2][2];
        
        m[0][0]=1;
        m[0][1]=2;
        m[1][0]=3;
        m[1][1]=4;
        
        sat_push_double_matrix(2,2,m);
        
    
        
        return 0;
    
    }
    "sat_push_double_matrix" is a function to push a matrix to the stack of the HP calculator. Its format is:
    Code:
    BOOL sat_push_double_matrix(int rows,int columns,double **array)
    I'm not interested in the BOOL output and I am not familiar with pointer to pointer variables, which is my problem. If I leave it like this I get the following warning in my compiler:

    warning: passing argument 3 of 'sat_push_double_matrix' from incompatible pointer type

    On my HP calculator it does give back a 2x2 matrix, but its content is not what i expected, which should be [[1,2],[3,4]] as i wrote in the main.

    So my question is what should I do in the main function in order to avoid this variable format incompatibility so the expected matrix gets pushed into the stack? I would be very grateful if you could post me a solution to this problem.

    Regards,
    Rodri

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Rodri View Post
    So my question is what should I do in the main function in order to avoid this variable format incompatibility so the expected matrix gets pushed into the stack? I would be very grateful if you could post me a solution to this problem.
    A double** is a pointer to an array of pointers to double (I think you realize this). Kind of a hassle, but this is the only way to have a parameter for a variable sized matrix in C. So, what you need is an array of pointers to double (and as with all arrays in C, you can use the variable name as a pointer to itself):

    Code:
    	double *m[2];
    	int i;
    	for (i=0; i<2 ;i++) {
    		m[i] = malloc(2*sizeof(double));
    	}
    	m[0][0]=1;
    	m[0][1]=2;
    	m[1][0]=3;
    	m[1][1]=4;
    Alternately, you could do this:

    Code:
    	double m[2][2], *p[2];
    	m[0][0]=1;
    	m[0][1]=2;
    	m[1][0]=3;
    	m[1][1]=4;
    	int i;
    	for (i=0; i<2 ;i++) {
    		p[i] = m[i];
    	}
    And use "p" for the function call.

    If you use the first version, with malloc(), you must free() the matrix when done with it:

    Code:
    	for (i=0; i<2 ;i++) {
    		free(m[i]);
    	}
    Last edited by MK27; 11-20-2011 at 10:16 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Works nicely now, thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  2. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  3. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  4. Replies: 4
    Last Post: 11-05-2006, 02:57 PM
  5. pointer to pointer how do i solve following problem?
    By kobra_swe in forum C Programming
    Replies: 5
    Last Post: 07-19-2006, 04:49 PM