问题描述
晚上好,
我是一名正在学习如何使用C ++编写代码的学生。如果我问Pointers在制作节目时有用吗?当我学习C语言时,我发现有点难以理解使用指针。有人可以告诉我或告诉我在一些问题中使用指针吗?
一些可能有助于我理解的问题。
1. can你给我一个简单的示例程序,其中需要使用指针?
2.你能给我一个示例程序,显示使用指针而不使用指针的区别吗?
我认为第二个问题可以帮助我欣赏指针的使用。
谢谢。
抱歉,如果我问一个对于真正的程序员来说太基本的问题。还在努力学习:)谢谢。
PS:如果这篇文章有任何问题。请告诉我。比如这类帖子是否违反本网站的规则。
good evening,
i'm a student currently learning how to code using C++. it is ok if I ask how Pointers is useful in making programs? I find kinda difficult to appreciate the use of pointers back when I was learning the C language. can someone show me or tell me the use of pointers in some problems?
some questions that may help me understand.
1. can you give me a simple sample program in which it is required to use a pointer?
2. can you give me a sample program that shows the difference of using pointers and not using a pointer?
I think that the second question would help me appreciate the use of pointers.
thank you.
sorry if I asked a question that is too basic for a real programmer. still trying to learn :) thank you.
PS: if there is anything wrong with this post. Please tell me. like if this kinds of post is against the rules of this site.
推荐答案
if (char1 == '.') char1 = '\0';
if (char2 == '.') char2 = '\0';
if (char3 == '.') char3 = '\0';
if (char4 == '.') char4 = '\0';
...
但是当你不知道输入多长时间或者它是一个非常长的字符串时,这会变得非常困难。
相反你可能只是在循环中使用指针:
but that gets very difficult when you don't know how long the input is, or its a very long string.
Instead you could just use a pointer in a loop:
for (i = 0, pstr = startOfString; i < lengthOfString; i++, pstr++)
{
if (*pstr == '.')
{
*pstr = '\0';
break;
}
}
现在您不必担心单个名称或字符串的持续时间。
Now you don't need to worry about individual names, or about how long the string is.
这篇关于指针在C ++中的重要性是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!