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.