gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:47:13 24 4
gpt4 key购买 nike

我有以下方法:

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;
}
}

source 包含一个 char* 缓冲区。该方法的目的是首先计算缓冲区中不是 ; 标记的所有标记(并得出我们需要的节点数)。这是我正在使用的缓冲区:11 + 31;。至此, token 已创建如下:

  • 11
  • +
  • 31
  • ;

我传入一个 token*,它包含一个开始 (t_S) 和一个长度 (t_L)。因此,例如,表示 + 字符的标记:

  • t->t_S = 3
  • t->t_L = 1

memcpy(...) 处,调试器跳转到 ++p_Tcurrent,这意味着没有任何内容被复制到 word - 本质上是一个未捕获的我猜是异常(exception)。初始化单词数组我做错了什么?然后用token指定的具体信息填充进去?

source.h:

struct source {
const char* fileName;
char* buffer;
int bufLen;
Token* tknBuffer;
Node* nodeBuffer;
Expression* exprBuffer;
};

token.h:

struct token {
int t_S;
int t_L;
};

最佳答案

这是不对的。

if (word == ";") {

这将比较两个指针并且很可能始终为假。

我怀疑你打算使用:

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

// Test whether the entire word is a semicolon
if (strcmp(word, ";") == 0) {

关于c - 如何初始化数组并填充缓冲区中的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29930525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com