what's wrong with my program. how do I pass double pointer?

Discussion in 'C Programming' started by Tao Li, Oct 27, 2009.

  1. Tao Li

    Tao Li Guest

    #include <stdio.h>
    2
    3 int mat[10][20];
    4
    5
    6 void func(float **p){
    7 int i,j;
    8
    9 for(i=0; i<10; i++)
    10 for(j=0; j<20; j++)
    11 printf("%d\n", p[j]);
    12
    13 }
    14
    15
    16 int main()
    17 {
    18 int mat[10][20];
    19 int i,j;
    20
    21 for(i=0; i<10; i++)
    22 for(j=0; j<20; j++)
    23 mat[10][20] = i + j;
    24
    25 func(mat);
    26 return 0;
    27 }
    28
    29

    The program compiled fine, but give Segmentation fault error. I think
    the reason is double pointer **P in FUNC can't access the content of
    two dimentional array MAT.

    How can I fix it. Thank you very much
     
    Tao Li, Oct 27, 2009
    #1
    1. Advertising

  2. Tao Li

    Tao Li Guest

    please ignore line 18, it repeats the global defination.

    On Oct 26, 10:27 pm, Tao Li <> wrote:
    >  #include <stdio.h>
    >   2
    >   3 int mat[10][20];
    >   4
    >   5
    >   6 void func(float **p){
    >   7     int i,j;
    >   8
    >   9     for(i=0; i<10; i++)
    >  10         for(j=0; j<20; j++)
    >  11             printf("%d\n", p[j]);
    >  12
    >  13 }
    >  14
    >  15
    >  16 int main()
    >  17 {
    >  18     int mat[10][20];
    >  19     int i,j;
    >  20
    >  21     for(i=0; i<10; i++)
    >  22         for(j=0; j<20; j++)
    >  23             mat[10][20] = i  + j;
    >  24
    >  25     func(mat);
    >  26     return 0;
    >  27 }
    >  28
    >  29
    >
    > The program compiled fine, but give Segmentation fault error. I think
    > the reason is double pointer **P in FUNC can't access the content of
    > two dimentional array MAT.
    >
    > How can I fix it. Thank you very much
     
    Tao Li, Oct 27, 2009
    #2
    1. Advertising

  3. Tao Li <> writes:

    > #include <stdio.h>
    > 2
    > 3 int mat[10][20];


    Did you mean float mat[10][20]?

    > 6 void func(float **p){


    If you correct the type to float you can pass mat by writing:

    void func(float (*p)[20]) {

    but you probably need to read: http://c-faq.com/aryptr/pass2dary.html
    and the FAQs that that page links to as well.

    > 7 int i,j;
    > 8
    > 9 for(i=0; i<10; i++)
    > 10 for(j=0; j<20; j++)
    > 11 printf("%d\n", p[j]);
    > 12
    > 13 }
    > 14
    > 15
    > 16 int main()
    > 17 {
    > 18 int mat[10][20];


    [ignored as per subsequent post]

    > 19 int i,j;
    > 20
    > 21 for(i=0; i<10; i++)
    > 22 for(j=0; j<20; j++)
    > 23 mat[10][20] = i + j;


    You try to set one element many times and leave most elements
    untouched. Is that what you mean to do?

    I say "try" because the element mat[10][20] does not exist. The
    indexes go from mat[0][0] to mat[9][19].

    > 25 func(mat);


    The compiler must complain about this call and you should take what it
    says seriously!

    While you are learning, use the highest possible warning level and
    make sure you know what all the messages mean. Try to aim for none,
    but be sure you know that any you leave are quite safe and do not
    represent a serious problem. This is not easy, but you'll learn a lot
    from trying.

    > 26 return 0;
    > 27 }
    >
    > The program compiled fine, but give Segmentation fault error. I think
    > the reason is double pointer **P in FUNC can't access the content of
    > two dimentional array MAT.


    That is one thing that is wrong but it is not the only possible cause.

    --
    Ben.
     
    Ben Bacarisse, Oct 27, 2009
    #3
  4. Ben Bacarisse <> writes:
    [...]
    > While you are learning, use the highest possible warning level and
    > make sure you know what all the messages mean.

    [...]

    Where "learning" is nearly synonymous with "not dead".

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 27, 2009
    #4
  5. Keith Thompson <> writes:

    > Ben Bacarisse <> writes:
    > [...]
    >> While you are learning, use the highest possible warning level and
    >> make sure you know what all the messages mean.

    > [...]
    >
    > Where "learning" is nearly synonymous with "not dead".


    How true. I will confess that I went through a "young and arrogant"
    stage where I thought I no longer needed all those pesky warnings, but
    I have came round to having them back on all the time.

    --
    Ben.
     
    Ben Bacarisse, Oct 27, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,647
    Victor Bazarov
    Feb 17, 2005
  2. Michael Mair

    Typecast long double->double seems to go wrong

    Michael Mair, May 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    695
    Michael Mair
    May 24, 2004
  3. Robert Palma

    Pass pointer to double array as func. parameter

    Robert Palma, Jun 5, 2005, in forum: C Programming
    Replies:
    10
    Views:
    889
  4. Mr A
    Replies:
    111
    Views:
    2,181
  5. Radde
    Replies:
    38
    Views:
    1,024
Loading...

Share This Page