Reputation: 13
I am trying to pass a pointer to an array to my function GetAgent and modify the values there and then I call my func on the main function. I get the errors:
I tried to array = &layout but it shows literally the same.
I'm basically trying to build a toroidal grid where agents spawn randomly on the grid and I have to locate the 8 nearest locations that surround him.
main.c: In function 'GetAgent':
main.c:10:13: error: subscripted value is neither array nor pointer nor vector
array[i][j] = array[i % xdim][j % ydim];
main.c:10:34: error: subscripted value is neither array nor pointer nor vector
array[i][j] = array[i % xdim][j % ydim];
main.c: In function 'main':
main.c:25:11: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] array = layout;
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define xdim 20
#define ydim 20
void GetAgent(int i, int j, char *array)
{
array[i][j] = array[i % xdim][j % ydim];
}
int main(){
//FILE *fp;
//fp = fopen("file.ini", "r");
//fscanf(fp, "%d %d", &xdim, &ydim);
srand((unsigned) time(NULL));
int x = (rand() % 20);
int y = (rand() % 20);
char layout[xdim][ydim];
char *array;
array = layout;
for (int i = 0; i < xdim; i++){
for (int j = 0; j < ydim; j++ ){
if (i == x && j == y){
layout[i][j] = 'x';
} else {
layout[i][j] = '.';
}
}
}
for (int i = 0; i < xdim; i++){
for (int j = 0; j < ydim; j++){
if (i == x && j == y){
for (int z = 1; z < 2; z++){
for (int t = 1; t < 2; t++){
GetAgent(i, j, array);
layout[x][y + t] = '1';
GetAgent(i, j, array);
layout[x + z][y + t] = '2';
GetAgent(i, j, array);
layout[x + z][y] = '3';
GetAgent(i, j, array);
layout[x + z][y - t] = '4';
GetAgent(i, j, array);
layout[x][y - z] = '5';
GetAgent(i, j, array);
layout[x - z][y - t] = '6';
GetAgent(i, j, array);
layout[x - z][y] = '7';
GetAgent(i, j, array);
layout[x - z][y + t] = '8';
}
}
}
}
}
for (int i = 0; i < xdim; i++){
for (int j = 0; j < ydim; j++ ){
printf("%c ", layout[i][j]);
}
printf("\n");
}
}
Upvotes: 1
Views: 112
Reputation: 67476
Your probably do not understand the arrays and pointers in C.
But if you want to pass the char *
pointer and and index the referenced object like the two dimensional array - of course you can work it around.
array[i + j*xdim] = array[(i % xdim) + (j % ydim) * xdim];
I abstract from the logic of this assignment.
Upvotes: 1
Reputation: 30926
For the assignment
char (*array)[ydim];
array = layout;
Then pass it to the function
void GetAgent(int i, int j, char (*array)[ydim])
{
array[i][j] = array[i % xdim][j % ydim];
}
In your case compiler complained since you were dereferencing a pointer value twice which should be done once. Moreover it had type incompatibility issues.
But again, why aren't you passing the 2d array itself. Array decays into pointer to first element - that means you will also be able to change it in the called function where you have passed the 2d array. Then you would just pass the layout
to the function like this
GetAgent(i, j, layout);
And you don't need to change the signature of GetAgent
because 2d array decyas into int(*)[dim]
- pointer to an array of dim
int
's.
Upvotes: 0