Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > question about structure array as a argument to a function

Reply
Thread Tools

question about structure array as a argument to a function

 
 
Chang Byun
Guest
Posts: n/a
 
      10-20-2003
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;
}
}

}

 
Reply With Quote
 
 
 
 
Jirka Klaue
Guest
Posts: n/a
 
      10-20-2003
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

 
Reply With Quote
 
 
 
 
Chang Byun
Guest
Posts: n/a
 
      10-20-2003
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
>


 
Reply With Quote
 
Jirka Klaue
Guest
Posts: n/a
 
      10-20-2003
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

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-20-2003
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable argument function as a parameter of a variable argument function AikidoGuy C Programming 11 11-21-2011 10:43 PM
function argument dependent on another function argument? Reckoner Python 11 01-19-2009 03:31 AM
Passing a function as an argument and using the evaluated functionas an argument User1014 Javascript 1 11-30-2006 12:13 PM
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM



Advertisments