Fix incompatible-pointer-types warning - c

Bonjour, I keep getting this warning, and I've tried everything to fix the warning w/o any result. The program still works, but the warning is annoying and I'd love to know the "why".
It's a table, takes input from the user for a position in the table, and uses that position in the function encuentRuta.
This is (part of) the code:
char encuentRuta (int posv, int posh, char lab[TAMV][TAMH]);
int main (int argc, char *argv[]) {
int altura = atoi(argv[1]);
int base = atoi(argv[2]);
/* Define the table */
char laberinto[TAMV][TAMH] = {LABERINTO};
encuentRuta (altura, base, &laberinto);
And the error I keep getting is:
> warning: incompatible pointer types passing 'char (*)[8][12]'
> to parameter of type 'char (*)[12]' [-Wincompatible-pointer-types]
> encuentRuta (altura, base, &laberinto);
> ^~~~~~~~~~
> laberinto.c:16:44: note: passing argument to parameter 'lab' here
> char encuentRuta (int posv, int posh, char lab[TAMV][TAMH]);
Thanks for the help.

Omit the & before laberinto in the call to encuentRuta().
The message does its best to explain it to you. If you take the address of a 2D array (&laberinto), you get a pointer to an array, which is written SomeType (*)[size1][size2], or char (*)[8][12] in your example. To pass an array, you simply name it:
encuentRuta(altura, base, laberinto);

Related

error: incompatible integer to pointer conversion assigning to 'string' (aka 'char *') from 'int' C

This is incomplete code but I'm trying to make copies of argv[1] that are uppercase and lowercase, but get an error message. If it's an inefficient way to go about it in general or completely wrong I would appreciate any tips, but is there a way to fix the error in this specific case?
I'm extremely new to coding as you can tell by the question, so I'm sorry if it's a stupid one, but where might the error be occurring? I realize somehow argv[1] is being converted into an integer but I neither know where nor how to really fix it.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
string keyu[26];
string keyl[26];
string key = argv[1];
for (int u = 0; u < strlen(key); u++)
{
keyu[u] = toupper(key[u]);
}
for (int l = 0; l < strlen(key); l++)
{
keyl[l] = tolower(key[l]);
}
The error it gives out is:
14:17: error: incompatible integer to pointer conversion assigning to 'string' (aka 'char *') from 'int' [-Werror,-Wint-conversion]
keyu[u] = toupper(key[u]);
These declarations
string keyu[26];
string keyl[26];
are equivalent to
char * keyu[26];
char * keyl[26];
That is they are arrays of pointers.
This in these statements
keyu[u] = toupper(key[u]);
keyl[l] = tolower(key[l]);
you are trying to assign an integer (as for example toupper(key[u])) to a pointer (as for example keyu[u])
It seems you want actually to declare arrays of characters
char keyu[26];
char keyl[26];
Pay attention to that you should append the arrays with the terminating zero character '\0' after the for loops to guarantee that the arrays contain strings.

Passing 2D array of structs

I'm having difficulty passing a 2D array of structs. The size of the 2D array is dynamic (depends on given input parameters). I get the error:
maze_array.c:76:14: error: incompatible types when assigning to type ‘BlockNode {aka struct BlockNode}’ from type ‘BlockNode * {aka struct BlockNode *}’
Maze[i][j]=myBlockNode;
Here is my code:
int main(int argc, char *argv[]){
int MazeWidth=1;
int MazeHeight=1;
int NumOfAvatars=1;
BlockNode* Maze[MazeWidth][MazeHeight];
InitializeArray(Maze[0],MazeWidth,MazeHeight,NumOfAvatars);
return 1;
}
int InitializeArray(BlockNode** Maze,int MazeWidth, int MazeHeight, int NumOfAvatars){
for (int i=0; i<MazeWidth;i++)
{
for (int j=0; j<MazeHeight;j++)
{
//Initialize a BlockNode
printf("HERE1\n");
BlockNode *myBlockNode;
myBlockNode=calloc(1,sizeof(BlockNode));
myBlockNode->North=0;
myBlockNode->South=0;
myBlockNode->East=0;
myBlockNode->West=0;
int myArray[NumOfAvatars];
memset(myArray,0,sizeof(myArray));
memcpy(myBlockNode->AvatarVisited,myArray,sizeof(myArray));
//Place BlockNode in the Maze
Maze[i][j]=myBlockNode;
}
}
/*
printf("North %d\n", Maze[0][0]->North);
printf("AvatarVisted %d\n", Maze[0][0]->AvatarVisited[0]);
*/
return 1;
}
You should take into account that a 2D array is not equal to pointer to pointer, for example, if you try to compile...
int array[10][10];
int** p=array;
...you would get a similar error.
If you want to pass a 2D array of pointers and use it like AnArray[i][j] = something, you should change the function declaration to...
int InitializeArray( BlockNode* Maze[][MazeHeight]
, int MazeWidth
, int MazeHeight
, int NumOfAvatars )
...or...
int InitializeArray( BlockNode* (*Maze)[MazeHeight]
, int MazeWidth
, int MazeHeight
, int NumOfAvatars )
...and call it like...
InitializeArray( Maze
, MazeWidth
, MazeHeight
, NumOfAvatars );
Also, read this.
You haven't actually asked a question, but I will assume you are seeking an explanation of the compiler error.
Within your function, Maze[i][j] will be of type BlockNode, since it is dereferencing a BlockNode ** (pointer to a pointer to a Blocknode) twice. myBlockNode is of type BlockNode *. That is the cause of the compiler error - there is no valid assignment of the form some_BlockNode = some_pointer_to_BlockNode.
The real problem, however, is that you don't properly understand the difference between pointers and arrays. You'll need to read up on such topics before anyone will be able to offer useful (that will make sense to you) advice on how to do what you want.

Incompatible pointer type - Trying to modify array from another function

I have an array:
char gameBoard[6][6];
// Initilize the array
for(int i = 0; i < 6; i++) {
for(int o = 0;o < 6;o++) {
gameBoard[i][o] = ' ';
}
}
Later in my code I have a function I am trying to use that will modify this array:
void placePiece(char piece, char *gameBoard) {
int x = 0;
int y = 0;
gameBoard[posXInArray][posYInArray] = piece;
}
I am calling the function from the same scope as where the gameBoard array is created and initialized.
placePiece('X', gameBoard);
The warning I get doing this:
warning: passing argument 2 of ‘placePiece’ from incompatible pointer type [enabled by default]
placePiece('X', gameBoard);
^
note: expected ‘char *’ but argument is of type ‘char (*)[6]’
void placePiece(char piece, char *gameBoard);
I am confused as to what I am doing wrong? I assumed to modify the array from another function I would pass the pointer so that I could directly modify the array. Is this incorrect? I have also tried &gameBoard in the call with the same warning message. I'm guessing I am doing something silly that is incorrect?
You can't pass the 2-dimensional array to this function
void placePiece(char piece, char *gameBoard)
I think this would work fine for you
void placePiece(char piece, unsigned int size, char gameBoard[size][size])
you can use it this way
placePiesce('X', 6, gameBoard);
or better
placePiesce('X', sizeof(gameBoard) / sizeof(gameBoard[0]), gameBoard);
and since the type of gameBoard[0] is char this will also work
placePiesce('X', sizeof(gameBoard), gameBoard);
because sizeof(gameBoard[0]) == 1 in that case.

Converting string to long using strtol and pointers

My goal is to convert a string such as "A1234" to a long with value 1234. My first step was to just convert "1234" to a long, and that works as expected:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
char* test = "1234";
long val = strtol(test,NULL,10);
char output[20];
sprintf(output,"Value: %Ld",val);
printf("%s\r\n",output);
return 0;
}
Now I am having trouble with pointers and trying to ignore the A at the beginning of the string. I have tried char* test = "A1234"; long val = strtol(test[1],NULL,10); however that crashes the program.
How do I set this up properly to get it pointing to the correct spot?
You are almost right. You need to pass a pointer to strtol, though:
long val = strtol(&test[1], NULL, 10);
or
long val = strtol(test + 1, NULL, 10);
Turning on some compiler warning flags would have told you your problem. For example, from clang (even with no special flags added):
example.c:6:23: warning: incompatible integer to pointer conversion passing
'char' to parameter of type 'const char *'; take the address with &
[-Wint-conversion]
long val = strtol(test[1],NULL,10);
^~~~~~~
&
/usr/include/stdlib.h:181:26: note: passing argument to parameter here
long strtol(const char *, char **, int);
^
1 warning generated.
and from GCC:
example.c: In function ‘main’:
example.c:6: warning: passing argument 1 of ‘strtol’ makes pointer from integer
without a cast
Editorial note: I think you can see from these error messages why beginners are often well-advised to use clang rather than GCC.

Passing arrays into functions

hi I'm attempting to create a program that accepts a 7 element array as an argument and returns the third through fifth element of that array to a smaller array however i'm currently getting this error
assign8p7.c: In function 'main':
assign8p7.c:18:2: warning: passing argument 1 of 'copysect' makes pointer from
integer without a cast [enabled by default]
assign8p7.c:3:6: note: expected 'int *' but argument is of type 'int'
from what i can tell the warning has a problem with me passing it an array in the arguments does anyone know how i might fix this? also any other advice for my code is welcome.
#include <stdio.h>
int *copysect(int ar[],int start,int end)
{
int i;
static int retar[3];
for(i = 0; i<3;i++)
{
retar[i+start]=ar[i+start];
}
return retar;
}
int main(int argc, char const *argv[])
{
int arry[7] = {1,2,3,4,5,6,7};
int miniarry[3];
miniarry[0] = *copysect(arry[0],3,5);
return 0;
}
int *copysect(int ar[],int start,int end)
Okay, copysect takes as its first parameter an array of integers.
miniarry[0] = *copysect(arry[0],3,5);
Oops, you passed it a single integer instead of an array.
You are calling the function copysect with the first element in the array, not the pointer to the array. The correct call is:
copysect(arry,3,5);
You could calculate the difference of the array dynamically. Now the caller of copysect function has to know that the difference between start and end is 2.
int retar[end - start + 1]
The assignment in the for loop is wrong. You are dereferencing a value that is out of scope of retar array
retar[i]=ar[i+start];
When calling the copysect function, you are assigning only the first element in the miniarry by dereferencing the array that the function returns, instead of the whole array.
It's not the best idea to have a static array in a function (that would be problematic if you called the function more than once, etc). Instead, you could declare the smaller array elswhere and pass it as a parameter to the function.
void copysect(int ar[], int retar[], int start,int end, )

Resources