Stupid question about pointers (it is driving me crazy). Pls help!!!

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default 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;
    > ...
    > }
    > 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?
    No, it isn't. It is a two-dimensional array, no pointers involved.
    > So I can do
    > puntero2=matriz;
    > Why is compiler giving me this warning?
    > prueba_con_matrices.c:21: warning: assignment from incompatible pointer type
    You can't because matriz without the '[]' stuff is treated as a
    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)
    > }
    > void lets_give_a_value_to_matriz(int n, int **p,int value)
    You need here

    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 can't work because the compiler has no way of figuring out that
    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

  4. #3

    Default Re: Stupid question about pointers (it is driving me crazy). Pls help!!!

    [email]Jens.Toerring@physik.fu-berlin.de[/email] wrote:
    > 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.
    Sorry, that was meant to be: first all the elements 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139