Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 指向一个有索引的指针_C++_Pointers_Indexing - Fatal编程技术网

C++ 指向一个有索引的指针

C++ 指向一个有索引的指针,c++,pointers,indexing,C++,Pointers,Indexing,我目前正在从头开始写strstr。在我的代码中,我正在索引一个字符串,最终需要使用另一个指针保存字符串上的一个特定点。下面是我正在努力解决的代码部分: char *save_str; for(int i=0;i<length_str1; i++) { if(str1[i]==str2[0]) { *save_str=str[i]; char*save\u str; 对于(int i=0;i,您可以从以下两种方式中进行选择: save_str = &

我目前正在从头开始写strstr。在我的代码中,我正在索引一个字符串,最终需要使用另一个指针保存字符串上的一个特定点。下面是我正在努力解决的代码部分:

char *save_str;
for(int i=0;i<length_str1; i++)
{
    if(str1[i]==str2[0])
    {
        *save_str=str[i];
char*save\u str;

对于(int i=0;i,您可以从以下两种方式中进行选择:

save_str = &str[i];

or

save_str = str+i;

快速实用答案

save_str = &str[i];
扩展描述性枯燥回答

save_str = &str[i];
“纯c”和“c++”中有一个关于数组和指针的特性

当程序员想要完整数组的地址或第一项时,不需要“&”运算符,即使某些编译器将其视为错误或警告

char *myptr = NULL;
char myarray[512];

strcpy(myarray, "Hello World");

// this is the same:
myptr = myarray;

// this is the same:
myptr = &myarray[0];
当程序员需要某个特定项目的地址时,则需要“&”运算符:

save_str = &str[i];
我在某个地方读到,这些功能是在紫色中添加的

许多开发人员避免了这种情况,而是使用指针算法:

...

char *save_str;
...

// "&" not required
char *auxptr = str1;

for(int i=0; i < length_str1; i++)
{
    // compare contents of pointer, not pointer, itself
    if(*auxptr == str2[0])
    {
        *save_str = *auxptr;
    }

    // move pointer to next consecutive location
    auxptr++;
}

...
。。。
字符*保存字符;
...
//“&”不是必需的
char*auxptr=str1;
对于(int i=0;i
就我个人而言,我希望“&”应该经常使用,避免混淆。
干杯。

编译器给出的错误消息是什么?在处理类似问题之前,您应该确保理解指针和指针指向的对象之间的区别。
*save_str
在您的上下文中没有意义。