question about structure array as a argument to a function

C

Chang Byun

Hi, folks,

I have a question about structure array as a argument.
The below short program is that main function call a subroutine
which increases real and imaginary part by 1 respectively in
complex numbered structure array.

There is no error when it is compiled.
But when it is running, it has a segmentation fault.

I guess I made a mistake when main passes the matrix to subroutine.
But I don't know how.

Please, help me out a trouble!

Thanks,

Chang

**************************************************************
#include <stdio.h>
#include <stdlib.h>

typedef struct DCOMPLEX{double r,i;}dcomplex;

void inc_mat(dcomplex **);

void main()
{
int ii,kk;
dcomplex mat[4][4];

for(ii=0;ii<4;ii++){
for(kk=0;kk<4;kk++){
(*(*(mat+ii)+kk)).r=ii;
(*(*(mat+ii)+kk)).i=kk;
}
}
inc_mat(mat);

}

void inc_mat(dcomplex **mata)
{
int ii,kk;
printf("mata=%d\n",mata);

for(ii=0;ii<4;ii++){
for(kk=0;kk<4;kk++){
(mata[ii][kk]).r +=1;
(mata[ii][kk]).i +=1;
}
}

}
 
C

Chang Byun

Thank you very much Jirka,
But I don't understand your reply because I am a C beginner.
Can you give me more detail?
It would be very appreciated.

Chang

Jirka said:
Chang said:
void main()


/* Tss, tss */
{
int ii,kk;
dcomplex mat[4][4];
[...]

}

void inc_mat(dcomplex **mata)


void inc_mat(dcomplex (*mata)[4])

Jirka
 
M

Martin Ambuhl

Chang said:
Hi, folks,

I have a question about structure array as a argument.
The below short program is that main function call a subroutine
which increases real and imaginary part by 1 respectively in
complex numbered structure array.

There is no error when it is compiled.

If so, you have the diagnostic level turned down to unacceptable level or
need to get a better compiler.

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
double r, i;
} dcomplex;

void inc_mat(dcomplex **);

int /* mha: correcting the 'void' error your
compiler should have reported. Get a
compiler that detects this error */
main(void)
{
int ii, kk;
dcomplex mat[4][4];

for (ii = 0; ii < 4; ii++) {
for (kk = 0; kk < 4; kk++) {
(*(*(mat + ii) + kk)).r = ii;
(*(*(mat + ii) + kk)).i = kk;
}
}
inc_mat((dcomplex **) mat); /* mha: mat is a dcomplex[4][4]; inc_mat
expects a dcomplex ** as an argument.
This is not the right way to fix this.
Get a compiler that detects this error.
*/
return 0; /* mha: for C89 (and good practice)
conformance */

}

void inc_mat(dcomplex ** mata)
{
int ii, kk;
printf("mata=%p\n", (void *) mata); /* mha: replacement for ... */
#if 0
printf("mata=%d\n", mata); /* mha: ... this illiteracy */
#endif

for (ii = 0; ii < 4; ii++) {
for (kk = 0; kk < 4; kk++) {
(mata[ii][kk]).r += 1;
(mata[ii][kk]).i += 1;
}
}

}
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top