0

I wanted to declare a pointer which would point to a matrix and retrieve a value back from the matrix:

float *p; float ar[3][3]; [..]//give values to ar[][] p = ar; //Keep on printing values in the 3 X 3 matrix for (int i = 0; i < 10; i++) { p = p + i; cout << *p << ", "; } 
1
  • If I should use **p to point to the matrix ar[][], and how can I get the value of the pointer which is pointing to a specific element in the matrix. Commented Oct 26, 2011 at 5:07

2 Answers 2

3

I suspect that you are after:

p = &ar[0][0]; 

which can also be written:

p = ar[0]; 

although your for loop then needs to use p = p + 1; rather than p = p + i;.


You can also use a pointer to an array, if you want your loop to be able to access the members of the matrix by row and column:

float (*p)[3]; p = ar; for (int i = 0; i < 3; i++) for (j = 0; j < 3; j++) { cout << p[i][j] << ", "; } 
Sign up to request clarification or add additional context in comments.

5 Comments

what if I wanted to use a double pointer to point at the matrix and de-reference values from the matrix?
@Josh: You must clarify what you mean by a "double pointer". If you mean a double * (pointer to double), then you can't, because the matrix stores float values. If you mean a float ** (pointer to pointer to float), then the only way to do that is to create a shadow array of float * values, pointing at the first float in each row of the matrix. Perhaps you want a pointer to an array (float (*)[3]) (see update)?
I wanted to use "**p" to point to some elements in ar[][] and retrieve those elements (to find the determinant of the matrix), so can I do this: **p; p = ar; value += **(p + 1);
@Josh: You can't really do that, because float **p isn't the right tool for that job. A float **p variable is for pointing at a float * (or an array of float *), and you don't have any float * objects in your example. You could create some, but there's no point, it would just be needlessly obfuscatory.
@Josh: You can iterate through the matrix using the pointer p in my first example, but you only have to dereference it once (*(p + i) or p[i] for i in 0..9).
0

EDIT2: I'm an idiot I accidentally had float **matrix instead of float (*matrix)[3]. caf had the right answer all along.

Is this what you want?

#include <stdio.h> #include <stdlib.h> void print_matrix(float (*matrix)[3], size_t rows, size_t cols) { int i, j; for (i = 0; i < rows; i++) for (j = 0; j < cols; j++) printf("%f ", matrix[i][j]); } int main(void) { float ar[][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; print_matrix(ar, 3, 3); return EXIT_SUCCESS; } 

EDIT: you can also have:

float *row1, *row2, *row3; row1 = ar[0]; row2 = ar[1]; row3 = ar[2]; ... float row1_total = row1[0] + row1[1] + row2[2]; 

8 Comments

I wanted to use a pointer to access the matrix elements. Instead of printing using the matrix itself, I wanted a pointer to access the elements in the matrix and perform some operations using them (like addition)
float **matrix is a pointer (to a pointer to a float). You can access the elements using pointer arithmetic or array notation as I did.
so if I wanted to access the value stored in ar[1][1], I would do **(matrix + 4)?
It would be better to do it as matrix[1][1], especially if each of the "rows" in the matrix weren't in consecutive blocks of memory. You gain no advantage by doing **(matrix + 4), only reducing readability and potentially causing problems.
Did you even try this? It does not work; you cannot pass a float (*)[3] to a function expecting a float **.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.