473,656 Members | 2,824 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing a 2-D array to a function by reference..

I am trying to update a msg[11][11] array in function by passing the
address but it is showing an error. and also, i want the value of msg
array to be accessible to the full code that is inside the main
function...I hope i am making sense...Please look at the code and help
me in pointing out the error..

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

void generateMessage _matrix(int *msg[11][11],int k, int loops);

void main()
{
int *msg[11][11];
printf("\n Enter the code parameters and field k:");
scanf("%d",&k);

generateMessage _matrix(msg,k);
}

void generateMessage _matrix(int *msg[11][11],int k)
{
int i,j;
float x;
srand((unsigned )time(NULL));
for (i=0;i<k;i++)
{
for(j=0;j<k;j++ )
{
x = rand()/(RAND_MAX + 0.0);
if(x 0.5)
msg[i][j] = 1;
else
msg[i][j] = 0;
}
}
}

Aug 13 '06 #1
2 2178
so*******@gmail .com schrieb:
I am trying to update a msg[11][11] array in function by passing the
address but it is showing an error. and also, i want the value of msg
array to be accessible to the full code that is inside the main
function...I hope i am making sense...Please look at the code and help
me in pointing out the error..

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

void generateMessage _matrix(int *msg[11][11],int k, int loops);

void main()
main() returns int in standard C.
Make this
int main (void)
{
int *msg[11][11];
This is an array of 11 arrays of 11 pointers to int.
You either want a pointer to an array of 11 arrays of 11 ints.
or an array of 11 arrays of 11 ints itself:
int (*msg)[11][11];
and
int msg[11][11];
As you do not allocate memory below, you need the latter --
the former gives you only a pointer, i.e. a place to put the
address of an object like the latter.
printf("\n Enter the code parameters and field k:");
scanf("%d",&k);
Several mistakes:
- You did not declare k
- You do not check the return value of scanf() to make
sure that you got an int
- You do not check whether the int is within sensible boundaries
(i.e. >= 0 and <= 11)
generateMessage _matrix(msg,k);
As you have several ways of passing information about an
array, you can either
1) pass &msg which is of type int (*)[11][11] and the "value" of
which is the whole array; or
2) pass &msg[0] (or, equivalently, msg) which is of type int *[11]
and the "value" of the first entry of which is the first "row"; or
3) pass &msg[0][0] (or, equivalently, msg[0]) which is of type
int * and the "value" of the first entry of which is the first
"column" of the first "row"
The latter is not sensible as you need the information that a
"row" has 11 "columns".

Obviously, as main() returns int, we need
return 0;
}

void generateMessage _matrix(int *msg[11][11],int k)
This would be
1) void generateMessage _matrix(int (*pMsg)[11][11],int k)
2) void generateMessage _matrix(int *pMsg[11],int k)
or
void generateMessage _matrix(int Msg[][11],int k)
or
void generateMessage _matrix(int Msg[11][11],int k)
3) void generateMessage _matrix(int *pMsg,int k)
or
void generateMessage _matrix(int Msg[],int k)
or
void generateMessage _matrix(int Msg[11],int k)

Let us stick with variant 2) and passing "msg" above.
{
int i,j;
float x;
srand((unsigned )time(NULL));
for (i=0;i<k;i++)
{
for(j=0;j<k;j++ )
{
x = rand()/(RAND_MAX + 0.0);
if(x 0.5)
Note: You do not floating point for this,
if (rand() (RAND_MAX/2))
will do.
msg[i][j] = 1;
originally, this would have been the assignment of "1"
to a pointer of int -- which is not what you want.
else
msg[i][j] = 0;
}
}
Note: In case 1), you would have accessed pMsg as
(*pMsg)[i][j]
and in case 3) as
pMsg[i*11+j]
}

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Aug 13 '06 #2
so*******@gmail .com wrote:
I am trying to update a msg[11][11] array in function by passing the
address but it is showing an error. and also, i want the value of msg
array to be accessible to the full code that is inside the main
function...I hope i am making sense...Please look at the code and help
me in pointing out the error..
You should start off by reading section 6 of the comp.lang.c FAQ
http://c-faq.com/
#include<stdio. h>
#include<stdlib .h>
#include<time.h >
If you are running short of spaces I can email you a few million. Using
spaces makes code far easier to read
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void generateMessage _matrix(int *msg[11][11],int k, int loops);
The normal think is to pass a pointer to the first element of an array.
The easy notation for this with multi-dimensional arrays is:
void generateMessage _matrix(int msg[][11],int k, int loops);
Also, when you use or define it you only use two parameters. A little
proof reading for the obvious would not go amiss!
void generateMessage _matrix(int msg[][11],int k);
void main()
Completely the wrong type for main. main returns an int. You should
probably throw away whatever reference it was that told you it returns
void. Don't sell it or give it away, you would just be spreading
miss-information.

int main(void)
{
int *msg[11][11];
You said you wanted msg[11][11] so why the *?
int msg[11][11];

Some indentation would also be useful.
printf("\n Enter the code parameters and field k:");
There is no guarantee this will be displayed before the inputting is
done. Try flushing the output stream
scanf("%d",&k);
k? What is k? You haven't declared it. You should add a declaration to
the start of main.
generateMessage _matrix(msg,k);
Since main returns an int, return one.
return 0;
}

void generateMessage _matrix(int *msg[11][11],int k)
This should match the prototype provided earlier.
{
int i,j;
float x;
srand((unsigned )time(NULL));
Make sure you only call srand once. The easiest way would be to call it
from main instead just in case you later want to call this function more
than once.
for (i=0;i<k;i++)
{
for(j=0;j<k;j++ )
{
The method below looks neadlessly messy. I suggest you read question
13.16 of the comp.lang.c FAQ.
x = rand()/(RAND_MAX + 0.0);
if(x 0.5)
msg[i][j] = 1;
else
msg[i][j] = 0;
}
}
}
Deal with all the above and you will have a program that compiles and
initialises your array.
--
Flash Gordon
Still sigless on this computer.
Aug 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
119618
by: Ohmu | last post by:
Hi! How to pass an (multidimensional)array of something to a function with reference/pointer? Can anyone help me with that? Thanks, Ohmu
15
4666
by: Dave | last post by:
I'm currently working on a small project (admitedly for my CS class) that compares the time difference between passing by value and passing by reference. I'm passing an array of 50000 int's. However, since in C++ an array is passed by reference by default I need to embed the array into a struct in order to pass it by value. The problem is that I get a segmentation error when doing so. I'm using the Dev-c++ compiler. Any ideas? ...
3
14928
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
58
10120
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
8
4109
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
4
2491
by: hello smith | last post by:
I have a lot of functions that add values to an array. They alos update a global variable of type int. Currently, I use a global variable to hold this array. All functions access this array global variable and add their values. Then they increment the global int variable. Is it faster to pass the array and the int variable by reference to each function or just access the global variables? I have tried using gprof, I did not get...
2
4840
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short cmd; short fdc; WORD dsf; short boxcar; short average; short chan_ena;
11
8116
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
7
3297
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
8
3493
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
0
8297
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
8717
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...
0
8600
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
6162
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
5629
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();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1600
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.