0

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;
}
3
  • 3
    What is the purpose of the program? Commented Oct 10, 2016 at 5:03
  • Pay attention, that expression const char * const str = (char*)ch; as well as const char * const str = ch; with uninitialized local variable ch assign unknown and WRONG value (address) to pointer str Commented Oct 10, 2016 at 5:18
  • 1
    Your code can work well with char *. Why did you use const char * const? Commented Oct 10, 2016 at 5:20

2 Answers 2

2
  • 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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.