#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.
Related
here is the code
#include <stdio.h>
#include <string.h>
int main()
{
int val;
struct info{
char model[50];
int price;
char color[30];
}car[11];
int i;
for(i=0;i<11;i++)
{
printf("Enter model name:\n");
scanf("%s",&car[i].model);
printf("Enter price:\n");
scanf("%d",&car[i].price);
printf("Enter color:\n");
scanf("%s",&car[i].color);
}
printf("\nThe red cars are:\n");
for(i=0;i<11;i++)
{
val=strcmp("red",tolower(car[i].color));
if(0==val)
{
printf("%d. %s\n",i+1,car[i].model);
}
}
return 0;
}
also I tried using gets for string input but it doesn't seem to work.
Here are the warnings:
*main.c:17:17: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[50]’ [-Wformat=]
scanf("%s",&car[i].model);
main.c:21:17: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[30]’ [-Wformat=]
scanf("%s",&car[i].color);
main.c:26:34: warning: passing argument 1 of ‘tolower’ makes integer from pointer without a cast [-Wint-conversion]
val=strcmp("red",tolower(car[i].color));
In file included from main.c:4:0:
/usr/include/ctype.h:124:12: note: expected ‘int’ but argument is of type ‘char *’
extern int tolower (int __c) __THROW;
main.c:26:26: warning: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]
val=strcmp("red",tolower(car[i].color));
In file included from main.c:2:0:
/usr/include/string.h:144:12: note: expected ‘const char *’ but argument is of type ‘int’
extern int strcmp (const char *__s1, const char *__s2)
You posted your compiler output. Good. Let's look at all those errors:
main.c:17:17: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[50]’
Most of the time, when you call scanf, you need & on the variable being stored, but %s is an exception. (Explanation elsewhere.) Get rid of the &: scanf("%s",car[i].model);
main.c:26:34: warning: passing argument 1 of ‘tolower’ makes integer from pointer without a cast
This is your main problem. Your program as written will never work. tolower expects a single character to convert, but you're passing it a pointer instead. (You're passing it a pointer to the entire string you want to convert.)
/usr/include/ctype.h:124:12: note: expected ‘int’ but argument is of type ‘char *’
This is another message explaining the tolower problem.
main.c:26:26: warning: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast
tolower returns the single character it has converted. But strcmp expects an entire string.
/usr/include/string.h:144:12: note: expected ‘const char *’ but argument is of type ‘int’
This is another message explaining the tolower/strcmp problem.
How to fix this? There is not a standard function (that I can remember) that converts an entire string to lowercase. You'd have to write that yourself. Another option is to use a version of strcmp that compares the strings without regard to case. Two such functions (neither of which is quite standard, however) are strcasecmp and stricmp.
tolower only works with char not string, so use the function on every char of the string.
#include <string.h>
#include <stdio.h>
#include <ctype.h>
int strcicmp(char const *a, char const *b)
{
if (!a && !b)
return 0
else if (!a || !b)
return -1
for (;; a++, b++) {
int d = tolower((unsigned char)*a) - tolower((unsigned char)*b);
if (d != 0 || !*a)
return d;
}
}
int main()
{
int val;
struct info{
char model[50];
int price;
char color[30];
}car[11];
int i;
for(i=0;i<11;i++)
{
printf("Enter model name:\n");
scanf("%s",&car[i].model);
printf("Enter price:\n");
scanf("%d",&car[i].price);
printf("Enter color:\n");
scanf("%s",&car[i].color);
}
printf("\nThe red cars are:\n");
for(i=0;i<11;i++)
{
val=strcicmp(car[i].color, "red");
if(0==val)
{
printf("%d. %s\n",i+1,car[i].model);
}
}
return 0;
}
I'm trying to read a string and print it in Linux using:
cc child.c -o child
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
char st[100];
scanf("%s", &st);
printf("%s", st);
return 0;
}
However I encounter this warning that will not allow me to compile.
child.c: In function ‘main’:
child.c:8:2: warning: format ‘%s’ expects argument of type ‘char ’, but argument 2 has type ‘char ()[100]’ [-Wformat=]
scanf("%s", &st);
How can I solve it? Thanks!
In C, when passed to a function, array names converted to pointer to the first element of array. No need to place & before st. Change
scanf("%s", &st);
to
scanf("%s", st);
In C array types degenerate to pointers. So when you take the address of an array, you actually get a pointer to a pointer. Just pass the array itself to scanf.
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
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.
Hallo All,
I have this method:
void *readFileLocal(char filename[]){
.....
}
Now i want to start this method a a thread:
char input[strlen(argv[1])];
strcpy(input,argv[1]);
pthread_t read,write;
pthread_create(&read, NULL, &readFileLocal, &input);
But during compilation it gives this warning:
file.c:29: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(char *)’
How can I parse an char array to my funcation over pthread_create without this warning ?
Thanks for helpt
Just use this:
pthread_create(&read, NULL, readFileLocal, input);
And consider changing your function's signature to:
void *readFileLocal(void *fileName) { }
When you are passing a pointer to function (like the one you are using in readFileLocal parameter) you don't need to put &.
Also, when you have an array (like input in your case) you don't need & since in C arrays can be used as pointers already.
Functions for threads need to be prototyped:
void *func(void *argv);
As with all void pointers you then need to interpret ("cast") the pointer to a meaningful entity. You readFileLocal functions then becomes:
void *readFileLocal(void *argv)
{
char *fname = argv; // Cast to string
// Rest of func
}