Q: Matrixes and pointers
Author Message
Q: Matrixes and pointers

Quote:

> How come I get a warning message (suspicious pointer conversion) when
> performig the assignment operation graph.arcs = testMatrix; ?
> If I write something like graph.arcs[2][2] = 1, the program terminates due
> to access violation.

> Isn't testMatrix a pointer to a pointer to an int? In that case, why can't
> I use graph.arc and testMatrix interchangably?
> Best regards,

> Simon Wolfsriller, Sweden

> typedef struct {
>   int **arcs;
>   nodeT nodes[7];
> } graphT;

> void main() {
>   int testMatrix[7][7] = {{0, 1, 1, 0, 0, 0, 0},
>                                      {0, 0, 0, 0, 1, 1, 0},
>                                      {0, 0, 0, 0, 1, 1, 0},
>                                      {0, 0, 0, 0, 0, 0, 0},
>                                      {0, 0, 0, 0, 0, 0, 1},
>                                      {0, 0, 0, 0, 0, 0, 0},
>                                      {0, 0, 0, 0, 0, 0, 0}};
>  graphT graph;
>  graph.arcs = testMatrix;

testMatrix is an array of array of 7 ints. In the expression above it
evaluates to pointer to array of 7 ints not pointer to pointer to int.
You could do something like this (Except that you can't do this kind of
initialisation):

int *testPointers[]={
&testMatrix[0][0],
&testMatrix[1][0],
&testMatrix[2][0],
&testMatrix[3][0],
&testMatrix[4][0],
&testMatrix[5][0],
&testMatrix[6][0]

Quote:
};

graph.arcs=testPointers;

Tobias.

--
unix http://www.*-*-*.com/
clc http://www.*-*-*.com/ ~scs/C-faq/top.html
fclc (french): http://www.*-*-*.com/ ~rumeau/fclc/

Wed, 09 Feb 2005 00:26:17 GMT
Q: Matrixes and pointers

Quote:

> How come I get a warning message (suspicious pointer conversion) when
> performig the assignment operation graph.arcs = testMatrix; ?
> If I write something like graph.arcs[2][2] = 1, the program terminates due
> to access violation.

> Isn't testMatrix a pointer to a pointer to an int?

No.  testMatrix is an array of 7 arrays of 7 ints.  When testMatrix is
evaluated in an expression, if it's not the subject of the unary &
operator or the sizeof operator, it evaluates to a pointer to array of 7
ints.

Quote:
> In that case, why can't I
> use graph.arc and testMatrix interchangably?

They're not interchangeable.

Quote:

> Best regards,

> Simon Wolfsriller, Sweden

> typedef struct {
>  int **arcs;

If you want it to work, declare that instead as:

int (*arcs)[7];

Quote:
>  nodeT nodes[7];
> } graphT;

> void main() {
>  int testMatrix[7][7] = {{0, 1, 1, 0, 0, 0, 0},
>                                     {0, 0, 0, 0, 1, 1, 0},
>                                     {0, 0, 0, 0, 1, 1, 0},
>                                     {0, 0, 0, 0, 0, 0, 0},
>                                     {0, 0, 0, 0, 0, 0, 1},
>                                     {0, 0, 0, 0, 0, 0, 0},
>                                     {0, 0, 0, 0, 0, 0, 0}};
> graphT graph;
> graph.arcs = testMatrix;

There's a great deal more detail on this in the FAQ for this newsgroup.

- Kevin.

Wed, 09 Feb 2005 11:18:21 GMT
Q: Matrixes and pointers

Quote:

> > Isn't testMatrix a pointer to a pointer to an int?

No.

Quote:
> In that case, why can't I
> use graph.arc and testMatrix interchangably?

Premise false, question flushed.

Quote:

> typedef struct {
>   int **arcs;
>   nodeT nodes[7];
> } graphT;

> void main() {

Evil.  main() returns an int.
Quote:
>   int testMatrix[7][7] = {{0, 1, 1, 0, 0, 0, 0},
>                                      {0, 0, 0, 0, 1, 1, 0},
>                                      {0, 0, 0, 0, 1, 1, 0},
>                                      {0, 0, 0, 0, 0, 0, 0},
>                                      {0, 0, 0, 0, 0, 0, 1},
>                                      {0, 0, 0, 0, 0, 0, 0},
>                                      {0, 0, 0, 0, 0, 0, 0}};
>  graphT graph;
>  graph.arcs = testMatrix;

Wed, 09 Feb 2005 15:41:49 GMT
Q: Matrixes and pointers

Quote:

> > How come I get a warning message (suspicious pointer conversion) when
> > performig the assignment operation graph.arcs = testMatrix; ?
> > If I write something like graph.arcs[2][2] = 1, the program terminates due
> > to access violation.

> > Isn't testMatrix a pointer to a pointer to an int? In that case, why can't
> > I use graph.arc and testMatrix interchangably?

No.  Array and pointer are _not_ the same thing, and in particular
array of array is not array of pointer and pointer to array is not
pointer to pointer, etc.  FAQ section 6.

Quote:
> > typedef struct {
> >   int **arcs;
> >   nodeT nodes[7];
> > } graphT;

> > void main() {

The return type of main() is int.  FAQ 11.12a et seq.

Quote:
> >   int testMatrix[7][7] = ...
> >  graphT graph;
> >  graph.arcs = testMatrix;

> testMatrix is an array of array of 7 ints. In the expression above it
> evaluates to pointer to array of 7 ints not pointer to pointer to int.

Right.

Quote:
> You could do something like this (Except that you can't do this kind of
> initialisation):

> int *testPointers[]={
>         &testMatrix[0][0],
>         &testMatrix[1][0],
>         &testMatrix[2][0],
>         &testMatrix[3][0],
>         &testMatrix[4][0],
>         &testMatrix[5][0],
>         &testMatrix[6][0]
> };

> graph.arcs=testPointers;

You can in C99 (or gcc); or if the data (both testMatrix and testPointers)
is static (including at file scope, which is always static) even in C89.

--
- David.Thompson 1 now at worldnet.att.net

Fri, 11 Feb 2005 09:39:57 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages