473,387 Members | 1,592 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,387 software developers and data experts.

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 2153
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
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
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. ...
3
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) {...
58
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...
8
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
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...
2
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...
11
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...
7
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...
8
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.