Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何初始化数组并用缓冲区中的字符填充?_C_Arrays - Fatal编程技术网

C 如何初始化数组并用缓冲区中的字符填充?

C 如何初始化数组并用缓冲区中的字符填充?,c,arrays,C,Arrays,我有以下方法: int create_nodes(Source* source, int maxTokens) { int nodeCount = 0; Token* p_Tstart = source->tknBuffer; Token* p_Tcurrent = source->tknBuffer; while ((p_Tcurrent - p_Tstart) < maxTokens && p_Tcurrent != NUL

我有以下方法:

int create_nodes(Source* source, int maxTokens) {
    int nodeCount = 0;
    Token* p_Tstart = source->tknBuffer;
    Token* p_Tcurrent = source->tknBuffer;

    while ((p_Tcurrent - p_Tstart) < maxTokens && p_Tcurrent != NULL) {
        int szWord = p_Tcurrent->t_L;
        char word[szWord];
        //  void *memset(void *str, int c, size_t n)
        memset(word, '\0', sizeof(char)*szWord);
        //  void *memcpy(void *str1, const void *str2, size_t n)
        char* p_T = source->buffer + p_Tcurrent->t_S;
        memcpy(word, p_T, szWord);

        if (word == ";") {
            ++p_Tcurrent;

            continue;
        }

        ++p_Tcurrent;
        ++nodeCount;
    }
}
token.h

struct source {
    const char* fileName;
    char* buffer;
    int bufLen;
    Token* tknBuffer;
    Node* nodeBuffer;
    Expression* exprBuffer;
};  
struct token {
    int t_S;
    int t_L;
};
这是不对的

if (word == ";") {
这将比较两个指针,并且很可能一直是错误的

我猜你是想用:

// Test whether the first character is a semicolon
if (word[0] == ';') {


请添加令牌和源类型声明!奇怪的是,即使调试器似乎跳过了这条指令,当我更正它时,我还是能够通过比较。strcmp是否依赖于以'\0'结尾的字符串?是的。否则,您将面临访问越界内存的风险。@IAbstract:将
szWord
增大一个字节,并在现在有
memcpy
的末尾加上零来修复它。
malloc(szWord+1)
本质上是,并且
memset
带有“\0”。
// Test whether the entire word is a semicolon
if (strcmp(word, ";") == 0) {