我读到的strcpy是复制字符串,并strdup返回指向新字符串的指针以复制字符串.
您能否解释一下您喜欢使用strcpy哪些情况以及您更喜欢使用哪些情况strdup?
我知道我可以按成员复制结构成员,而不是我可以在结构上进行复制memcpy吗?
这样做是否明智?
在我的结构中,我有一个字符串也作为成员,我必须复制到具有相同成员的另一个结构.我怎么做?
我有一个用例,我可以获得在内存或文字中分配的字符串指针.现在后者不能被释放,所以如果我通过错误的话就会出问题.有没有办法知道哪一个被分配哪个不分配?
char *b = "dont free me!";
if(!IS_LITERAL(b)) {
free(b);
}
Run Code Online (Sandbox Code Playgroud)
我想象那样的事情.
我的例子:
场景1:字面意思
char *b = "dont free me!";
scruct elem* my_element = mylib_create_element(b);
// do smth
int result = mylib_destroy_element(my_element); // free literal, very bad
Run Code Online (Sandbox Code Playgroud)
场景2:在堆中
char *b = malloc(sizeof(char)*17); // example
strncpy(b, "you can free me!",17);
scruct elem* my_element = mylib_create_element(b);
// do smth
int result = mylib_destroy_element(my_element); // free heap, nice
Run Code Online (Sandbox Code Playgroud)
用户如何调用mylib_create_element(b);不受我的控制.如果他在mylib_destroy_element崩溃之前释放.所以它必须mylib_destroy_element清理干净.
标准C库中的许多函数,尤其是用于字符串操作的函数,最显着的是strcpy(),共享以下原型:
char *the_function (char *destination, ...)
Run Code Online (Sandbox Code Playgroud)
这些函数的返回值实际上与提供的相同destination.你为什么要浪费多余的回报价值呢?这样的函数无效或返回有用的东西更有意义.
我唯一的猜测是,为什么将函数调用嵌套在另一个表达式中更容易,更方便,例如:
printf("%s\n", strcpy(dst, src));
Run Code Online (Sandbox Code Playgroud)
还有其他合理的理由来证明这个成语吗?
如何使用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'
Run Code Online (Sandbox Code Playgroud)
这些是我包含的库.
%{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
%}
Run Code Online (Sandbox Code Playgroud)
函数yy_init_buffer不在文件中.以下是函数storeLexeme.
int storeLexeme() {
for (int i = 0; i < count; i++) {
char *curr = *(symbolTable + i);
if (strcmp(curr, yytext) == 0) {
return i;
}
}
char *lexeme = (char *)malloc(sizeof(char *));
lexeme = (char *)strdup(yytext);
symbolTable[count] = lexeme;
count++;
return (count …Run Code Online (Sandbox Code Playgroud) 我正在调用strdup,必须在调用之前为变量分配空间strdup.
char *variable;
variable = (char*) malloc(sizeof(char*));
variable = strdup(word);
Run Code Online (Sandbox Code Playgroud)
我这样做了吗?或者这里有什么问题吗?
当我在完成任务时,我发现我们不应该使用如下任务:
char *s="HELLO WORLD";
Run Code Online (Sandbox Code Playgroud)
使用这种语法的程序容易崩溃.
我尝试过并使用过:
int fun(char *temp)
{
// do sum operation on temp
// print temp.
}
fun("HELLO WORLD");
Run Code Online (Sandbox Code Playgroud)
甚至上面的工作(虽然输出是编译器和标准特定的).
相反,我们应该尝试strdup()或使用const char*
我曾尝试在博客上阅读其他类似的问题,但无法得到上述代码为什么工作的概念.
内存分配?const有什么不同?
是否需要释放使用' strcpy ' 创建的字符串?以及如何释放它?
编辑:目的地分配如下:
char* buffer[LEN];
Run Code Online (Sandbox Code Playgroud) char *tempMonth;
char month[4];
month[0]='j';
month[1]='a';
month[2]='n';
month[3]='\0';
Run Code Online (Sandbox Code Playgroud)
如何为tempMonth分配月份?谢谢
以及如何打印出来?
谢谢
出于某种原因,我在第一次使用strtok()时得到一个异常我想要完成的是一个函数,它只是检查子串在字符串中是否重复.但到目前为止我还没有开始工作
int CheckDoubleInput(char* input){
char* word = NULL;
char cutBy[] = ",_";
word = strtok(input, cutBy); <--- **error line**
/* walk through other tokens */
while (word != NULL)
{
printf(" %s\n", word);
word = strtok(NULL, cutBy);
}
return 1;
}
Run Code Online (Sandbox Code Playgroud)
和主要调用功能:
CheckDoubleInput("asdlakm,_asdasd,_sdasd,asdas_sas");
Run Code Online (Sandbox Code Playgroud)
