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

S

sonaliagr

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[j] = 1;
else
msg[j] = 0;
}
}
}
 
M

Michael Mair

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[j] = 1;


originally, this would have been the assignment of "1"
to a pointer of int -- which is not what you want.
else
msg[j] = 0;
}
}


Note: In case 1), you would have accessed pMsg as
(*pMsg)[j]
and in case 3) as
pMsg[i*11+j]


Cheers
Michael
 
F

Flash Gordon

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 said:
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[j] = 1;
else
msg[j] = 0;
}
}
}


Deal with all the above and you will have a program that compiles and
initialises your array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top