Thread: Suspicious Pointer Conversion

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    32

    Suspicious Pointer Conversion

    Can anyone explain what this warning means and how to cure,
    Also written a function to multiply 2 matrices but its throwing up half right half wrong answers is there something silly ive missed out or got wrong?
    Code:
    typedef coord [2}
    void MatCalc (coord *p2d_1, coord *p2d_2, coord *p2d_3, int *r, int *c, int *n)
    {
    
    	int c_i, r_i, N, sum;
    
    	for(r_i=0; r_i<*r; r_i++){
    	for(c_i=0; c_i<*c; c_i++){
    		sum = 0;
    	for(N=0; N<*n; N++){
    	sum += p2d_1 [r_i] [N] * p2d_2 [N] [c_i];
    	p2d_3 [r_i] [c_i] = sum;
    
    				}
    				}
    			  }
    }
    Wjere r and c would be i/j and n k in common matrix terms

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    This caught my eye:
    Code:
    p2d_1 [r_i] [N]
    I don't think you can dereference a pointer as a 2D array. The compiler can't know what dimensions it has. You could use an algorithm for it instead:

    p2d_1 [(Y * Width) + X]
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    sorry foe being ignorant but X and Y representing what there. and wouldnt:
    Code:
    oid EleEnt (coord *p_2d, int r, int c, int num)
    {
      int r_i, c_i;
    for(r_i=0; r_i<r; r_i++){
    		printf("\nfor row %d of Matrix_%d\n",r_i +1,num);
    	for(c_i=0; c_i<c; c_i++){
    		printf("\tEnter element value for column %d:",c_i +1);
    		scanf("%d",&p_2d [r_i] [c_i]);
    				}
    be drefing a 2D array and this function works fine.
    the problem occurs when N (the number columns mat has , rows mat 2 has) is bigger than 1

  4. #4
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    this isn't actually dereferencing the pointer.
    Code:
    p2d_3 [r_i] [c_i] = sum;
    you will want
    Code:
    *p2d_3 [r_i] [c_i] = sum;
    also
    Code:
    sum += (*p2d_1[r_i][N]) * (*p2d_2[N][c_i]);
    ---edit---
    added the also part.
    Last edited by stumon; 04-08-2003 at 04:50 PM.
    The keyboard is the standard device used to cause computer errors!

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>*p2d_3 [r_i] [c_i] = sum;
    But this cannot be done if the pointer was declared like so:
    >>char *p2d_3;
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char buf[BUFSIZ];
      char *p = buf;
      
      p[0][0] = 'a';  /* Bad */
      *p[0][0] = 'a'; /* Bad */
      p[0] = 'a';     /* OK  */
      p[10+20] = 'a'; /* OK */
      
      return(0);
    }
    
    /*
    Compiler output:
    
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    junk1.c:
    Error E2062 junk1.c 8: Invalid indirection in function main
    Error E2062 junk1.c 9: Invalid indirection in function main
    
    *** 2 errors in Compile ***
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    What is the pointer was declared as
    char blah[BUFSIZ][BUFSIZ];
    char *b = blah;
    Wont this work with my example? I figured he was passing the address of a 2d array.

    --edit--
    this is why if people pass arrays and so forth in functions, they need to also put the function that is calling the function to see whats its actually passing! otherwise, we here are clueless to what its pointing to.
    Last edited by stumon; 04-08-2003 at 05:08 PM.
    The keyboard is the standard device used to cause computer errors!

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>char *b = blah;
    This is still the same. Just take my code, and change it around, and you're compiler should warn you when its got problems.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    I got round the Invalid indirection thing by making my own type that consisits of a aray of two ints and called it coord and use this with a pointer to pass a array into and out the function.well in theory anyway , Thanks for all the help so far though, given me ideas to play with

  9. #9
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Not that it really makes a diff, but this is what i was thinking of when passing the address of a 2d array. Seeing how the memory is stored in order even in a 2d array, you can use pointer incrementation to get all the values. I confused this with derefing pointers with pointers to elements. sorry. but hey, this will work if you ever need anything like this
    Code:
    #include <stdio.h>
    
    void func1(int *);
    
    int main()
    {
    	int blah[2][2] = {0};
    	blah[1][0] = 10;
    	blah[0][1] = 50;
    
    	func1(&blah[0][0]);
    
    	return 0;
    }
    
    void func1(int *b)
    {
    	int i, ii;
    	
    	for(i = 0; i <= 1; i++)
    		for(ii = 0; ii <= 1; ii++)
    			printf("[ %d ]", (*b++));
    }
    The keyboard is the standard device used to cause computer errors!

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    thanks for all the help so far but still doesnt get to heart of why the function dont work, it will run so dont think the pointing is affecting as i also had the same problem when i just ran everythign in the main fucntion and assigned everything by name rather than pointers. Ive gone it several times now and just cant get why it doesnt work.When N is one everything is fine and mulitplies the matrices fine as soon as it goea above one all hell breaks loose and i get 1 or 2 right answers in the product array

  11. #11
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Question: Are you sure this is right,
    Code:
    sum += p2d_1 [r_i] [N] * p2d_2 [N] [c_i];
    Why are you having N be the element of the column's part in array 1, and it be the rows part in array 2? Is there a reason,? Im not sure what you are actually trying to do, just multiple 2 - 2d arrays. If so, this will not be getting the correct element numbers to multiply.

    Ex. Is this what you want to do? Multiple 1 with 1, 2 with 2, 3 with 3, and 4 with 4 and so on......
    Code:
    array1                            array2
    ________________________________________
    1  |  2                           1  |  2
    3  |  4                           3  |  4
    5  |  6                           5  |  6
    and so forth.......?
    Last edited by stumon; 04-08-2003 at 08:10 PM.
    The keyboard is the standard device used to cause computer errors!

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    Im multiplying two real matrices together and using 2D arrays to store the element values. and N is the common order number betweeen the 2 matrices, ie mat 1 column must be equal to Mat 2's no of rows for a legal multiplication

  13. #13
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    this is the proper way to multiple matrices, is this what you are trying to do?
    Code:
                                   -     -
                 -       -        |  9 10 |
                |  1 2 3  |       |       |
            A = |         |, B =  | 11 12 |.
                |  4 5 6  |       |       |   
                 -       -        | 13 14 |
                                   -     -  
    
    Their product is 
    
                  -                                       -     -       -
                 |  1x9 + 2x11 + 3x13  1x10 + 2x12 + 3x14  |   |  70  76 |
            AB = |                                         | = |         |
                 |  4x9 + 5x11 + 6x13  4x10 + 5x12 + 6x14  |   | 169 184 |
                  -                                       -     -       -
    Let me know if this is what you want, if so, i will show youa good algorithm to get this.
    The keyboard is the standard device used to cause computer errors!

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    Thats exactly what i want to do.would be great if you could show me it, thanks

  15. #15
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    here, this will do it, if you follow the code, its kinda hard, this takes the elements of array1 with the oppsite of array2, so when array 1 = [1][0], array2 = [0][1] so it will multiply 4 * 10, and if array1 = [1][2], array2 = [2][1], so it will multiply 6 * 14, and so on...
    Code:
    int j, l, sum = 0;
    int array1[2][3], array2[3][2];
    
    for (j = 0; j < 2; j++)
    	for(l = 0; l < 3; l++)
    		sum += array1[j][l] * array2[l][j];
    The keyboard is the standard device used to cause computer errors!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. Pointer array's and ASCII to bit conversion
    By AdamLAN in forum C++ Programming
    Replies: 12
    Last Post: 05-06-2005, 05:55 PM
  3. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Replies: 2
    Last Post: 02-07-2002, 09:39 AM