I'd like to be able to print out the value of a charcter in c as follows:
fprintf("%c", alphabet[val]);
, where alphabet is initialized as
for(i = 0; i < 26; i++){
alphabet[i] = i + 65;
}
However, this line gives the following errors:
encode.c: In function ‘main’:
encode.c:76:13: warning: passing argument 1 of ‘fprintf’ from incompatible pointer type [-Wincompatible-pointer-types]
fprintf("%c", alphabet[val]);
^
In file included from encode.c:1:0:
/usr/include/stdio.h:356:12: note: expected ‘FILE * restrict {aka struct _IO_FILE * restrict}’ but argument is of type ‘char *’
extern int fprintf (FILE *__restrict __stream,
^
encode.c:76:19: warning: passing argument 2 of ‘fprintf’ makes pointer from integer without a cast [-Wint-conversion]
fprintf("%c", alphabet[val]);
^
In file included from encode.c:1:0:
/usr/include/stdio.h:356:12: note: expected ‘const char * restrict’ but argument is of type ‘char’
extern int fprintf (FILE *__restrict __stream,
^
encode.c:76:5: warning: format not a string literal and no format arguments [-Wformat-security]
fprintf("%c", alphabet[val]);
^
How can I print out a character like this?
Check your code closely. For the statement
fprintf("%c", alphabet[val]);
where is the file pointer? It's missing. You have to provide the file pointer, to which you want to see the output, something like
fprintf(fp, "%c", alphabet[val]);
where, fp is the file pointer (of type FILE *), usually returned by fopen(), check the man page for more details on this.
In case, you want the prints to arrive on stdout, i.e., the standard output, use printf().
Related
I had written this c program for study purpose and I have found that
printf("%s",a); works but printf("%s",*a); does not works.
I had defined a like this char *a[]="how are you";
why *ais not pointing towards the string?
I am getting this error
test2.c: In function ‘main’:
test2.c:7:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf("%s \n",*a);
~^ ~~
%d
It is undefined behaviour because of %s printf interprets *a as an address, but it's actually not an address and if you treat it as address it points to some location that is read protected for your program. So,may be you get segmentation fault.
#include<stdio.h>
#include<curses.h>
#include<string.h>
void main()
{ const char *str1[20];
const char *str2[20];
int comp;
printf("Enter the first string:\n");
scanf("%s",&str1);
printf("Enter the second string:\n");
scanf("%s",&str2);
comp=strcmp(str1,str2);
}
This was compiled in gcc compiler 4.8 . A detailed explanation will be appreciated.
Error message:
strcmp.c: In function ‘main’:
strcmp.c:10:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘const char * (*)[20]’ [-Wformat=]
scanf("%s",&str1);
^
strcmp.c:12:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘const char * (*)[20]’ [-Wformat=]
scanf("%s",&str2);
^
strcmp.c:13:14: warning: passing argument 1 of ‘strcmp’ from incompatible pointer type [-Wincompatible-pointer-types]
comp=strcmp(str1,str2);
^~~~
In file included from strcmp.c:3:0:
/usr/include/string.h:140:12: note: expected ‘const char *’ but argument is of type ‘const char **’
extern int strcmp (const char *__s1, const char *__s2)
^~~~~~
strcmp.c:13:19: warning: passing argument 2 of ‘strcmp’ from incompatible pointer type [-Wincompatible-pointer-types]
comp=strcmp(str1,str2);
^~~~
In file included from strcmp.c:3:0:
/usr/include/string.h:140:12: note: expected ‘const char *’ but argument is of type ‘const char **’
extern int strcmp (const char *__s1, const char *__s2)
const char *str1[20];
Should be
char str1[20];
You want a mutable array of characters, not an array of pointers.
scanf("%s",&str1);
Should be:
scanf("%s",str1);
The scanf function needs the address to store the input in. That's equivalent to &str1[0] which is what str1 decays into.
Your declared arrays are of the wrong type and qualifier.
The first thing is the incorrect declarations of the string buffers. You want an array of characters, but const char* str[20] declares an array of const character pointers (that is why the compiler is talking about argument 2 with type const char * (*)[20] being the wrong type). You do not want const here either since you want the characters to be changeable. char str[20] declares a string of characters which is what you really want.
Another point in C coding is that the address of an array is the same as the name of an array, so &str1 and str1 mean the same thing.
Change const char *str1[20]; to char str1[20];,
and scanf("%s",&str1); to scanf("%s",&str1[0]);
Same with second string.
As it currently is, you're declaring, not an array of characters, but an array of pointers to characters.
You're not printing any value, so hard to know the result of the comparison.
You did not explain exactly what your code is supposed to do, but I guess that
const char *str1[20];
(the above declares an array of 20 pointers to constant strings, or zones of several char-s)
should probably be
char str1[256];
this declares a single string -which should be writable by the computer, since you read it from thje user- of at most 256 bytes, and you want it to be null-terminated
(I'm using 256, because 80 bytes is really not a lot - and 20 bytes is not enough -; you want UTF8 everywhere, and 80 bytes can be much less than 80 characters).
BTW
scanf("%s",&str1);
is poor taste (may you wanted scanf("%19s", str1);). I suggest to use fgets and code:
if (!fgets(str1, sizeof(str1)-1, stdin)) {
perror("fgets str1");
exit(EXIT_FAILURE);
}
(Hint, you should always test for successful input).
BTW, always improve your code to get no warnings, and yes you need to compile with gcc -Wall -g (all warnings and debug info) to be able to use the gdb debugger (which you need to learn).
I am new to C programming.I am trying to take input from a file line by line and print length of each line.
This is my code-
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char name[100];
printf("Enter file name\n");
scanf("%s",name);
FILE * input;
input=fopen(name,"r");
char line[100];
fscanf(input,"%s",line);
while(!feof(input))
{
printf("%d\n",strlen(line));
fscanf(input,"%s",line);
}
return 0;
}
This is my input file-
172.24.2.1
172.24.2.225
172.17.4.41
172.24.4.42
202.16.112.150
172.24.152.10
This is correctly printing the length of each line.But during compilation I get this warning-
In function ‘main’:
main.c:24:4: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘size_t’ [-Wformat=]
printf("%d\n",strlen(line));
So my question is why am I getting this warning although my output is correct and how can I fix this?
Thanks in advance.
The function strlen returns an object of type size_t (cf. ISO 9899:2011§7.24.6.3). You need to specify that the argument you pass to printf has type size_t, as opposed to int. You can do that by adding a z length modifier (cf. ISO 9899:2011§7.21.6.1/7) specifying an object of type size_t. You should also use the u formatting specifier as size_t is an unsigned type.
printf("%zu\n",strlen(line));
i have a token of type char* that i want to get the second element of. For example the tokens all consist of a 4 long course number like 1405, i want to grab the 2nd character or 4, and turn it into an interger. I use strcpy to add the string into a char string array in order to grab the 4 but i get some errors and im not quite sure how to fix them. here is my code
#include <stdio.h>
#include <string.h>
int search(struct id array[],char* tok);
int main(void)
{
char* token2 = "1405";
char* text[] = malloc(strlen(token2)+1);
strcpy(text,token2);
int i,number;
for(i=0;i<4;i++)
if(i==1)
{
number = atoi(token2[i]); }
printf("%d\n",number);
i get the following compilation errors
gcc structures_arrayof.c
structures_arrayof.c: In function ‘main’:
structures_arrayof.c:23:20: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default]
char* text[] = malloc(strlen(token2)+1);
^
structures_arrayof.c:23:5: error: invalid initializer
char* text[] = malloc(strlen(token2)+1);
^
structures_arrayof.c:24:5: warning: passing argument 1 of ‘strcpy’ from incompatible pointer type [enabled by default]
strcpy(text,token2);
^
In file included from structures_arrayof.c:5:0:
/usr/include/string.h:129:14: note: expected ‘char * __restrict__’ but argument is of type ‘char **’
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
atoi is for converting a string to integer. However, you want to convert a single character.
You can write:
number = token2[i] - '0';
This works because the digits '0' through '9' have consecutive character codes. There is no need to malloc or whatever.
To work with what you are doing....
This should clear up the first set of errors.
char* text = malloc(strlen(token2)+1);
You are need a pointer to a char string.
To do the the conversion, you need to null terminate the string.
token2[i+1]=0;
Then you can
number = atoi(token2[i]);
The first error:
You forgot to #include <stdlib.h> to get malloc.
The second error:
A string represented as a pointer to a char, so you just need
char *text = malloc(strlen(token2)+1);
Drop the brackets.
Alternatively, since size is known at compile time, you can declare it without malloc using char text[5];
The third error:
It warns you that you are using the wrong type of pointer. This should be fixed by changing the declaration as mentioned above.
The fourth error (not in your pasted compiler output):
As Matt McNabb pointed out, you should not be using atoi. Replace it with
number = token2[i] - '0'; This will subtract the ASCII vaule of 0, returning the integer value of the character.
An additional comment: I'd recommend changing the spacing/indent style of the if statement. Putting an opening brace on its own line and a closing brace on the same line as the last statement is very non-conventional, and it decreases readability.
I suggest you pick a standard style and try to stick to it. Personally, I like the Linux Kernel Coding Style, but feel free to choose whichever suits you.
Parser.h
enum { PLUS, MINUS, DIVIDE, MULTIPLY, NUMBER, END } type;
int token;
/* parsing functions */
void parse_token (void);
Parser.c
void get_token (void)
{
token++;
parse_token(); /* LINE 11 */
}
void parse_token (void) /* LINE 14 */
{
if ( strchr ("1234567890.", token) )
type = NUMBER;
else if ( strchr ("+", token) )
type = PLUS;
else if ( strchr ("-", token) )
type = MINUS;
else if ( strchr ("/", token) )
type = DIVIDE;
else if ( strchr ("*",token) )
type = MULTIPLY;
else if ( token == '\0' )
type = END;
else
show_error(strcat("Couldn't parse token : ", token));
}
The Errors
parser.c:14:6: warning: conflicting types for ‘parse_token’ [enabled by default]
parser.c:11:2: note: previous implicit declaration of ‘parse_token’ was here
parser.c: In function ‘parse_token’:
parser.c:16:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:17:3: error: ‘type’ undeclared (first use in this function)
parser.c:17:3: note: each undeclared identifier is reported only once for each function it appears in
parser.c:17:10: error: ‘NUMBER’ undeclared (first use in this function)
parser.c:19:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:20:10: error: ‘PLUS’ undeclared (first use in this function)
parser.c:22:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:23:10: error: ‘MINUS’ undeclared (first use in this function)
parser.c:25:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:26:10: error: ‘DIVIDE’ undeclared (first use in this function)
parser.c:28:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:29:10: error: ‘MULTIPLY’ undeclared (first use in this function)
parser.c:32:10: error: ‘END’ undeclared (first use in this function)
parser.c: In function ‘show_error’:
parser.c:40:2: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
I'm utterly bamboozled. :(.
Any help?
One you get it to compile (by including the header, as Luchian Grigore said), you'll find that you can't do strcat() on a constant string.
The constant string is allocated in read-only memory, and can't be modified. And even if you could modify it, you would be overwriting other things in memory.
You're not including your header, so there's no way for the translation unit to know about the declarations of type and token.
You need:
#include "Parser.h"
at the beginning of the implementation file.