Share via


C language; warning C4047: 'initializing' : 'char *' differs in levels of indirection from 'int'

Question

Thursday, March 22, 2012 11:37 PM

Re.c(93) : warning C4047: 'initializing' : 'char *' differs in levels of indirec
tion from 'int'
Re.c(128) : warning C4047: 'initializing' : 'char *' differs in levels of indire
ction from 'int'
Re.c(236) : warning C4047: 'initializing' : 'char *' differs in levels of indire
ction from 'int'
Re.c(237) : warning C4047: 'initializing' : 'char *' differs in levels of indire
ction from 'int'
Re.c(323) : warning C4047: 'initializing' : 'char *' differs in levels of indire
ction from 'int'
Re.c(355) : warning C4047: 'initializing' : 'char *' differs in levels of indire
ction from 'int'
Re.c(356) : warning C4047: 'initializing' : 'char *' differs in levels of indire
ction from 'int'
Re.c(357) : warning C4047: 'initializing' : 'char *' differs in levels of indire
ction from 'int'
Re.c(400) : warning C4047: 'initializing' : 'char *' differs in levels of indire
ction from 'int' 

Here is the c code;

#include <stdio.h>
#include <string.h>
#define STRING 4096
#define MESSAGE 4096
#define MAX_STRING 4096
#define COPY_WHOLE_CONSONANT_WORD 4096
/* The function to delete unwanted chars from strings */
void delete_char(char *src, char n, int len)
{
    char *dst;
    int i;
    // Do not remove NULL characters.
    if ( n == 0 )
        return;
    // Small attempt to control a buffer overflow if the
    // the string is not null-terminated and a proper length
    // is not specified.
    if ( len <= 0 )
        len = MAX_STRING;
    dst = src;
    for ( i = 0; i < len && *src != 0; i++, src++ )
    {
        if ( *src != n )
            *dst++ = *src;
    }
    // Ensure the string is null-terminated.
    *dst = 0;
    return;
}
/* The function to split the input into a sentence per line */
int split_by_sentence(void)
{
    int character, file_character=0;
    char buffer[1024];
    FILE *book=fopen("readtext1.txt", "r");
    FILE *book2=fopen("readtext.txt", "a+");
    if(!book) {printf("Error: unable to open input file!\n"); return 1;}
    if(!book2) {printf("Error: unable to open output file!\n"); return 1;}
    while(file_character!=EOF)
    {
        buffer[0]='\0';
        for(character=0;character<sizeof(buffer);character++) 
        {
            file_character=fgetc(book);
            if(file_character==EOF)
                break;
            if(file_character=='.')
            {
                buffer[character]='\0';  
                break;
            }
            if(file_character=='?')
            {
                buffer[character]='\0';  
                break;
            }
            if(file_character=='!')
            {
                buffer[character]='\0';  
                break;
            }
            buffer[character]=file_character;
        }
        if(file_character==EOF)
            break;
        fprintf(book2, "%s.\n", buffer);
    }
    fclose(book);
    fclose(book2);
    putchar('\n');
    return 0;
}
void split_sentence (char* sentence)
{
    FILE *list = NULL;
    char * pch = malloc(4096);
    list = fopen("list.txt", "w");
    if(list == NULL)  
    {
        perror("Error: file list.txt was not found or opened");  
        goto Cleanup;
    }
    /* split sentence into a word per line, and save to list.txt */
    pch = strtok(sentence, " ");
    while(pch != NULL)
    {
        fprintf(list, "%s\n", pch);
        pch = strtok (NULL, " ");
    }
    free(pch);
    fclose(list);
Cleanup:
    if (list != NULL)
    {
        fclose(list);
        list = NULL;
    }
}
void find_consonants()
{
    FILE *list = NULL;
    FILE *intermediate = NULL;
    FILE *consonant_words = NULL;
    char consonant[] = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXYZ";
    char * list_of_words = malloc(4096);
    int loop = 0;
    int loop_2 = 0;
    int count = 0;
    list = fopen("list.txt", "r");
    if(list == NULL) 
    {
        perror("Error: file list.txt was not found or opened"); 
        goto Cleanup;
    }
    intermediate = fopen("intermediate.txt", "w");
    if(intermediate == NULL) 
    {
        perror("Error: file intermediate.txt was not found or opened");  
        goto Cleanup;
    }
    consonant_words = fopen("consonant_words.txt", "w");
    if(consonant_words  == NULL) 
    {
        perror("Error: file consonant_words.txt was not found or opened"); 
        goto Cleanup;
    }
    while(fgets(list_of_words, 4096, list)!=NULL)
    {
        fprintf(intermediate, "\n");
        for(loop=0;list_of_words[loop]!='\0';loop++)
        {
            for(loop_2=0;consonant[loop_2]!='\0';loop_2++)
            {
                if(list_of_words[loop] == consonant[loop_2])
                {
                    fprintf(intermediate, "%c", consonant[loop_2]);
                    count++;
                }
            }
        }   
        if(count > 0)
        {
            fprintf(consonant_words, "%s", list_of_words);
            count = 0;
        }
    }
    free(list_of_words);
    fclose(list);
    fclose(intermediate);
    fclose(consonant_words);
    loop = 0;
    loop_2 = 0;
Cleanup:
    if (list != NULL)
    {
        fclose(list);
        list = NULL;
    }
    if (intermediate != NULL)
    {
        fclose(intermediate);
        intermediate = NULL;
    }
    if (consonant_words != NULL)
    {
        fclose(consonant_words);
        consonant_words = NULL;
    }
}
void last_line(char *last)
{
    FILE *intermediate = NULL;
    intermediate = fopen("intermediate.txt", "r");
    if(intermediate == NULL) 
    {
        perror("Error: file intermediate.txt was not found or opened");  
        goto Cleanup;
    }
    while(fgets(last, 4096, intermediate)!=NULL)
    {
    }
    fclose(intermediate);
Cleanup:
    if (intermediate != NULL)
    {
        fclose(intermediate);
        intermediate = NULL;
    }
}
void compare_the_consonants(char *last)
{
    FILE *last_word = NULL;
    FILE *intermediate = NULL;
    FILE *comparison = NULL;
    FILE *consonant_words = NULL;
    char consonant[] = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXYZ";
    char copy_whole_consonant_word[COPY_WHOLE_CONSONANT_WORD] = {0};
    char * letters = malloc(4096);
    char * whole_consonant_words = malloc(4096);
    int count = 0;
    intermediate = fopen("intermediate.txt", "r");
    if(intermediate  == NULL) 
    {
        perror("Error: file intermediate.txt was not found or opened");  
        goto Cleanup;
    }
    consonant_words = fopen("consonant_words.txt", "r");
    if(consonant_words == NULL) 
    {
        perror("Error: file consonant_words.txt was not found or opened");   
        goto Cleanup;
    }
    last_word = fopen("last_word.txt", "w");
    if(last_word == NULL)  
    {
        perror("Error: file last_word.txt was not found or opened");  
        goto Cleanup;
    }
    comparison = fopen("comparison.txt", "w");
    if(comparison == NULL) 
    {
        perror("Error: file comparison.txt was not found or opened"); 
        goto Cleanup;
    }
    while(fgets(letters, 4096, intermediate)!=NULL)
    {
        if(strpbrk(letters, consonant))
        {
            fgets(whole_consonant_words, 4096, consonant_words);
            if(strpbrk(letters, last))
            {
                fprintf(last_word, "%s", whole_consonant_words);
                memmove(copy_whole_consonant_word, whole_consonant_words, strlen(whole_consonant_words)+1);
                delete_char(copy_whole_consonant_word, '\n', 0);
                fprintf(comparison, "%s ", copy_whole_consonant_word);
                memset(&copy_whole_consonant_word[0], 0, sizeof(copy_whole_consonant_word));
                count++;
            }
        }
    }
    fprintf(comparison, "\n");
    free(last);
    free(whole_consonant_words);
    free(letters);
    fclose(comparison);
    fclose(intermediate);
    fclose(consonant_words);
    fclose(last_word);
Cleanup:
    if (intermediate != NULL)
    {
        fclose(intermediate);
        intermediate = NULL;
    }
    if (comparison != NULL)
    {
        fclose(comparison);
        comparison = NULL;
    }
    if (consonant_words != NULL)
    {
        fclose(consonant_words);
        consonant_words = NULL;
    }
    if (last_word != NULL)
    {
        fclose(last_word);
        last_word = NULL;
    }
}
void secret_message()
{
    FILE *comparison = NULL;
    char * words = malloc(4096);
    comparison = fopen("comparison.txt", "r");
    if(comparison == NULL) 
    {
        perror("Error: file comparison.txt was not found or opened");    
        goto Cleanup;
    }
    while(fgets(words, 4096, comparison)!=NULL)
    {
        printf("\n\n__________________\n");
        printf("\nThe secret message in the sentence:\n");
        printf("__________________\n\n");
        printf("%s\n", words);
    }
    fclose(comparison);
    free(words);
Cleanup:
    if (comparison != NULL)
    {
        fclose(comparison);
        comparison = NULL;
    }
}
void redefined_sentence (char* sentence)
{
    FILE *comparison = NULL;
    char dels[] = ".\n`1234567890-=~!@#$%^&*()_+[]\\{}|;\':\",/<>?$";
    char message[MESSAGE] = {0};
    char * last = malloc(4096);
    char * last_string = malloc(4096);
    char * calculation = malloc(4096);
    int i = 0;
    for ( i = 0 ; dels[i] != '\0' ; i++ ) {
        delete_char(sentence, dels[i], 0);
    }
    /* split sentence into a word per line, and save to list.txt */
    split_sentence(sentence);
    /* find consonants in the list.txt, write results to intermediate.txt */
    find_consonants();
    /* open intermediate.txt, and put the consonants from the last word in the text file into a variable */
    last_line(last);
    /* compare the consonants from the last word to the consonants from the rest of the words. 
    Also loop through file with the whole word version.
    Then, display the results on the screen. */
    compare_the_consonants(last);
    //////////////
    /* Display the secret message in the sentence */
    secret_message();
    i = 0;
    memset(&message[0], 0, sizeof(message));
Cleanup:
    if (comparison != NULL)
    {
        fclose(comparison);
        comparison = NULL;
    }
}
void MyExit(void) { system("pause"); }
int main ()
{
    FILE *book;
    char * read_book = malloc(4096);
    int j = 0;
    book=fopen("readtext.txt", "r");
    if(!book) {printf("Error: unable to open input file!\n"); return 1;}
    atexit(MyExit); 
    split_by_sentence();
    while(fgets(read_book, 4096, book)!=NULL)
    {
        /* Announces the sentences will be conveted to lower case */
        printf("\n__________________\n");
        printf("__________________\n\n");
        printf("An input sentence is converted to lower case:\n");
        printf("__________________\n\n");
        /* Convert the sentence to lowercase */
        for (j = 0; read_book[j] != '\0'; j++)
            read_book[j] = putchar (tolower(read_book[j]));
        /* Uses the lower cased sentence */
        redefined_sentence(read_book);
    }
    free(read_book);
    fclose(book);
    return 0;
}

All replies (5)

Friday, March 23, 2012 12:06 AM âś…Answered

On 3/22/2012 7:54 PM, brownie ri wrote:

This is line 93: char * pch = malloc(4096)

#include <stdlib.h>

malloc() is never declared. C compiler assumes that a function that wasn't declared returns int.

Igor Tandetnik


Thursday, March 22, 2012 11:41 PM

On 3/22/2012 7:37 PM, brownie ri wrote:

Re.c(93) : warning C4047: 'initializing' : 'char *' differs in levels of indirec
tion from 'int'

Which line is line 93? Somewhere around there, you are assigning an int value to a char* variable. Don't.

Igor Tandetnik


Thursday, March 22, 2012 11:54 PM

This is line 93: char * pch = malloc(4096)

The function "redefined_sentence" is calling it. The redefined sentence function recieves a string from main, and then deletes unwanted chars from it and I think this converts the string to int, then the "split_sentence" function is called and that's where line 93 is.

How should I fix it again?


Friday, March 23, 2012 12:12 AM

That include fixed the warning. Thank you.


Friday, March 23, 2012 3:53 AM

What is the purpose of the call to putchar() at the end of split_by_sentence?

In split_sentence you call malloc but never free the area allocated.  By the time you call free, pch is NULL so the call is effectively a no-op.  You also call fclose twice for the same file which could invoke undefined behavior.

find_consonants causes a memory leak if any of the files fail to open.

What is the purpose of last_line()?  It reads the contents of a file without taking any action on that data.  Then it attempts to fclose the file twice.

compare_the_consonants causes a memory leak if any of the files fail to open.  It also attmepts to close the files twice.  Why do you use memmove to copy a string when it is much more expensive than strcpy?  Why bother calling memset to reset all of copy_whole_consonant_word when you only copy into it without regard to previous contents?  Why do you bother setting a bunch of pointers to NULL just prior to exiting the function when the pointers will be immediately destroyed?

secret_message causes a memory leak if any of the files fail to open.  It also attmepts to close the files twice.

redefined_sentence allocates three areas of memory.  Two are never used and none of them are freed, causing memory leaks.  The function also resets variables that are about to go out of existence.  What is the purpose of the array message?