467,915 Members | 1,118 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

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
  • viewed: 1876
Share:
2 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Dave | last post: by
3 posts views Thread by domeceo | last post: by
8 posts views Thread by kalinga1234 | last post: by
4 posts views Thread by hello smith | last post: by
2 posts views Thread by Steve Turner | last post: by
11 posts views Thread by John Pass | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.