值类型const char不能用于初始化char *类型的实体 [英] Value type const char cannot be used to initialize an entity of type char*

查看:883
本文介绍了值类型const char不能用于初始化char *类型的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管我的代码可以正确编译,但我仍然遇到以下问题:

I am having the following problem with my code, though it compiles correctly:


值类型const char不能用于初始化char *类型的实体*

value type const char cannot be used to initialize an entity of type char*

有人可以帮助我吗?我可以运行很奇怪的代码,但是无法使用它创建一个makefile。

Can someone help me? I can run the code which is weird but I can't create a makefile using this. It's very weird to me.

int SpliString(struct dict_word *entry, const char *str)
{
long word_length,j,k;

int yearIndex;
char *buffer;
char *endOfYears;
char *endOfYear;
char *endOfDefinition;
char *endOfWord = strstr(str, "_#_");

   //Sets the first num bytes of the block of memory pointed by ptr
  //to the specified value (related as an unsigned char)
memset(entry, 0, sizeof(struct dict_word));

  // If '_#_' is not found, it's NULL
    if (endOfWord)
{
    // Calculating word legth; 'str' points to start of word, 'endofWord' points to '_#_' that is just after word
    word_length = endOfWord - str;

    // Copying data into the word
    strncpy(entry->words, str, word_length);

    // 'endOfYears' points to '_#_,' but wee need to find follow '_#_'
    // therefore there is added 3 in order to skip curremnt '_#_
    endOfYears = strstr(endOfWord+3, "_#_");

    if (endOfYears)
    {
        word_length = endOfYears - (endOfWord+3);
        // Skips _#_
        buffer = endOfWord+3;
        yearIndex = 0;
        j = 0;

        // Finds next year in the line, it stops if all 10 years is filled
        // or end of years string is reached
        while(yearIndex<10 && buffer+j<endOfYears)
        {
            // Stores year in the buffer, with converting 'stirng' to 'int'
            entry->year[yearIndex] = atoi(buffer+j);
            // check year for negative...
            if (entry->year[yearIndex]<=0)
                return 0;

            // Locating substring; 'j' is current offset from beginning of buffer
            endOfYear = strchr(buffer+j, '_');

            if (endOfYear)
            {
                j = endOfYear - buffer;
                j++;
                yearIndex++;
            }
            else
            {
                break;
            }
        }

        //endOfYears points to '_#_' that separatates 'years' and 'definition'
        //and there is needed to find '_#_' between 'definition' and 'synonyms'
        //therefore it skips '_#_' that separatates 'years' and 'definition',
        //+3, because '_#_' has length = 3
        endOfDefinition = strstr(endOfYears+3, "_#_");
        if (endOfDefinition)
        {
            word_length = endOfDefinition - (endOfYears+3);
            k = 0;

            for(j=0; j<word_length; j++)
            {
                // Skips '_#_'
                if (endOfYears[j+3]==',')
                {
                    entry->eng_synonyms[k] = ' ';
                    k++;
                }
                else if (endOfYears[j+3]>='a' && endOfYears[j+3]<='z')
                {
                    entry->eng_synonyms[k] = endOfYears[j+3];
                    k++;
                }
                else if (endOfYears[j+3]!='_')
                {
                    return 0;
                }
            }

            k = 0;
            word_length = (str+strlen(str)) - (endOfDefinition+3);

            for(j=0; j<word_length; j++)
            {
                if (endOfDefinition[j+3]==',')
                {
                    entry->heb_synonyms[k] = ' ';
                    k++;
                }
                else if (endOfDefinition[j+3]>='A' && endOfDefinition[j+3]<='Z')
                {
                    entry->heb_synonyms[k] = endOfDefinition[j+3];
                    k++;
                }
                else if (endOfDefinition[j+3]!='_')
                {
                    return 0;
                }
            }
        }

        // Check for legality
        // Check all symbols of 'entry->words'
        // calculate length and supress warning
        for(j=0;j<(int)strlen(entry->words);j++)
        {
            if (entry->words[j]<'a' || entry->words[j]>'z')
                return 0;
        }

        return 1;
    }
}
return 0;
}


推荐答案

使用

const char *buffer;
const char *endOfWord = strstr(str, "_#_");

确信的OP正在用C ++编译。

Confident OP is compiling in C++.

// C
char *strstr(const char *s1, const char *s2);
// C++
const char* strstr(const char* s1, const char* s2);
char* strstr( char* s1, const char* s2);

请参见

使用Visual Studio 2012编译C应用程序

如何在Windows 8的Visual Studio 2012上编译并执行C程序?

这篇关于值类型const char不能用于初始化char *类型的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆