使用指针指向字符串中的某些字符
Pointing to a some characters in a string using pointer
#include<iostream>
using namespace std;
int main()
{
char s1[80]={"This is a developed country."};
char *s2[8];
s2[0]=&s1[10];
cout<<*s2; //Predicted OUTPUT: developed
// Actual OUTPUT: developed country.
return 0;
}
我想要 cout<<*s2;应该只打印其中的字母 {"developed"},所以我给 *s2[8] 长度为 8 个字符。我该怎么做才能使变量 cout<<*s2 最多只能打印 8 个字符的长度。我正在使用 dmc、lcc 和 OpenWatcom 编译器。这只是我使用字符串数据类型的其他更大程序的一小部分,所以我现在能做什么,非常感谢您回答我的问题:)
s2
是指向 char
的长度为 8 的指针数组。您正在使它的第一个元素指向 s1
从位置 10 开始。仅此而已。您没有使用该数组的其余元素。因此 s2
的长度无关紧要。
您也可以这样做:
char* s2 = &s1[10];
如果你想从 s1
的一部分创建一个字符串,你可以使用 std::string
:
std::string s3(s1+10, s1+19);
std::cout << s3 << endl;
请注意,这会分配自己的内存缓冲区并保存副本或原始字符序列。如果您只想查看另一个字符串的一部分,您可以轻松地实现一个 class 持有一个 begin 和 one-past 指向原始的结束指针。这是一个粗略的草图:
struct string_view
{
typedef const char* const_iterator;
template <typename Iter>
string_view(Iter begin, Iter end) : begin(begin), end(end) {}
const_iterator begin;
const_iterator end;
};
std::ostream& operator<<(std::ostream& o, const string_view& s)
{
for (string_view::const_iterator i = s.begin; i != s.end; ++i)
o << *i;
return o;
}
然后
int main()
{
char s1[] = "This is a developed country.";
string_view s2(s1+10, s1+19);
cout << s2 << endl;
}
s2
是指向 char*
的指针数组。您只会使用此数组中的第零个元素。
&s1[10]
指向字符串s1
中的第11个字符。该地址分配给 s2
.
的第零个元素
在cout
语句中,*s2
等同于s2[0];
。所以 cout << *s2;
输出 s2
的第零个元素,它已分配给 s1
的第 11 个字符。 cout
将沿着内存滚动,直到到达字符串的空终止符。
字符串必须以 NULL 结尾,也就是 [=10=]
。 s2
的开头很好,但 cout 将继续读取直到结束。如果你想能够输出,你必须实际复制数据而不是简单地设置指针。
你的错误想法是
char *s2[8];
声明了一个指向 8 个字符数组的指针(或者, 而不是 等效地声明了一个指向恰好 8 个字符的字符串的指针)。它不会做任何一个。它不是声明一个指向数组的指针,而是声明一个指针数组。
如果您希望 s2
成为一个指向 8 个字符数组的指针,您需要:
char (*s2)[8];
但是,还是一团糟。你问:
What can I do so that the variable *s2 will store only up to its length?
你认为它的长度是8吗?在尝试回答之前,return 到您对 s1
:
的定义
char s1[80]={"This is a developed country."};
长度是80还是28?答案是,取决于您如何定义 'length' - 数组的长度或到空终止符的长度?
所有这些关于大小的误解都没有帮助。作为@n.m。在评论中指出,C++ 中所有指针问题的解决方案是停止使用指针。 (如果我解释有误,请见谅 n.m.!)
#include<iostream>
using namespace std;
int main()
{
string s1="This is a developed country.";
string s2;
s2 = s1.substr(10, 9);
cout << s2;
return 0;
}
如果你想使用 ghetto 风格并跳过 std::string 出于某种原因你总是可以使用 strncpy、memcpy 或 strstr 等。
int main()
{
char s1[80]="This is a developed country.";
char s2[10];
strncpy(s2,s1+10,9);
s2[9] = '[=10=]';
std::cout << s2 << std::endl;
std::cin.get();
return 0;
}
s2是一个char类型的数组,数组的元素是char *,所以不能用它来存储字符串。如果你想得到字符串中的"developed",你可以这样写代码:
#include<iostream>
using namespace std;
int main()
{
char *s1[]={"This", "is", "a", "developed", "country."};
char *s2[8];
s2[0]= s1 + 3;
cout<<s2[0]; //Predicted OUTPUT: developed
// Actual OUTPUT: developed country.
return 0;
}
#include<iostream>
using namespace std;
int main()
{
char s1[80]={"This is a developed country."};
char *s2[8];
s2[0]=&s1[10];
cout<<*s2; //Predicted OUTPUT: developed
// Actual OUTPUT: developed country.
return 0;
}
我想要 cout<<*s2;应该只打印其中的字母 {"developed"},所以我给 *s2[8] 长度为 8 个字符。我该怎么做才能使变量 cout<<*s2 最多只能打印 8 个字符的长度。我正在使用 dmc、lcc 和 OpenWatcom 编译器。这只是我使用字符串数据类型的其他更大程序的一小部分,所以我现在能做什么,非常感谢您回答我的问题:)
s2
是指向 char
的长度为 8 的指针数组。您正在使它的第一个元素指向 s1
从位置 10 开始。仅此而已。您没有使用该数组的其余元素。因此 s2
的长度无关紧要。
您也可以这样做:
char* s2 = &s1[10];
如果你想从 s1
的一部分创建一个字符串,你可以使用 std::string
:
std::string s3(s1+10, s1+19);
std::cout << s3 << endl;
请注意,这会分配自己的内存缓冲区并保存副本或原始字符序列。如果您只想查看另一个字符串的一部分,您可以轻松地实现一个 class 持有一个 begin 和 one-past 指向原始的结束指针。这是一个粗略的草图:
struct string_view
{
typedef const char* const_iterator;
template <typename Iter>
string_view(Iter begin, Iter end) : begin(begin), end(end) {}
const_iterator begin;
const_iterator end;
};
std::ostream& operator<<(std::ostream& o, const string_view& s)
{
for (string_view::const_iterator i = s.begin; i != s.end; ++i)
o << *i;
return o;
}
然后
int main()
{
char s1[] = "This is a developed country.";
string_view s2(s1+10, s1+19);
cout << s2 << endl;
}
s2
是指向 char*
的指针数组。您只会使用此数组中的第零个元素。
&s1[10]
指向字符串s1
中的第11个字符。该地址分配给 s2
.
在cout
语句中,*s2
等同于s2[0];
。所以 cout << *s2;
输出 s2
的第零个元素,它已分配给 s1
的第 11 个字符。 cout
将沿着内存滚动,直到到达字符串的空终止符。
字符串必须以 NULL 结尾,也就是 [=10=]
。 s2
的开头很好,但 cout 将继续读取直到结束。如果你想能够输出,你必须实际复制数据而不是简单地设置指针。
你的错误想法是
char *s2[8];
声明了一个指向 8 个字符数组的指针(或者, 而不是 等效地声明了一个指向恰好 8 个字符的字符串的指针)。它不会做任何一个。它不是声明一个指向数组的指针,而是声明一个指针数组。
如果您希望 s2
成为一个指向 8 个字符数组的指针,您需要:
char (*s2)[8];
但是,还是一团糟。你问:
What can I do so that the variable *s2 will store only up to its length?
你认为它的长度是8吗?在尝试回答之前,return 到您对 s1
:
char s1[80]={"This is a developed country."};
长度是80还是28?答案是,取决于您如何定义 'length' - 数组的长度或到空终止符的长度?
所有这些关于大小的误解都没有帮助。作为@n.m。在评论中指出,C++ 中所有指针问题的解决方案是停止使用指针。 (如果我解释有误,请见谅 n.m.!)
#include<iostream>
using namespace std;
int main()
{
string s1="This is a developed country.";
string s2;
s2 = s1.substr(10, 9);
cout << s2;
return 0;
}
如果你想使用 ghetto 风格并跳过 std::string 出于某种原因你总是可以使用 strncpy、memcpy 或 strstr 等。
int main()
{
char s1[80]="This is a developed country.";
char s2[10];
strncpy(s2,s1+10,9);
s2[9] = '[=10=]';
std::cout << s2 << std::endl;
std::cin.get();
return 0;
}
s2是一个char类型的数组,数组的元素是char *,所以不能用它来存储字符串。如果你想得到字符串中的"developed",你可以这样写代码:
#include<iostream>
using namespace std;
int main()
{
char *s1[]={"This", "is", "a", "developed", "country."};
char *s2[8];
s2[0]= s1 + 3;
cout<<s2[0]; //Predicted OUTPUT: developed
// Actual OUTPUT: developed country.
return 0;
}