我的代码段错误,我不知道为什么.
1 #include <stdio.h>
2
3 void overwrite(char str[], char x) {
4 int i;
5 for (i = 0; str[i] != '\0'; i++)
6 str[i] = x;
7 }
8
9 int main(void) {
10 char *s = "abcde";
11 char x = 'X';
12 overwrite(s, x);
13 printf("%s\n", s);
14 return 0;
15 }
Run Code Online (Sandbox Code Playgroud)
gdb调试器告诉我,问题出在第6行,我想将一个char存储到c-string中(如果我使用左值指针解除引用,那就是同样的问题.)这就是他所说的:
(gdb) run
Starting program: /tmp/x/x
Breakpoint 1, overwrite (str=0x8048500 "abcde", x=88 'X') at x.c:5
5 for (i = 0; str[i] != '\0'; i++) …Run Code Online (Sandbox Code Playgroud) 我有一个代表double的字节数组:
char number[8];
Run Code Online (Sandbox Code Playgroud)
我需要将它转换为实际的double(也有8个字节).基于我尝试过的建议,但它失败了:
std::cout<<(*((*double)number))<<" is my number.\n";
Run Code Online (Sandbox Code Playgroud)
为什么会失败,我该怎么办?当然,我可以使用一些<<魔法提取数据,但我不想这样做 - 它会消耗内存并使代码过于健壮.
在Phobos中是否有一个函数用于将零终止字符串转换为D字符串?
到目前为止,我只发现了相反的情况toStringz.
我在下面的代码片段中需要这个
// Lookup user name from user id
passwd pw;
passwd* pw_ret;
immutable size_t bufsize = 16384;
char* buf = cast(char*)core.stdc.stdlib.malloc(bufsize);
getpwuid_r(stat.st_uid, &pw, buf, bufsize, &pw_ret);
if (pw_ret != null) {
// TODO: The following loop maybe can be replace by some Phobos function?
size_t n = 0;
string name;
while (pw.pw_name[n] != 0) {
name ~= pw.pw_name[n];
n++;
}
writeln(name);
}
core.stdc.stdlib.free(buf);
Run Code Online (Sandbox Code Playgroud)
我用它来查找用户ID的用户名.
我现在假设UTF-8兼容性.
简单问题:
如何使用尽可能少的代码返回空C-String?
我有需要返回空的代码char*.我正在寻找一些类似的东西return "";.我知道有几种方法可以做到这一点,但我正在寻找最有效的方法.
使用return "";给出warning: conversion from string literal to 'char *' is deprecated [-Wdeprecated-writable-strings]
谢谢!
我正在尝试编写一个非常简单的程序,但我在这里找不到问题.试过不同的方法,这就是我现在尝试的方法:
#include <stdio.h>
void copyStr(char *p, char *h){
int i=0,j=0;
int length=0;
length=strlen(p); int l=length;
for (i=0; i<length; i++){
h[i]=p[l-1];
l--;
}
char *temp=&h[0];
for (i=0; i<length; i++){
printf("%c",temp[i]);
}
}
main(){
char p[]="abcde";
char h [sizeof(p)];
copyStr(p,h);
}
Run Code Online (Sandbox Code Playgroud)
当我复制这些字符串时,似乎没有复制第一个字母.
我的任务实际上更大,试图复制REVERSE中的字符串,但我相信找出这里出了什么问题会帮助我成功.
任何帮助都是适当的.
编辑:解决了,代码现在正在运行.
这里我有一个动态分配的c字符串数组.
释放这样一个数组的正确方法是什么?
有必要单独释放每个元素A,如下面的代码?
谢谢.
#include <string.h>
int main()
{
const char** A = new const char*[3];
A[0] = (const char*)memcpy(new char[5], "str0", 5);
A[1] = (const char*)memcpy(new char[5], "str1", 5);
A[2] = (const char*)memcpy(new char[5], "str2", 5);
// Is this the proper way to free everything?
for (int i = 0; i < 3; ++i)
delete[] A[i];
delete[] A;
}
Run Code Online (Sandbox Code Playgroud) 以下程序运行正常,我很惊讶为什么:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void xyz(char **value)
{
// *value = strdup("abc");
*value = "abc"; // <-- ??????????
}
int main(void)
{
char *s1;
xyz(&s1);
printf("s1 : %s \n", s1);
}
Run Code Online (Sandbox Code Playgroud)
输出:
s1 : abc
Run Code Online (Sandbox Code Playgroud)
我的理解是我必须使用strdup()函数为C中的字符串分配内存,而我没有分配内存.但是在这种情况下,只需使用""分配字符串值,程序似乎工作正常,任何人都可以解释一下吗?
我有一个字符串.我只想更改字符串的开头几个字符,并保留原样.在C中执行此操作的最佳方法是什么?
#include <stdio.h>
#include <string.h>
int main() {
char src[40];
char src2[40];
char dest[12];
memset(dest, '\0', sizeof(dest));
strcpy(src, "This is a string");
strcpy(src2, "That");
strncpy(dest, src, sizeof(src));
strncpy(dest, src2, sizeof(src2));
printf("Final copied string : %s\n", dest);
}
Run Code Online (Sandbox Code Playgroud)
我希望将字符串更改"This is a string"为"That is a string".
有没有一种简单的方法可以实现这个我想念的东西?
我一直看到const char *以这种方式使用,并对libc这种效果做出假设。
在 API 审查期间,我说const char *应该假设是一个NULL终止的字符串,然后有人说它不一定是 - 所以不能做任何假设。
我们仍然可以做出防御性的假设,但我们可以编写更少的代码。
鉴于所有以libc这种方式使用它的函数,C 或 C++ ISO 标准中是否有任何内容表明它必须是NULL终止字符串?
#include <stdio.h>
void fun(char *p) {
if (p) {
printf("%c", *p);
p++;
}
}
int main() {
fun("Y32Y567");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Output:Y
Expected Output:Y32Y567
My questions why it is not working the expected way?