我真的很困惑在字符串上使用指针.感觉他们遵守不同的规则.请考虑以下代码
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)我无法理解应该暗示什么
请帮忙,我在其他地方找不到任何信息
我知道char*和char []之间的一些区别.
char x[] = "xxxx"
是一系列的字符;
char *y = "xxxx"
是一个指向文字(const)字符串的指针; 而且x[4]=='\0',*(y+4) == '\0'也是.为什么sizeof(x)==5和sizeof(y)==4?
我在调试AVR微控制器时遇到了这个问题:我有一个带有大量变量定义的main.c文件,其中有一个结构数组,如下所示:
struct mystruct mystruct_array[COUNT];
Run Code Online (Sandbox Code Playgroud)
在另一个.c文件中,我将此数组称为外部,但是我放弃了数组括号和大小,所以我不会重复自己,只是将变量声明为指针(因为数组本质上是指针,不是吗?) :
extern struct mystruct *mystruct_array;
Run Code Online (Sandbox Code Playgroud)
但是,当我检查使用数组的地址printf("%p\n", mystruct_array);我得到了一个空指针,而不是在存储阵列的位置.另外,如果我将访问后续项目数组中,喜欢printf("%p\n", &(mystruct_array[n]));将打印地址0加n倍sizeof(struct mystruct).
只有在我将定义更改为
extern struct mystruct mystruct_array[COUNT];
Run Code Online (Sandbox Code Playgroud)
(与main.c完全相同),我得到了数组的真实地址.
我的问题:为什么这会对编译器产生影响(在我的例子中是avr-gcc)?
我了解到,当我初始化一个字符数组时,就像初始化一个指向字符的指针一样.但是,如果是这种情况,为什么以下代码输出奇怪的字符?
char* returnMe()
{
char text[] = "Will I live forever?";
return text;
}
Run Code Online (Sandbox Code Playgroud)
而以下代码:
char* returnMe()
{
char* text = "Will I live forever?";
return text;
}
Run Code Online (Sandbox Code Playgroud)
输出:
我会永远活着吗?
这两个初始化之间到底有什么区别?它们都像指针一样,所以如果我这样做:
puts(X); //puts get char* as a parameter in it.
Run Code Online (Sandbox Code Playgroud)
它适用于两种情况(当我还没有超出范围时.)
这是我无法理解的代码:
char* myPtr = "example";
myPtr[1] = 'x';
Run Code Online (Sandbox Code Playgroud)
我如何被允许使用myPtr[1]?为什么我可以选择像数组一样的位置?myPtr甚至不是一个阵列.
OBS.我知道查找表,文字池和字符串文字,我关心的是这甚至编译.我没有那么多使用指针.
有人可以帮忙吗?
在 C++11 中,char指针不能直接初始化为字符串文字。
在早期版本的 C++ 中,我可以毫无问题地执行此操作。
char arr[] = "Hello";
char *p_str1 = arr; //allowed
Run Code Online (Sandbox Code Playgroud)
char *p_str3 = "Hello"; //Not allowed
Run Code Online (Sandbox Code Playgroud)
注意:我知道添加const可以修复。但我需要知道原因。
这似乎是一个愚蠢的问题。我有一个字符数组,想要将数组的地址存储在另一个变量中,但似乎无法声明数组地址的正确类型(我正在使用 gcc):
\n在:
\nint main(void){\n char cha[] = "abcde";\n char **arrayAddress = &cha;\n}\nRun Code Online (Sandbox Code Playgroud)\n出去:
\narrayaddress.c: In function \xe2\x80\x98main\xe2\x80\x99:\narrayaddress.c:3:25: warning: initialization of \xe2\x80\x98char **\xe2\x80\x99 from incompatible pointer type \xe2\x80\x98char (*)[6]\xe2\x80\x99 [-Wincompatible-pointer-types]\n 3 | char **arrayAddress = &cha;\n | ^\nRun Code Online (Sandbox Code Playgroud)\n这是预期的,我在其他地方读过, 的类型cha应该是char(*)[6]. 但是当我尝试arrayAddress用这种类型声明时,我的程序失败了:
在:
\narrayaddress.c: In function \xe2\x80\x98main\xe2\x80\x99:\narrayaddress.c:3:25: warning: initialization of \xe2\x80\x98char **\xe2\x80\x99 from incompatible pointer type \xe2\x80\x98char (*)[6]\xe2\x80\x99 [-Wincompatible-pointer-types]\n 3 | char **arrayAddress = …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
#include <stdio.h>
void changeValue(char str[]) {
str[2] = 'a';
}
int main()
{
char a[]="Hello world";
changeValue(a);
printf("%s", a);
}
Run Code Online (Sandbox Code Playgroud)
我试图了解这是如何工作的.为什么在传递名为'a'的数组时,它是通过引用传递的?那么,我在changeValue中所做的更改实际上是从外部看到的?不应该是定义为char*str的函数参数,以便能够更改它吗?
我用这段代码得到了这个错误:
string folder;
getline(cin, folder);
string folder2 = folder + "/index.txt";
const char* oldhtml[] = { folder2.c_str() };
folder2 = folder + "/index.html";
const char* newhtml[] = { folder2.c_str()};
rename(oldhtml, newhtml);
Run Code Online (Sandbox Code Playgroud)
发生错误: rename(oldhtml, newhtml);
我是C++的新手.所以,如果这是一个简单的修复我道歉