Ask a Question related to UNIX Programming, Design and Development.
-
Eusebio Salgado #1
Stupid question about pointers (it is driving me crazy). Pls help!!!
Ok, let's imagine an stupid program working with matrix.
{
...
int matriz[N][N];
...
int **puntero2;
...
}
as defined above matriz is a matrix of NxN elements, an array of N
pointers to N arrays of N elements of type int, isn't it?
So "matriz" itself is a pointer to pointer to int, isn't it?
So I can do
puntero2=matriz;
Why is compiler giving me this warning?
prueba_con_matrices.c:21: warning: assignment from incompatible pointer type
Problem is that I have a main like:
int main (void)
{
...
int matriz[10][10];
...
lets_give_a_value_to_matriz(10,matriz,5)
}
void lets_give_a_value_to_matriz(int n, int **p,int value)
{
int i,j;
for (i=0;i<n;i++)
for (j=0;j<n,j++)
p[i][j]=value;
}
This gives a warning like this:
prueba_con_matrices.c:40: warning: passing arg 2 of `matriz_a_valor'
from incompatible pointer type
and I obtain an "Segmentation fault" if I try to execute.
I have tried same with vectors. It is:
int vector[10];
int *pointer;
And it works fine. So I can't undestarnd what the problem is...
Please, any suggestion is wellcomed.
BR,
Eu
Pd.- I'm using a linux box with
gcc version 3.2.2 (Mandrake Linux 9.1 3.2.2-3mdk)
Eusebio Salgado Guest
-
Contribute Driving me Crazy!... Help!
:confused; I have built a site using standard XHTML 1.0 Transitional practices and one of my div layers (footer) is positioned absolutely to the... -
styles driving me crazy!
My website was set up using Dreamweaver 6.1 on a Windows pc. I'm trying to edit pages using Contribute 3.1 on a Mac. Every time I sect text and... -
LINE PATH QUESTION PLEASE !!!!!!!! ITS DRIVING ME CRAZY !!!!!!!!
HI First loet me say, I'm an idiot. Next let me say the people here are gods...so forgive this dumb question...here goes- Let say that I have a... -
CDONTS is driving me crazy. Please help !!!
Hi, I am trying to create a web page in ASP where the user enters their name and email address, and when they submit the form, an email is... -
focusManager driving me CRAZY!
First off, you want to address the focusManager on _root _root.focusManager.setFocus(myTextBox) The problem I am having, is that when I set... -
Jens.Toerring@physik.fu-berlin.de #2
Re: Stupid question about pointers (it is driving me crazy). Pls help!!!
Eusebio Salgado <eu_soy_yo@terra.es> wrote:
> Ok, let's imagine an stupid program working with matrix.> {
> ...
> int matriz[N][N];
> ...
> int **puntero2;
> ...
> }No, it isn't. It is a two-dimensional array, no pointers involved.> as defined above matriz is a matrix of NxN elements, an array of N
> pointers to N arrays of N elements of type int, isn't it?
> So "matriz" itself is a pointer to pointer to int, isn't it?
> So I can do> puntero2=matriz;You can't because matriz without the '[]' stuff is treated as a> Why is compiler giving me this warning?
> prueba_con_matrices.c:21: warning: assignment from incompatible pointer type
simple pointer to int.
In memory the elements of matriz are stored as a flat array, first all
the elements of the first column, then the elements of the second etc.
And matriz without the '[]' is a pointer to the first element of the
first row.
By defining matriz as matriz[10][10] you tell the compiler how it can
calculate where a certain element is. If, for example, you have
matriz[7][3] the compiler uses this information to convert this to
matriz[7][3] -> *( matriz + 10 * 7 + 3 )
But if you pass matriz as a pointer to a function this information
gets lost and you have to do the above conversion all by yourself.
> Problem is that I have a main like:> int main (void)
> {
> ...
> int matriz[10][10];
> ...
>> lets_give_a_value_to_matriz(10,matriz,5)
> }You need here> void lets_give_a_value_to_matriz(int n, int **p,int value)
void lets_give_a_value_to_matriz( int n, int *p, int value )
This can't work because the compiler has no way of figuring out that> {
> int i,j;
> for (i=0;i<n;i++)
> for (j=0;j<n,j++)
> p[i][j]=value;
the pointer you passed to the function was originally a matrix. So
you must do the calculation of the memory location you want to change
by yourself:
*( p + n* i + j ) = value;
Since the elements of the matrix are stored as a flat array, you> }
can even simplify the function to
void lets_give_a_value_to_matriz( int n, int *p, int value )
{
int i;
for ( i = 0; i < n * n; i++ )
p[ i ] = value;
}
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| [email]Jens.Toerring@physik.fu-berlin.de[/email]
_ | | | | | |
| |_| | | | | | [url]http://www.physik.fu-berlin.de/~toerring[/url]
\___/ens|_|homs|_|oerring
Jens.Toerring@physik.fu-berlin.de Guest
-
Jens.Toerring@physik.fu-berlin.de #3
Re: Stupid question about pointers (it is driving me crazy). Pls help!!!
[email]Jens.Toerring@physik.fu-berlin.de[/email] wrote:
Sorry, that was meant to be: first all the elements of the first *row*,> In memory the elements of matriz are stored as a flat array, first all
> the elements of the first column, then the elements of the second etc.
> And matriz without the '[]' is a pointer to the first element of the
> first row.
then the elements of the second *row* etc. (it's FORTRAN where the
elements are stored in a column after column fashion).
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| [email]Jens.Toerring@physik.fu-berlin.de[/email]
_ | | | | | |
| |_| | | | | | [url]http://www.physik.fu-berlin.de/~toerring[/url]
\___/ens|_|homs|_|oerring
Jens.Toerring@physik.fu-berlin.de Guest



Reply With Quote

