473,782 Members | 2,542 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(dcomple x **);

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

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

}

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

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

}

Nov 13 '05 #1
4 2124
Chang Byun wrote:
void main()
/* Tss, tss */
{
int ii,kk;
dcomplex mat[4][4]; [...] }

void inc_mat(dcomple x **mata)


void inc_mat(dcomple x (*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(dcomple x **mata)

void inc_mat(dcomple x (*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(dcomple x **);

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((dcompl ex **) 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(dcomple x ** 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
2415
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 follow? Second, have I correctly passed the structure's pointer to the functions in this program?
11
3192
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 accomplish it. For example: int main void() { while there are more data types { print next data type; }
15
2781
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
4996
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 get an error: ---------------- An unhandled exception of type 'System.NullReferenceException' occured in
8
3451
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 I access the array elements. Thanks for your help. #include <stdio.h>
15
1858
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
2517
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 the array is an object itself but what is this syntax of the consecutive double quotes inside the brackets ?
16
309
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 a few pages prior to this, the example that K&R showed used this definition for it's version of strcpy void strcpy( char *s, char *t){ while ( *s++ = *t++);
4
1693
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); there is a line: int iOff = (char *)pLink - (char *)pPg;
0
9639
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10146
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9942
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.