const char*src ="你好";
调用strlen(src);返回大小为5 ...
现在说我这样做:
char* dest = new char[strlen(src)];
strcpy(dest, src);
Run Code Online (Sandbox Code Playgroud)
这似乎不应该工作,但当我输出一切看起来正确.好像我最后没有为空终结器分配空间......这是对的吗?谢谢
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
const char *str = "This is a string";
char *strCpy = strdup(str); // new copy with malloc in background
printf("str: %s strCpy: %s\n", str, strCpy);
free(strCpy);
char *anotherStr = "This is another string";
printf("anotherStr: %s\n", anotherStr);
char *dynamicStr = malloc(sizeof(char) * 32);
memcpy(dynamicStr, "test", 4+1); // length + '\0'
printf("dynamicStr: %s\n", dynamicStr);
free(dynamicStr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么 without malloc 的定义也是可能的,和之间anotherStr有什么区别?anotherStrdynamicStr
我编写了这个简单的代码来检查字符串是否仅包含字母和空格
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define N 100
int checkString(char str1[]);
void main()
{
char str1[N];
scanf("%s", str1);
printf("%d",checkString(str1));
getch();
}
int checkString(char str1[])
{
int i, x=0, p;
p=strlen(str1);
for (i = 0; i < p ; i++)
{
if ((str1[i] >= 'a' && str1[i] <= 'z') || (str1[i] >= 'A' && str1[i] <= 'Z') || (str1[i] == ' '))
{
continue;
}
else{ return 0; }
}
return 1;
}
Run Code Online (Sandbox Code Playgroud)
当我输入类似以下内容时,效果很好:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define …Run Code Online (Sandbox Code Playgroud) 在这一行:
auto a = "Hello World";
Run Code Online (Sandbox Code Playgroud)
什么是确切的类型a?我猜char[]或者const char* const我不确定.
我对A Tour of C++中的nullptr的使用感到困惑:
int count_x(char* p, char x)
// count the number of occurrences of x in p[]
// p is assumed to point to a zero-terminated array of char (or to nothing)
{
if (p==nullptr) return 0;
int count = 0;
for (; p!=nullptr; ++p)
if (*p==x) ++count;
return count;
}
// The definition of count_x() assumes that the char* is a C-style string,
// that is, that the pointer points to a zero-terminated array …Run Code Online (Sandbox Code Playgroud) 所以我好奇地阅读了一些C代码; 假设我们有以下代码:
char text[10] = "";
Run Code Online (Sandbox Code Playgroud)
那么C编译器在哪里放置空字符?
我可以想到3种可能的情况
'\0''\0'问题是,根据两种情况,是否有必要'\0'在执行时添加尾随strncpy.如果是第2和第3种情况,那么这不是绝对必要的,而是一个好主意; 如果它是案例1,那么这是绝对必要的.
这是什么?
我真的很困惑在字符串上使用指针.感觉他们遵守不同的规则.请考虑以下代码
char *ptr = "apple";// perfectly valid here not when declaring afterwards like next
ptr = "apple"; // shouldn't it be *ptr = "apple"
Run Code Online (Sandbox Code Playgroud)也printf()表现不同-
printf("%s", ptr) // Why should I send the address instead of the value
Run Code Online (Sandbox Code Playgroud)我还在一本书中看到了以下代码
char str[]="Quest";
char *p="Quest";
str++; // error, constant pointer can't change
*str='Z'; // works, because pointer is not constant
p++; // works, because pointer is not constant
*p = 'M'; // error, because string is constant
Run Code Online (Sandbox Code Playgroud)我无法理解应该暗示什么
请帮忙,我在其他地方找不到任何信息
我试图理解指针在这里是如何工作的.该findTheChar功能搜索str该角色chr.如果chr找到,则返回指向str首次找到字符的指针,否则nullptr(未找到).我的问题是为什么函数打印"llo"而不是"l"?虽然我写的代码main返回"e"而不是"ello"?
#include <iostream>
using namespace std;
const char* findTheChar(const char* str, char chr)
{
while (*str != 0)
{
if (*str == chr)
return str;
str++;
}
return nullptr;
}
int main()
{
char x[6] = "hello";
char* ptr = x;
while (*ptr != 0)
{
if (*ptr == x[1])
cout << *ptr << endl; //returns e
ptr++; …Run Code Online (Sandbox Code Playgroud) 所以说我有这个:
const auto foo = "lorem ipsum"
Run Code Online (Sandbox Code Playgroud)
如果我strlen(foo)在代码中使用,11 是在运行时找到的还是在编译时注入的?
c++ c-strings string-length string-literals compile-time-constant
下面是根据"标准C库,strlen.c的实现,
size_t strlen(const char *s){
const char *sc;
for(sc = s; *sc != '\0'; ++sc)
return (sc-s); }
Run Code Online (Sandbox Code Playgroud)
我对合法性的理解sc = s是否正确?
sc=s是一个合法的赋值,因为两个变量都被声明为const,都保护s指向的对象.在这种情况下,改变sc或s都指向但是任何赋值(或引用?)*s或者sc是非法的是合法的.