I've been getting the following warning when trying to write a program that only allows strings of numbers:
warning: format ‘%s’ expects argument of type char *’, but argument 3 has type ‘char (*)[100]’
Why am I getting this warning, and what does it mean/consequences? Not had any problems running the code.
The program:
char check[100];
char line[100];
int i;
int true = 0;
int main
{
fgets(line, sizeof(line), stdin);
sscanf(line, "%s" , &check);
for(i=0; i<3; i++)
{
if(isdigit(check[i]) == 0)
{
true++;
}
else
continue;
}
if(true>0)
printf("Not a number.\n");
else
printf("Is a number.\n");
return(0);
}
The compiler is complaining about the use of &check in the following line.
sscanf(line, "%s" , &check);
The expected argument is just check.
sscanf(line, "%s" , check);
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char string;
printf("Hello\n");
printf("What would you like to do\n");
printf("Here are the options\n");
printf("s : How are you\n");
printf("c : What would you like to search\n");
scanf("%s",&string);
if(string == 'h')
printf("iam fine\n");
else if (string == 's')
printf("What would you like to search\n");
scanf("%s",&string);
system(string);
return 0;
}
When I run this after it says what would you like to search and I type run notepad it stops working.
There are two problems with this scanf:
printf("What would you like to search\n");
scanf("%s",&string);
system(string);
string is a single char - the scanf will result in a buffer overrun.
The %s format specifier only reads until the next whitespace.
To fix the problem you should allocate a larger buffer and read an entire line:
char buffer[1024];
printf("What would you like to search\n");
fgets(buffer, sizeof buffer, stdin);
system(buffer);
Problem 1. defining string as char won't work for you. you need an array.
define char string[100] = {0};
Problem 2. scanf("%s",&string); not required, can be used as scanf("%s",string);
problem 3. if(string == 'h'), wrong. array contents cannot be compared using == operator. you've to use strcmp() function.
11: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[100]’
This is my error....Cant fix it...
#include <stdio.h>
#include <string.h>
int main()
{
char *a;
char *count;
char str[100];
int i ;
printf("\n:enter string for Palindrome test\t");
scanf("%s", &str);
i= strlen(str);
//a=(char *)malloc(i*sizeof(char));
a=&str[0];
count=&str[i-1];
while((*(a++)==*(count--)) && i>=1)
{ i--; }
if(i==0) { printf("\n%s is a palin",str);}
else { printf("\n%s is not palin",str);}
}
Sure you can fix it. Just don't pass the wrong type. Change:
scanf("%s", &str);
To:
scanf("%s", str);
Or, equivalently:
scanf("%s", &str[0]);
You don't always use an ampersand passing arguments to scanf(). If the format string contains either a %c or a %s, it will be looking for a char* type. When you call it with &str, you are calling it with a char** type, or as the compiler says, a char*[100] (a pointer to an array of 100 chars).
I am new student in c language and I just come up with this. I code:
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[80];
printf("Enter sth: ");
char st1 = gets(str);
printf("Enter sth: ");
char st2 = gets(str);
if(strcpy(st1,st2))
printf("Same\n");
else
printf("Different\n");
return 0;
}
My goal is to check if the 2 strings i enter from the keyboard are the same. I compile it and i get some warnings:
hello.c: In function ‘main’:
hello.c:9:16: warning: initialization makes integer from pointer without a cast [enabled by default]
hello.c:12:16: warning: initialization makes integer from pointer without a cast [enabled by default]
hello.c:14:5: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [enabled by default]
/usr/include/string.h:128:14: note: expected ‘char * restrict’ but argument is of type ‘char’
hello.c:14:5: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [enabled by default]
/usr/include/string.h:128:14: note: expected ‘const char * restrict’ but argument is of type ‘char’
Enter sth: asd Enter sth: asd Output: Segmentation fault (core dumped) Segmentation Fault i saw that is an error when you want to access sth that it doesnt exist!
I search it a little here in Stackoverflow with similar questions and here but i dont understand why this code isnt working. Thank you!
You are treating an address of char variable as string and using strcpy instead of strcmp. This:
char st1 = gets(str);
char st2 = gets(str);
if(strcpy(st1,st2))
was meant to be:
char st1[255], st2[255];
scanf("%254s", st1);
scanf("%254s", st2);
if(strcmp(st1, st2) == 0)
if(strcpy(st1,st2))
^
|
strcmp
strcpy is for string copy, not for string compare.
To remove segmentation fault change char str1 to char *str and char str2 to char *str2.
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[80];
char str2[80];
printf("Enter sth: ");
char *st1 = gets(str1);
printf("Enter sth: ");
char *st2 = gets(str2);
if(!strcmp(st1,st2))
printf("Same\n");
else
printf("Different\n");
return 0;
}
You get the compile warnings because gets() returns char *, but you declare str1 and str2 as char.
You get the segmentation fault because it should be:
if(strcpy(st1,st2))
should be used with strcmp, I guess it's a typo because strcmp is in your question's tag :)
Note: Never use gets(), you can use fgets() instead.
char *st1 = fgets(str, 80, stdin);
you are trying to use strcmp but you using strcpy.
This code may help you.
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[80];
char str2[80];
printf("Enter sth: ");
gets(str1);
printf("Enter sth: ");
gets(str2);
if(!strcmp(str1,str2))
printf("Same\n");
else
printf("Different\n");
return 0;
}
Here is how to fix your code :
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[80];
char str2[80];
printf("Enter sth: ");
//notice that gets is not safe,
//if the line is too long (>79 char), you'll have a buffer overflow
gets(str);
printf("Enter sth: ");
gets(str2);
//strcmp instead of strcpy
if(strcmp(str,str2) == 0)
printf("Same\n");
else
printf("Different\n");
return 0;
}
#include <stdio.h>
#include <string.h>
main()
{
int i;
int *b, *z;
char name[30];
char vowel[5] = {'A', 'E', 'I', 'O', 'U'};
char consonants[23] = {'B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','W','X','Y','Z'};
printf ("input the string: ");
scanf ("%s", name);
printf ("The string is %s\n", name);
for (i=0; name[i]!='\0'; i++){
if
(b=strchr(vowel, name[i]) != NULL) {
printf ("The vowels are: %s\n", b); }
else if
(z=strchr(consonants, name[i]) != NULL) {
printf ("The consonants are: %s\n", z);
}
}
}
I am trying to find how many vowels and consonants in array. That's the only algorithm that our teacher showed us, but it doesn't work. Any one can point me to my mistakes?
I just did one more try, with all your advices,
#include <stdio.h>
#include <string.h>
int main()
{
int vow, cons, i;
char *s, *s1;
char name[30];
char vowel[6] = "AEIOU";
char consonants[21] = "BCDFGHJKLMNPQRSTVWXYZ";
printf ("input the string: ");
scanf ("%s", name);
printf ("The string is %s\n", name);
for (i=0; name[i]!='\0'; i++)
s = strchr(vowel, name[i]);
printf ("The vowels are: %s\n", s);
s1 =strchr(consonants, name[i])) {
printf ("The consonants are: %s\n", s1);
}
return 0;
}
This is how I changed it, with all your advices, what is my other problems? cause still dosen't work fine.
Thanks.
And this is my another version of program
#include <stdio.h>
#include <string.h>
int main()
{
int i;
int counter=0, counter2=0;
char *s;
char name[30];
char vowel[6] = "AEIOU";
char consonants[21] = "BCDFGHJKLMNPQRSTVWXYZ";
printf ("input the string: ");
scanf ("%s", name);
printf ("The string is %s\n", name);
for (i=0; name[i]!='\0'; i++) {
if (s = strchr(vowel, name[i])) {
counter++;
}
else if (s =strchr(consonants, name[i])) {
counter2++;
}
printf ("First counter is %d\n", counter);
printf ("The second counter is %d\n", counter2);
return 0;
}
}
I added counters to count quantity of vowels and consonants, still doesn't work.
strchr() is for searching in strings.
char vowel[] = "AEIOU";
char consonants[] = "BCDFGHJKLMNPQRSTVWXYZ";
#include< stdio.h>
int main()
{
int vowel=0,consonant=0;
printf ("input the string: ");
scanf ("%s", name);
printf ("The string is %s\n", name);
for(int i=0;name[i] !='\0';i++)
{
if( name[i] == 'A' || name[i] == 'E' || name[i] == 'I' || name[i] == 'O' || name[i] == 'U' )
{
vowel++;
}
else
consanant++;
}
printf("%d %d",vowel,consonant);
return 0;
}
When I compile this, I get the following messages:
$ gcc -Wall vc.c
vc.c:4:1: warning: return type defaults to ‘int’ [-Wreturn-type]
vc.c: In function ‘main’:
vc.c:17:8: warning: assignment makes pointer from integer without a cast [enabled by default]
vc.c:17:3: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
vc.c:18:4: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat]
vc.c:20:13: warning: assignment makes pointer from integer without a cast [enabled by default]
vc.c:20:3: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
vc.c:21:4: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat]
vc.c:24:1: warning: control reaches end of non-void function [-Wreturn-type]
So, start by making sure that your return type for main is 'int'
int main(){
and adding a return at the bottom of the function
return 0;
Thereafter, set b and z to be char *s, so that they match the return type of strchr
char *b, *z;
This will get rid of all the warnings.
$ gcc -Wall vc.c
$
Excellent. Now, when we run your program:
$ ./a.out
input the string: aaa
The string is aaa
Segmentation fault
"Segmentation fault" means you're running off the end of an array and reading memory you don't own. Now implement Ignacio Vazquez-Abrams' solution
char vowel[] = "AEIOU";
char consonants[] = "BCDFGHJKLMNPQRSTVWXYZ";
Now your program will run to completion.
$ ./a.out
input the string: AAA
The string is AAA
The vowels are: AEIOU
The vowels are: AEIOU
The vowels are: AEIOU
But it doesn't do much, does it?
So, if you're just trying to count how many vowels and consonants there are, you can just add an integer for each that increments every time the correct type is found and output them at the end:
printf("Vowels:\t%d\nConsonants:\t%d", vowelsFound, consonantsFound);
However, if you're trying to output them as lists, you're going to have much more data manipulation to do. Some links to check out:
Linux Man Page for printf
Linux Man Page for String functions
You have placed the return statement inside the for loop which is preventing it from scanning the entire name array.
When using strchr, you'll also need to convert the current loop character to uppercase for it to match properly since you have defined vowels in uppercase. To use toupper() you need to include ctype.h.
You also don't need to define consonants. What is not a vowel is a consonant.
Here's the code. I've tested it and it works:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
int i;
int counter=0, counter2=0;
char *s;
char name[30];
char vowel[6] = "AEIOU";
printf ("input the string: ");
scanf ("%s", name);
printf ("The string is %s\n", name);
for (i=0; name[i]!='\0'; i++) {
if (strchr(vowel, toupper(name[i])) != NULL) {
counter++;
}
else {
counter2++;
}
}
printf ("First counter is %d\n", counter);
printf ("The second counter is %d\n", counter2);
return 0;
}
Alternatively.
#include <stdio.h>
#include <string.h>
int main() {
int t [256];
int i,c;
int cntw = 0;
int cntc = 0;
const char * vowel="AEIOUaeiou";
const char * consonants="BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz";
memset(t,0,256);
while (*vowel) { t[*vowel] = 1; ++vowel;}
while (*consonants) { t[*consonants] = 2; ++consonants;}
printf ("Input the text: CTRL-D to end\n");
c = getchar();
while(c >=0) {
switch(t[c]) {
case 1: ++cntw; break;
case 2: ++cntc; break;
}
c=getchar();
}
printf ("Text has %d vowel%s and %d consonant%s\n",
cntw,(cntw>1)?"s":"",
cntc,(cntc>1)?"s":"");
return 0;
}