getting an error about char* in using strcmp(char*,char*) - c

I'm getting this error:
$ gcc -Wall -g translate.c support.c scanner.c -o translate
support.c: In function ‘translate’:
support.c:148:13: warning: passing argument 1 of ‘strcmp’ from incompatible pointer type [enabled by default]
compareNum = strcmp(dict[i], token);
^
In file included from /usr/include/stdio.h:29:0,
from support.c:1:
/usr/include/string.h:28:6: note: expected ‘const char *’ but argument is of type ‘char **’
int _EXFUN(strcmp,(const char *, const char *));
^
and here is the function translate()
int
translate(char* token, char** dict[], int dictLength)
{
int i = 0;
int compareNum;
for(i=0;i<dictLength;i++)
{
if(i % 2 == 0)
{
compareNum = strcmp(dict[i], token);
++i;
if(compareNum == 1)
{
return i;
}
}
}
return 0;
}
for some reason I'm passing in dict[i], which is an array of strings that I'm trying to compare each even element of the array to the string token but its saying its char**. I know the array is char** but wouldn't the element be char *?

The dict argument is declared like so:
char** dict[]
So dict[i] is of type char**. Hence the error.
I guess that in order for us to offer further advice we'd need to have some details of the object that you supply as the dict argument when calling this function. Perhaps you simply need to change the declaration of the argument to be:
char* dict[]
One thing that I would recommend strongly would be to use const when declaring these parameters, to allow you to pass non-modifiable strings.

You have dict parameter declared as:
char** dict[]
Which is an array of char**.
This means dict[index] is a char**
Changing the parameter
char** dict[]
to
char* dict[]
should fix your problem

Related

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.

Cannot Use Printf!?! (while performing hashing algorithm using mhash)

I am using Mhash and I would like to print the length of my blocksize for debugging purposes, but I keep getting an error every time I try to compile
Any suggestions on how I can fix this error?
Here is my code:
#include <mhash.h>
#include <stdio.h>
#include <string.h>
// 0x12e6bc6e68c3b9506e6668db6b7224f894fab073728fc179 (TIGER192) (48)
int main()
{
char password[] = "Jefe";
int keylen = 4;
char data[] = "what do ya want for nothing?";
int datalen = 28;
MHASH td, td2;
unsigned char *mac, *mac2;
int i, j;
td = mhash_hmac_init(MHASH_TIGER192, password, keylen, mhash_get_hash_pblock(MHASH_TIGER192));
mhash(td, data, datalen);
mac = mhash_hmac_end(td);
printf("0x");
for (i = 0; i < mhash_get_block_size(MHASH_TIGER192); i++)
{
printf("%.2x", mac[i]);
}
printf("\n");
// int length = strlen(mac);
// printf(length);
// int length = 5;
// printf(length);
exit(0);
}
I run the program with the following commands:
hb2#hb1:~/Desktop$ gcc -o hashexample hashexample.c -lmhash
hb2#hb1:~/Desktop$ ./hashexample
0x12e6bc6e68c3b9506e6668db6b7224f894fab073728fc179
And it runs successfully, but when I try to print the length of the hashed result, I get the following error!!? Any ideas on why?
// int length = strlen(mac);
// printf(length);
hb2#hb1:~/Desktop$ gcc -o hashexample hashexample.c -lmhash
hashexample.c: In function ‘main’:
hashexample.c:33:2: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default]
/usr/include/stdio.h:363:12: note: expected ‘const char * __restrict__’ but argument is of type ‘int’
hashexample.c:33:2: warning: format not a string literal and no format arguments [-Wformat-security]
At first, I thought it was because I thought I was using strlen incorrectly?! But even when I try to do a simple printf of an integer, I still get an error:
// int length = 5;
// printf(length);
hb2#hb1:~/Desktop$ gcc -o hashexample hashexample.c -lmhash
hashexample.c: In function ‘main’:
hashexample.c:35:2: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default]
/usr/include/stdio.h:363:12: note: expected ‘const char * __restrict__’ but argument is of type ‘int’
hashexample.c:35:2: warning: format not a string literal and no format arguments [-Wformat-security]
Thanks for your help in advance!
Check the man page for printf(). The first argument is a const char *. You're passing an int.
That's what the warning says too:
warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default]
You wanted:
printf("%d", length);
You need the format string to specify that an int is going to be printed.

Trying to print a simple string: [Warning] conflicting types for 'rawrprint' [enabled by default]

When I try to run the following program, it prints out the string but I still receive a warning:
18 6 C:\Users\Starhowl\Documents\C problems\consoleproj\main.c [Warning] conflicting types for 'rawrprint' [enabled by default]
#include <stdio.h>
#include <stdlib.h>
char *text = "my test";
char *texta[] = {"array test", "mwrawr"};
int main(int argc, char *argv[]){
rawrprint(texta);
return 0;
}
void rawrprint(char *hurr){
printf("%s", hurr);
}
That texta is an array of pointer to char, and your rawrprint() requires its arguments has type of pointer to char. You should call it using texta[0] or texta[1] as its argument, like this:
rawrprint(texta[0]);
Additionally, you had better declare a function before you call it, so that compiler can check the types of argument(s) and return value, and issue warnings if you call it improperly.

Error incompatible pointer type?

#include<stdio.h>
#include<string.h>
char getInput(char *x[50]);
main (){
char string[50];
getInput(&string);
}
char getInput(char *x[50]){
printf("What is the string?");
gets(*x);
}
I keep getting these errors...
exer7.c:20:2: warning: passing argument 1 of ‘getInput’ from incompatible pointer type [enabled by default]
getInput(&string);
^
exer7.c:5:6: note: expected ‘char *’ but argument is of type ‘char ()[50]’
char getInput(char *x[50]);
I've been changing the pointers and ampersands but I really don't know the proper pointer type, pls help :(
BTW, that's just a code snippet, I have many other user-declared functions I don't need to post here.
void getInput(char (*x)[50]);
int main (){
char string[50];
getInput(&string);
return 0;
}
char *x[50] declares x as array of pointers.&string` is of type pointer to an array. Both types are incompatible.
Change your function declaration to
char getInput(char x[50]);
and call it as
getInput(string);
getInput(&string); You shouldn't pass & of string. Just base address string of the char array need to be passed as the argument.
char getInput(char *x[50]); This formal argument isn't correct too. This should be a pointer to char or array of char of 50 bytes.

Function return type and pointers

I'm having some trouble with function return types and pointers (sorry if this a stupid question but haven't programmed C in quite some time). When I to run this:
#include <stdio.h>
#include <string.h>
#include <strings.h>
char *parse(char const *data) {
return data;
}
int main(int argc, const char *argv[]) {
char *a = "# Test";
char newString = parse(a);
printf("%s\n", &newString);
return 0;
}
I get an error:
writer.c: In function ‘parse’: writer.c:6: warning: return from incompatible pointer type
writer.c:6: warning: function returns address of local variable writer.c: In function ‘main’:
writer.c:11: warning: initialization makes integer from pointer without a cast
Any ideas?
parse(a) returns char*, and newString is type of char so they are incompatible.
Change this:
char newString = parse(a);
printf("%s\n", &newString);
to this:
char *newString = parse(a);
printf("%s\n", newString);
Regarding your errors / warnings:
writer.c: In function ‘parse’:
writer.c:6: warning: return from
incompatible pointer type
You return const char* while your function return type is char*
writer.c:6: warning: function returns
address of local variable writer.c: In
function
data is considered a local variable and you return this address, which might be dangerous if it was defined inside the function.
‘main’: writer.c:11: warning:
initialization makes integer from
pointer without a cast
You assign char* to a char.
And as a side note: when you use a char pointer to a constant string, make it const: const char *a = "# Test";

Resources