There are several things wrong here.
First of all, you use == (comparison) where you meant to use = (assignment) (This is rather ironic, considering how often people the reverse of that mistake). This means that *z is never initialized. For that matter, z itself is never initialized, so you're accessing garbage memory.
Your error probably occurs when you try to write an integer ("%d") into a pointer (z). Remember, scanf takes a pointer to where you want the input to be written to, so if your input is an int, you'll want to pass an pointer to an int. You're passing a pointer to a pointer to an int.
The pointer logic here is probably what has you the most confused, so let's go through that in detail:
arrayPrimary[x][y] is an integer that has an address in memory like any normal variable.
scanf needs to know this location in order to write the value into your array.
- It looks like you're trying to use another variable,
z, to serve as an argument to scanf. However, even if you copy the value of arrayPrimary[x][y] to the address z points to, z is still a different variable from your array.
- The memory address of z (
&z) has no relation whatsoever to your array. When you pass &z to scanf, scanf will look at this address and write the input to it. Therefore, you are directing the input into z, not to your array.
Try this:
for (y=0; y<2; y++)
{
int *z = &arrayPrimary[x][y];
printf("please enter a value for [%d][%d]:",x,y);
scanf("%d", z);
}
This way, it goes like this:
- You create a pointer,
z, that points to the data you want to change (namelyarrayPrimary[x][y]).
- You pass this data yo
scanf, which writes input to where z points to-- namely, arrayPrimary[x][y].
However, there's no need for a separate pointer. You can just write:
for (y=0; y<2; y++)
{
printf("please enter a value for [%d][%d]:",x,y);
scanf("%d", &arrayPrimary[x][y]);
}
*z==arrayPrimary[x][y];is this supposed to be an assignment?==is not assignment operator.