473,396 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

question about structure array as a argument to a function

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
4 2108
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Mannequin* | last post by:
Hi all, I'm working on a quick program to bring the Bible into memory from a text file. Anyway, I have three questions to ask. First, is my implementation of malloc () correct in the program to...
11
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to...
15
by: damian birchler | last post by:
Hi I'm wondering of what type a structure is. Of course, it is a _structure_, but an array isn't an _array_ either. So of what type is a structure? I'd say a pointer, am I right?
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
8
by: SP | last post by:
The following code crashes after I add the two nested FOR loops at the end, I am starting to learn about pointers and would like to understand what I'm doing wrong. I think the problem is the way...
15
by: sethukr | last post by:
Hi everybody, While running the following program in GCC, i'm very much screwed. main() { char *ptr1; char arr; int i; char *ptr2;
2
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of...
16
by: mdh | last post by:
I have a question about the library function strcpy. In K&R on page 109, strcpy is used to copy a line ( line ) to a pointer (char *), which is first allocated space by K&Rs "alloc" function. In...
4
by: ctx2002 | last post by:
hi guys: I am reading Sqlite code at moment, my c language skill not good enough , hope some one here can help me. In function listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg);...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.