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

T

Tao Li

#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
 
B

Ben Bacarisse

Tao Li said:
#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.
 
K

Keith Thompson

Ben Bacarisse said:
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".
 
B

Ben Bacarisse

Keith Thompson said:
Ben Bacarisse said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,961
Messages
2,570,130
Members
46,689
Latest member
liammiller

Latest Threads

Top