在第一次迭代之后,我得到了奇怪的字符串。我怀疑这可能是由于字符串终止,但我不知道如何修复它。或者我用错了malloc。
任何暗示我都很高兴。
#include <stdio.h>
#include <memory.h>
#include <malloc.h>
#include <ctype.h>
#include "file_reader.h"
/**
* Opens a text file and reads the file. The text of the file is stored
* in memory
char *p = (char *) malloc(10 * sizeof(char));
... let p point to some chars
free(p);
char *p = (char *) malloc(10 * sizeof(char));
由于释放p不删除存储在特定内存地址上的数据,我面临的问题是,对于大型指针来说,新分配的内存与旧的内存重叠,这显然是有问题的。如何删除指针指向的数据?
我试图使用locale facet将wstring转换为string,但我遇到了以下错误:
test_facet.cpp: In function ‘int main()’:
test_facet.cpp:14: error: invalid initialization of reference of type ‘std::ctype<wchar_t>&’ from expression of type ‘const std::ctype<wchar_t>’
/usr/include/c++/4.4/bits/locale_facets.h:1430: err
我需要将char数组转换为小写的char数组。我还想将一些特殊字符转换为ä。但是“ASCII代码”196不在ASCII-128中。如何将它们转换为小写?我可以使用它们作为字符串初始化器,但不能真正处理它们的代码。如果这可能是一个编译器选项,我在没有c99模式的Linux上使用eclipse CDT (不能使用那个模式)。
char* toLowerCase(char* text)
{
int length = strlen(text);
char* result = malloc(length); // malloc adds the 0 automatically at re
在下面的代码中,我尝试逐字符加载一个单词的文本文件,然后尝试将每个单词保存在哈希表(字符串数组)中,但是strcpy似乎保存了一个完整的单词,而不是一个char,我不知道为什么。我是不是误用了strcpy和strcat
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <ctype.h>
# include <stdbool.h>
bool load(const char* dictionary);
#define LENGTH 45
如何在不收到以下警告的情况下使用gcc编译lex文件?
lex.yy.c: In function `yy_init_buffer':
lex.yy.c:1688: warning: implicit declaration of function `fileno'
lex.l: In function `storeLexeme':
lex.l:134: warning: implicit declaration of function `strdup'
这些是我包含的库。
%{
#include <stdio.h>
#include <std
我正在尝试创建一个链表结构来存储数据。链表的头似乎在以某种方式更新。我有以下代码。我似乎想不出如何将char数组数据放入节点中,并在char数组数据的地址更新时阻止其更新。
下面的代码打印出传递给processStr函数的所有字符串。如何防止head更新?
//Linked List Structure
mainNode *head = NULL;
//take and store word in data structure
void processStr(char *str){
//char array
char strArray[sizeof(str)+1];
有没有可能在不调用malloc的情况下从函数返回字符串?我有一个如下的函数:
char* getString(){
char tempStr[20]
// open file, read char by char and assign to tempStr
// ....
char* str = (char*)malloc(sizeof(char)*20);
strcpy(str, tempStr); // assume this copy the null terminating char as well
retur
目前,我在阅读单词时遇到了困难,这些单词由空格、一行一行与stdin分隔。我试图逐行读取单词,然后从访问字符串数组中打印它们。
如果我想读这句话:
Enter words: Hi there, how was your day sir?
然后我只想打印下面的句子,就像这样:
Your sentence: Hi there, how was your day sir?
到目前为止,我的代码是这样的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h&