This is my final version for gameoflife. I have this error bug I can't fix. Please help me to solve this one:
1 IntelliSense: argument of type "int (*)[50]" is incompatible with parameter of type "int *"
line 79 print(board, HEIGHT, WIDTH);
void print(int *board, int rows, int cols)
{
int x, y;
char c;
for (y = 0; y < rows; y++) {
for (x = 0; x < cols; x++) {
if (*(board + y*cols + x) == 1)
printf("X");
else
printf(" ");
}
printf("\n");
}
printf("Press any key to continue:\n");
getchar();
}
That error means you are trying to pass a function an argument of type "array of 50 pointers to int" while it should be getting an argument of type "pointer to int" (which could also be an array).
In your case, the signature of your print() function should probably change to get an int board[][WIDTH] instead of int * which it gets now.
The change of the signature also requires a change in the function code, so the line
if (*(board + y*cols + x) == 1)
Should be changed to:
if (board[y][x] == 1)
void print(int *board, int rows, int cols)
...
int board[HEIGHT][WIDTH];
...
print(board, HEIGHT, WIDTH);
Your print function first parameter is of type int *board but you are calling the function with an argument of type int (*)[50].
You should fix your print function prototype (and body) to work with a parameter of the right type (i.e., int (*)[50]).
you declare
void print(int *board, int rows, int cols)
but in the the other functions you have used
foo(int board[][WIDTH], int rows)
either keep same or declare as
(int **board, int rows, int cols )
Your print function is expecting a pointer to int, but you are passing a 2-dimensional array, which decays to a pointer to a 1-dimensional array. Either cast board to an int* or convert print() to handle a 2-d array.
Casting the call would look something like this:
print((int *)board, HEIGHT, WIDTH);
Converting print() to take a 2D array as argument would look something like this:
void print(int board[][WIDTH])
{
int x, y;
char c;
for (y = 0; y < HEIGHT; y++) {
for (x = 0; x < WIDTH; x++) {
if (board[y][x] == 1)
printf("X");
else
printf(" ");
}
printf("\n");
}
printf("Press any key to continue:\n");
getchar();
}
why don't you just do like what sverre told you to, since a multidimensional array is really an abstraction the compiler provides not to do the maths you did to find the row_th and col_th element
just call it like print((int *)board, HEIGHT, WIDTH);
Your board is a double array. It has both height and width. When you expect a *board argument in your print you actually expect an array with only one dimension. For the error to go away you have to pass int (*board)[WIDTH].
When you pass a two-dimensional array to a function you have to include the size of one of the dimensions. So your board[HEIGHT][WIDTH] has to be passed with, at least, the information about it's width. So : board[][WIDTH]. You can take those empty [] and write them down as a pointer in front of board.
So, in your main you call your function correctly, but you have to edit your function's head to be like :
void print(int (*board)[WIDTH], int rows, int cols){
Related
I have the following typedef:
typedef struct cell_t {
int x; // x coord of this cell
int y; // y coord of this cell
int neighbour_count; // Size of the following array
struct cell_t **neighbours; // array of pointers to neighbours
} cell_t;
and some code to initialize this type (neighbour_count and neighbours are to be set later)
cell_t *create_cell(int x, int y){
cell_t *cell = malloc(sizeof(cell_t));
cell->x = x;
cell->y = y;
return cell;
}
I have myfun to initialize the neighbours and neighbour_count variables for a matrix of cell_t, which it reads from some external source.
int myfun(cell_t ***cells, int width, int height){
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
// Read data from source, including the long `list_size`
cells[x][y]->neighbour_count = (int) list_size; // This segfaults
}
}
}
So in my main, I have the following:
int width = 3, height = 3;
cell_t *cells[width][height];
for (int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
cells[x][y] = create_cell(x,y);
}
}
myfun(cells, width, height);
When setting the neighbour count, it segfaults (as marked in the third block of code).
What I think I'm doing is that in my main I initialize an empty matrix of pointers to cell objects, and then loop through width and height, creating cells, and storing the pointers to them in the matrix. Then in myfun, I'm simply accessing a the variable neighbour_count of these cells and setting it. But apparently I'm mistaken, as it segfaults (not always on the first loop though, but generally quite quickly).
I'm thinking perhaps I did something wrong such that the matrix cells does not actually contain pointers to cell_t structs. But I can't see what I'm doing wrong.
I am getting a warning that "passing argument 1 of ‘myfun’ from incompatible pointer type", and that it should instead be of type cell_t * (*)[(sizetype)(height)]; perhaps that helps? I would expect there not to be a problem, as struct_t *** should be good for a 2d array of pointers, no?
The type of the first function parameter
int myfun(cell_t ***cells, int width, int height){
and the type of the supplied argument
myfun(cells, width, height);
do not correspond each other. The function argument declared like
cell_t *cells[width][height];
is implicitly converted to pointer to its first element. That is it has the type cell_t * ( * )[height].
Thus the corresponding function should be declared like
int myfun( int width, int height, cell_t * cells[][height] ){
or like
int myfun( int width, int height, cell_t * ( *cells )[height] ){
that declares the same function.
One thing is not correct in your code that i recognize is the definition of pointer.
cell_t *cells[width][height];
This defifnition is not for pointer that points to an array 2D. Like #Lundin commented above, it is not compatible with cell_t ***cells. If you want to define a pointer that points to an array 2D, you can use the code below:
cell_t cells[width][height];
cell_t *ptr = &cells;
The pointer ptr is compatible with ***cells in your code
The intended result is the printing of the same values that we input, but the output is zero after the first row.
#include<stdio.h>
void display(int *q, int);
int main() {
int i,j,n;
int d[50][50];
printf("Input the order\t");
scanf("%d", &n);
for (i=0;i<=(n-1);i++) {
for (j=0;j<=(n-1);j++) {
scanf("%d", &d[i][j]);
}
}
display (d, n);
}
void display (int *q, int r) {
int i,j;
for (i=0;i<r;i++) {
for (j=0;j<r;j++) {
printf("%d\t", *(q + i*r + j));
}
printf("\n");
}
}
Your function is declared with a parameter of type int *. You are attempting to pass an argument of type int [50][50], which decays to pointer type int (*)[50]. These pointer types are incompatible. Language does not support implicit conversion from int (*)[50] to int. Your code is not a valid C program, and I'm sure your compiler told you about it.
A more meaningful thing to do would be to declare your function as
void display (int n, int q[n][n])
and access your array elements in a natural way as
q[i][j]
However, for that you will have to use true array sizes as n (i.e. 50 in your case). If you want to process a smaller sub-matrix of elements, you will have to pass its size separately.
But if you really want to use your "hack", you will have to use an explicit cast when calling your function
display ((int *) d, n);
and keep in mind that each row of your original array still contains 50 elements, as declared, regardless of the value of n. This means that inside your function you will have to use 50 as row size multiplier
void display (int *q, int r) {
int i,j;
for (i=0;i<r;i++) {
for (j=0;j<r;j++) {
printf("%d\t", *(q + i*50 + j));
}
printf("\n");
}
}
your display routine assumes that the 2D array is [n][n] whereas it is [50][50].
You have to pass the actual 2D array dimension alongside with n, the display routine cannot know the actual 2D array size.
You could declare your array dynamically like this:
printf("Input the order\t");
scanf("%d", &n);
int d[n][n];
and then avoid the warning
test.c:20:18: warning: passing argument 1 of 'display' from incompatible pointer type
display (d, n);
here by casting explicitly to 1D pointer (since you know the structure):
display ((int*)d, n);
I am currently doing the fifteen exercise in CS50's Problem set 3. However, I am stuck in figuring out the syntax to pass a multi-dimensional array as an argument to a function. For instance, the following code (which prints out some numbers in an array) compiles and works:
#include <stdio.h>
void func(int array[], int size);
int main()
{
int size = 3;
int array[3] = {1,2,3};
func(array,size);
printf("Done\n");
}
void func(int array[], int size)
{
printf("%i %i %i\n", array[0],array[1], array[2]);
}
But this doesn't:
#include <stdio.h>
void func(int array[][], int size);
int main()
{
int size = 3;
int array[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
func(array,size);
printf("Done");
}
void func(int array[][], int size)
{
printf("%i %i %i\n", array[0][0],array[1][1], array[2][2]);
}
This is the error provided by clang:
test.c:3:20: error: array has incomplete element type 'int []'
void func(int array[][], int size);
^
test.c:13:20: error: array has incomplete element type 'int []'
void func(int array[][], int size)
Can anyone explain to me what's wrong with my syntax? I don't quite understand the error messages given to me by clang.
The function func() expects a pointer to int but you are passing a pointer to an array. Hence, the errrors.
It's because an array gets converted into a pointer to its first element when you pass it to a function.
You can change the function prototype and definition to receive a pointer to an array:
void func(int (*array)[3], int size);
int main()
{
int size = 3;
int array[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
func(array,size);
printf("Done");
}
void func(int (*array)[3], int size) {
...
}
Note that your array is initialized with size 3x3. So the array size has to 3x3 at least.
C99 allows to you pass dimensions. So you can write it like this too:
void func(int x, int y, int a[x][y]);
int main()
{
int size = 3;
int array[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
func(3, 3, array);
printf("Done");
}
void func(int x, int y, int array[x][y])
{
printf("%i %i %i\n", array[0][0],array[1][1], array[2][2]);
}
An array is not a type in C! Internally, it is always handled as a pointer, hence, a pointer to the array is passed as argument and not the array itself.
If you intend to pass arrays as arguments (and not just pointers), you should rethink your design!
If you still need to do this wrap the array into a structure and pass the structure.
struct astruct
{
int size;
int array[3];
};
A declaration like int a[][] isn't sensible, since the compiler must know the size of all dimensions (except of the outermost) to calculate the memory address on access.
I.e. if an array is declared as
int array[][a][b];
The memory address for access to
array[2][1][3];
is calculated by
base_address + (2*a*b + 1*b + 3) * sizeof int
Without knowing a and/or b, this calculation would not be possible.
Simply tell the function to expect a multi-dimensional array:
void func (int array[3][3], int size);
Or if you want the function to be completely flexible:
void func (int x, int y, int array[x][y]);
You need to specify size of array when declaring prototype as well as while defining function . Right now it is of incomplete type .
Try this instead -
void func(int array[][3], int size)
I am trying to fill a 2d array in function drawInitialNim(), using values passed from main.
int drawInitialBoard(int x, int y, int z);
int main(int argc, const char * argv[]) {
int board[3][50] = {{0}};
int row1, row2, row3;
int i, j;
row1 = 0;
row2 = 0;
row3 = 0;
printf("Enter the number of rocks in each row: ");
scanf("%d %d %d", &row1, &row2, &row3); //values for amount of "O" in the array
printf("this is row 1: %d\n", row1); //debug
board[2][49] = drawInitialBoard(row1, row2, row3); //fill array from function
printf("this is 0,0 in main: %d\n",board[0][0]); // debug
for (i=0; i<3; i++) { //print indexes that contain "O" or 79
printf("Row %d: ", i+1);
for (j=0; j<50; j++) {
if (board[i][j] != 0) {
printf("%c", board[i][j]);
}
}
printf("\n");
}
}
int drawInitialBoard(int x, int y, int z)
{
int i,j,k;
static int arr[3][50] = {{0}};
for (i=0; i<x; i++) { //fill board with "O", or its that equal 79
arr[0][i] = 'O';
}
for (j=0; j<y; j++) {
arr[1][j] = 'O';
}
for (k=0; k<z; k++) {
arr[2][k] = 'O';
}
printf("This is 0,0 in the function: %d\n", arr[0][0]); //debug
return **arr;
}
I am getting this output, using 1, 2, 3 as input for x, y, z in main:
Row 1:
Row 2:
Row 3: O
I feel like the values from drawInitialNim() are not getting passed to main properly. I know that arrays can't be returned, so I returned the pointer. Additionally, I don't think that this is a dereferencing problem. I am not sure how to solve this.
Any help would be appreciated!
See the first thing I see incorrect in your program is:
You define the function like int drawInitialBoard(int x, int y, int z); which means that it will give back an int but you are returning **arr. Now according to what I know, * means "value at address of" so *arr would mean value at address of the pointer arr. And as arr is an array it is a pointer to its first element. So I think there is something wrong in there when you use return **arr. And as the first element in the array is not a pointer then I think that **arr would probably just give you not what you desire.
Your function is declaring an array, filling the rows according to the arguments passed, and then returning the value of arr[0][0], which in the case of the input values you state is 79 or 'O'. This value is then written to the main array board[2][49] and you then report correctly on its contents - all other elements are still 0. I don't see why you are using two arrays, not just one. And I don't see why you have declared an int array when you are treating it as a char array.
An easier way to make you program work would be, make your array board[3][50] global.
And, change the return type of your function drawInitialBoard(int x, int y, int z); to void, i.e., void drawInitialBoard(int x, int y, int z);.
And instead of working on a new array arr in the function, you can use the array defined previously board[3][50].
This won't clear the concept of passing the 2-D array, but will surely make it work.
I'm working on some C homework for class and I've been running into issues using arrays. Here is a sample of one of my functions that's having an error.
void multiply(int a, int size)
{
int i;
for(i = 0; i < size; i++){
a[i] = a[i] * 5;
printf("%d, ", a[i]);
}
printf("\n");
}
It returns the error: subscripted value is neither array nor pointer nor vector on lines 5 & 6 when I call for a[i]. I have a as an array with size 10, but each time I try and call an individual value in the array it doesn't want to work. I've tried searching it but none of the solutions really seems to work.
You should change your function to:
void multiply(int * a, int size)
Change your function header to:
void multiply(int* a, int size)
Othewise the function thinks a is an int not an int array