# Suspicious Pointer Conversion

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 04-08-2003
mr_spanky202
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
• 04-08-2003
Magos
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]
• 04-08-2003
mr_spanky202
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
• 04-08-2003
stumon
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---
• 04-08-2003
Hammer
>>*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 *** */```
• 04-08-2003
stumon
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.
• 04-08-2003
Hammer
>>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.
• 04-08-2003
mr_spanky202
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
• 04-08-2003
stumon
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++)); }```
• 04-08-2003
mr_spanky202
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
• 04-08-2003
stumon
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.......?```
• 04-09-2003
mr_spanky202
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
• 04-09-2003
stumon
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.
• 04-09-2003
mr_spanky202
Thats exactly what i want to do.would be great if you could show me it, thanks :)
• 04-09-2003
stumon
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];```
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last