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.
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;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3][4];
for (int i=0;i<3;i++){
for(int j=0;j<4;j++){
printf("a[%d][%d]:",i+1,j+1);
scanf("%d",a[i][j]);
}
}
return 0;
}
The program stops in the third line and doesn't execute the next lines
Did you see the compiler warning in your scanf function:
warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’
So, using
scanf("%d",&a[i][j]);
instead of
scanf("%d",a[i][j]);
This happens because scanf receives an address (pointer) to variable, and not the variable itself. adding & before variable name will refer to it's address
I am at the very initial stage of learning programming. I working with a program that uses self-made function. I don't understand my mistakes. I would be grateful for your help. Please, do me a favor and answer using methods that are commensurate with the primitive stage I am at. I am leaving comments I've written, so you can see what I am trying to achieve by this or that code line.
/* Prints a user's name */
#include <stdio.h>
#include <string.h>
// prototype
void PrintName(char name);
/* this is a hint for the C saying that this function will be later specified */
int main(void)
{
char name[50];
printf("Your name: ");
scanf ("%49s", name); /* limit the number of characters to 50 */
PrintName(name);
}
// Says hello to someone by name
void PrintName(char name)
{
printf("hello, %s\n", name);
}
I get these error messages:
function0.c: In function ‘main’:
function0.c:14: warning: passing argument 1 of ‘PrintName’ makes integer from pointer without a cast
function0.c: In function ‘PrintName’:
function0.c:21: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
function0.c:21: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
The function PrintName is based on the previous program I took from the course (and adopted it to C):
#include <stdio.h>
#include <string.h>
int main (void)
{
char name[40];
printf ("Type a name: ");
scanf ("%39s", name);
printf ("%s", name);
printf("\n");
}
This last program works perfectly.
What mistake do I make in my original program? If I understand correctly, there is a problem with my PrintName function.
The initial program that prints a name is a modified version of CS50 program that uses CS50 library:
// Prints a user's name
#include <stdio.h>
#include <cs50.h>
// prototype
void PrintName(string name);
int main(void)
{
printf("Your name: ");
string s = GetString(); //GetString is the same as scanf; it takes input from the user
PrintName(s);
}
// Says hello to someone by name
void PrintName(string name)
{
printf("hello, %s\n", name);
}
Given "string" is "char" in C, I replace string with char in my program.
Thank you!
You should use char* instead of char, as an argument of your function. Char is one symbol, while char* is pointer to a string.
Your function PrintName is waiting for a char as parameter but you give a char[] that's why you see this :
warning: passing argument 1 of ‘PrintName’ makes integer from pointer without a cast
To give a char[] as parameter you need to change your function like this :
void PrintName(char *name);
Change void PrintName (char name); to void PrintName (char *name); or to void PrintName (char name[]);
Currently this function receives one character called name. You want it to receive an array of chars.
void PrintName(char name);
You function expects a character variable to be passed but instead you pass a char array. Thus it leads to error.
Moreover, %s in printf will expect a char * and you pass a char,thus leading to another problem.
To correct you program declare and define function with parameter as follows-
void PrintName(char *name);
or
void PrintName(char name[]);
Both will work.
So I am writing a simple code to print out each symbol in my string. When compiling it, it gives me an error tough that I do not understand:
The code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void) {
char my_string[50];
int i, n;
printf("Type in a string please : ");
scanf("%s", &my_string);
n = strlen(my_string);
for (i = 0;i < n; i++) {
printf("%c",my_string[i]);
}
}
The error it gives:
gcc yl2.c -o Yl2
yl2.c: In function ‘main’:
yl2.c:9:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[50]’ [-Wformat=]
scanf("%s", &my_string);
^
What is the problem here?
scanf("%s", &my_string); there should not be a &.
Since you have declared char my_string[50]; my_string as character array which is of type char * which is expecting in scanf() as the warning states.
Just use, scanf("%s", my_string);. Base address of the array as argument is sufficient.
scanf("%s", &my_string); should be scanf("%s", my_string);
my_string is an array, which decays to a pointer when you type it out without [ ].
If you type &my_string, you get an array pointer, which is strictly speaking not the same thing. That's why the compiler complains.
&my_string is a pointer to my_string i.e. a ‘char (*)[50]’
You can either use
scanf("%s", &my_string[0]);
or more conventionally
scanf("%s", my_string);
remove & sign in scan, it will work
Also no need to use string.h here
#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.