By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

question about structure array as a argument to a function

P: n/a
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;
}
}

}

Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Chang Byun wrote:
void main()
/* Tss, tss */
{
int ii,kk;
dcomplex mat[4][4]; [...] }

void inc_mat(dcomplex **mata)


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

Jirka

Nov 13 '05 #2

P: n/a
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 Klaue wrote:
Chang Byun wrote:
void main()

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


[...]
}

void inc_mat(dcomplex **mata)

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

Jirka


Nov 13 '05 #3

P: n/a
Chang Byun wrote:
But I don't understand your reply because I am a C beginner.


the (*a)[4] issue:
http://www.eskimo.com/~scs/C-faq/s6.html

the void main issue:
http://www.eskimo.com/~scs/C-faq/s11.html

Jirka

Nov 13 '05 #4

P: n/a
Chang Byun wrote:
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;
}
}

}


--
Martin Ambuhl

Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.