编辑:我添加了示例的源代码.
我遇到了这个例子:
char source[MAX] = "123456789";
char source1[MAX] = "123456789";
char destination[MAX] = "abcdefg";
char destination1[MAX] = "abcdefg";
char *return_string;
int index = 5;
/* This is how strcpy works */
printf("destination is originally = '%s'\n", destination);
return_string = strcpy(destination, source);
printf("after strcpy, dest becomes '%s'\n\n", destination);
/* This is how strncpy works */
printf( "destination1 is originally = '%s'\n", destination1 );
return_string = strncpy( destination1, source1, index );
printf( "After strncpy, destination1 becomes '%s'\n", destination1 );
Run Code Online (Sandbox Code Playgroud)
哪个产生了这个输出: …
memcpy()和strcpy()有什么区别?我试图在一个程序的帮助下找到它,但两者都提供相同的输出.
int main()
{
char s[5]={'s','a','\0','c','h'};
char p[5];
char t[5];
strcpy(p,s);
memcpy(t,s,5);
printf("sachin p is [%s], t is [%s]",p,t);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
产量
sachin p is [sa], t is [sa]
Run Code Online (Sandbox Code Playgroud) 我读到的strcpy是复制字符串,并strdup返回指向新字符串的指针以复制字符串.
您能否解释一下您喜欢使用strcpy哪些情况以及您更喜欢使用哪些情况strdup?
我试图了解如何以最干净/最安全的方式解决C中的这个微不足道的问题.这是我的例子:
#include <stdio.h>
int main(int argc, char *argv[])
{
typedef struct
{
char name[20];
char surname[20];
int unsigned age;
} person;
//Here i can pass strings as values...how does it works?
person p = {"John", "Doe",30};
printf("Name: %s; Age: %d\n",p.name,p.age);
// This works as expected...
p.age = 25;
//...but the same approach doesn't work with a string
p.name = "Jane";
printf("Name: %s; Age: %d\n",p.name,p.age);
return 1;
}
Run Code Online (Sandbox Code Playgroud)
编译器的错误是:
main.c:在函数'main'中:main.c:18:错误:从类型'char*'分配类型'char [20]'时出现不兼容的类型
我知道C(不是C++)没有字符串类型而是使用字符数组,所以另一种方法是更改示例结构以保存字符指针:
#include <stdio.h>
int main(int argc, char *argv[])
{
typedef …Run Code Online (Sandbox Code Playgroud) 我最近开始用C编程,来自Java和Python.现在,在我的书中,我注意到要制作一个"Hello World"程序,语法是这样的:
char message[10]
strcpy(message, "Hello, world!")
printf("%s\n", message);
Run Code Online (Sandbox Code Playgroud)
现在,这个例子使用了一个char数组,我想知道 - 字符串发生了什么?为什么我不能简单地使用其中一个?也许有不同的方法来做到这一点?
我一直在研究C中的一个项目,这个项目需要我很多地处理字符串.通常,我使用C++编程,所以这与仅仅说string.empty()有点不同.
我想知道在C中清空字符串的正确方法是什么呢.这就是它吗?
buffer[80] = "Hello World!\n"; // ... strcpy(buffer, "");
在编写项目时,我遇到了一个奇怪的问题.
这是我设法编写的用于重新创建问题的最小代码.我故意将一个实际的字符串存储在其他地方,并分配了足够的空间.
// #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stddef.h> // For offsetof()
typedef struct _pack{
// The type of `c` doesn't matter as long as it's inside of a struct.
int64_t c;
} pack;
int main(){
pack *p;
char str[9] = "aaaaaaaa"; // Input
size_t len = offsetof(pack, c) + (strlen(str) + 1);
p = malloc(len);
// Version 1: crash
strcpy((char*)&(p->c), str);
// Version 2: crash
strncpy((char*)&(p->c), str, strlen(str)+1);
// Version 3: works!
memcpy((char*)&(p->c), str, strlen(str)+1); …Run Code Online (Sandbox Code Playgroud) 有人可以向我解释为什么strcpy()是将字符串分配给字符数组所必需的,例如在下面的代码片段中.
int main(void) {
char s[4];
s = "abc"; //Fails
strcpy(s, "abc"); //Succeeds
return 0;
}
Run Code Online (Sandbox Code Playgroud)
s = "abc"失败的原因是什么?为什么strcpy()是在声明字符串后将字符串赋值给char数组的唯一方法?我似乎很奇怪你必须使用一个函数来执行一个基本的赋值.
标准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)
还有其他合理的理由来证明这个成语吗?
我是C的初学者,我正在自学.我正在创建以下功能:
char *foo(int x){
if(x < 0){
char a[1000];
char b = "blah";
x = x - 1;
char *c = foo(x);
strcpy(a, b);
strcat(a, c);
return a;
}
blah ...
}
Run Code Online (Sandbox Code Playgroud)
我基本上试图返回一个附加的字符串,但我收到以下错误:
"错误:函数返回局部变量的地址",任何建议,如何解决这个问题?