Reputation: 9
On const char * const str = ch;
there is a warning:initialization makes pointer from integer without a cast
.
If I change it to const char * const str = (char*)ch
the warning will be cast to pointer from integer of different size
.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv){
FILE *file1;
char ch;
char array[100];
file1 = fopen("list.txt","r");
while((ch=fgetc(file1))!=EOF)
{
const char * const str = (char*)ch;
const char * const delim = "\n";
char * const dupstr = strdup(str);
char *saveptr = NULL;
char *substr = NULL;
int count = 0;
printf("original string is %s",dupstr);
substr = strtok_r(dupstr, delim, &saveptr);
do{
printf("#%d filename is %s\n", count++, substr);
substr = strtok_r(NULL, delim, &saveptr);
}
while(substr);
free (dupstr);
return 0;
}
fclose(file1);
return 0;
}
Upvotes: 0
Views: 7687
Reputation: 36401
ch
is a char
(an integral type) and you try to convert it to a pointer type char *
(a type that can store an address). These two types are of very different nature, and it is forbidden by the standard to do such. At least convert the address of ch
: (char *)&ch
.
Beware this will not save your code as you are trying to use a char
as a C-string. Again these are of different kind. A char
is just something that let you store the code value of a character. A C-string is a sequence of characters terminated by a NUL one.
Suggestions (we don't really know what you try to achieve) : use an array of chars, read a full line from your opened file with fgets
, etc.
Upvotes: 1
Reputation: 213720
ch=fgetc(file1))!=EOF
is incorrect because ch is a char
, but EOF
is an int
. This is the very reason why fgetc
and similar functions return an int
. Easiest way to fix the current code is probably to use a temporary int
, then copy that to a char
inside the loop.
const char * const str = (char*)ch;
. Casting from a character to a pointer doesn't make any sense. This is the reason for the warning. If you want to create a temporary string consisting of one character, you should do something like char str[2] = {ch, '\0'}
. That way you don't have to use strdup
either.
Upvotes: 2