2013-10-02 46 views
0

我正在尝试使用Jesse Liberty的“在24小时内自学C++”自己学习C++。我写了这个简短的程序来找出C++中的指针。试图理解C++中的指针

#include <iostream> 

void hMany(int count); // hMany function prototype 


void hMany(int count){ 

    do { 
     std::cout << "Hello...\n"; 
     count--; 

     } while (count >0); 

}; 


int main (int argc, const char * argv[]) { 


    int counter; 
    int * pCounter = &counter; 

    std::cout << "How many hellos? "; 
    std::cin >> counter; 

    hMany(*pCounter); 

    std::cout << "counter is: " << counter << std::endl; 
    std::cout << "*pCounter is: " << *pCounter << std::endl; 

    return 0; 
} 

输出的结果是:

How many hellos? 2 
Hello... 
Hello... 
counter is: 2 
*pCounter is: 2 

什么是传递指针(*的Pcounter)与参数(计数器)有什么好处?

任何帮助将不胜感激。 路易斯

更新:

确定。该程序正在工作,我现在完全理解C++指针。谢谢大家的回应。在尝试Chowlett的代码后,我得到了2个警告(不是错误)。一个是!没有以前的函数hMany和* pCount--的原型!表达结果未使用。我能够自己纠正原型,但我无法弄清* pCount--警告。

我问我的朋友托尼的帮助,这里是他的答案。

圆括号使事情按正确的顺序发生。

(*pCount)-- 

表示遵循指向它指向的整数的指针,然后递减整数,这就是你想要做的。

*pCount-- 

最后也做了错误的事情时,编译器将其视为

*(pCount—) 

它说先递减指针,离开它指向“整数”你想改变一个之前(不存在这样的事情,因为你只有一个你称之为函数的整数),然后遵循这个递减的指针,并对该内存位置处的整数做任何事情。这就是编译器抱怨表达式结果未被使用的原因。编译器是正确的。此代码不正确地递减指针,提取错误的整数,并且不在任何地方存储错误的整数。

以下是可能感兴趣的C++新手的正确代码。

包括

空隙hMany(INT * pCount); // hMany函数原型

空隙hMany(INT * pCount){// * pCount接收次数的地址

do { 
    std::cout << "Hello...\n"; 


    // The parentheses make things happen in the correct order. 
    // says to follow the pointer to the integer it points to, 
    // and then decrement the integer. 

      (*pCount)--; 

} while (*pCount >0); 

}

INT主(INT的argc,常量字符* argv的[] ){

int counter; 
int * pCounter = &counter; 

std::cout << "How many hellos? "; 
std::cin >> counter; 

hMany(pCounter); // passing the address of counter 

std::cout << "counter is: " << counter << std::endl; 
std::cout << "*pCounter is: " << *pCounter << std::endl; 

return 0; 

}

+1

_Pointers_ not _Points_。 – deepmax

+10

请获得[*好*书](http://stackoverflow.com/q/388242/1171191)。 'C++'不是一种要在24小时内学习的语言。你只会学习那些难以忘却的可怕做法,并会给你一种对语言的不好印象。 – BoBTFish

+0

我认为下面的答案缺少一些东西。在上面的代码中,没有指针被传递给函数。 – john

回答

0

通过点(* pCounter)与参数 (counter)有什么好处?

有没有“利”,这是你应该如何通过由指针指向pCounter的价值。

hMany(*pCounter) - >提领pCounter获得价值

2
int counter; 
int * pCounter = &counter; 

... 

hMany(*pCounter); // Pass by value 

hMany(counter); // Pass by value 

什么是经过点(*的Pcounter)VS参数 (计数器)有什么好处?

在这种情况下没有什么,这只是一个教育的例子。它显示你可以取消引用一个指针,并通过*得到它的值。

此外,这两种情况都是传递价值。

在实践中,你应该避免在默认情况下使用指针,除非你有一个很好的理由。

0

的差是可变的范围。

如果你通过它被复制到堆栈中函数的局部范围的变量。 更改值是在本地副本中调用的函数完成的,如果情况如此,那么如果传递大型变量,则使用第一个空间;第二,需要从函数中返回值(因为堆栈一旦被破坏回去)

你可以认为它像这样:

堆栈:

当前函数栈:

[ ] 
[ ] 
[ ] 
[ ] 

,当你调用另一个1个元素添加

[ ] 
[ ] 
[ ] 
[ ] 
[newel] 

,当你离开的功能将被删除所以它这样再次

[ ] 
[ ] 
[ ] 
[ ] 

为newElement价值不能被不信任。

但如果这是指针的副本,你不改变指针复制值 但地方它指向太多的价值,使堆积会是这样

[ ] 
[ actual_value] 
[ ] 
[ pointer_to_value] 

比你调用函数

[ ] 
[ actual_value] 
[ ] 
[ pointer_to_value] 
[ pointer_to_value_copy] 

这将改变堆栈上的actual_value并退出pointer_to_value的删除副本

[ ] 
[ actual_value**changed] 
[ ] 
[ pointer_to_value] 
2

使用*pCountercounter没有区别。在这两种情况下,您都会通过变量counter。但是,如果您确实通过指针本身,则会得到不同的行为。

考虑稍有不同的程序:

#include <iostream> 

void hMany(int* pCount); // hMany function prototype 


void hMany(int* pCount){ 

    do { 
     std::cout << "Hello...\n"; 
     --*pCount; 

     } while (*pCount >0); 

} 


int main (int argc, const char * argv[]) { 


    int counter; 
    int * pCounter = &counter; 

    std::cout << "How many hellos? "; 
    std::cin >> counter; 

    hMany(pCounter); 

    std::cout << "counter is: " << counter << std::endl; 
    std::cout << "*pCounter is: " << *pCounter << std::endl; 

    return 0; 
} 

在这种情况下,你的输出将是:

How many hellos? 2 
Hello... 
Hello... 
counter is: 0 
*pCounter is: 0 

通过在指针传递counter(字面意思,地址在记忆counter),则允许该功能通过其内存位置更改counter

+1

注意,为了这个目的,人们总是会喜欢参考。 – cHao

+0

感谢大家提供的帮助。它通过指针清除了我很多困惑。价值被传递,而不是地址。我现在明白了* WhatEver获得的价值和whatEver是存储值的地址。但是,我复制了代码并将其粘贴到Xcode 4.2上,但我没有遇到与您相同的结果。我有一个void hMany(int * pCount)的错误!没有以前的功能hMany原型。我把它改为void hMany(int * pCount);错误消失了。我仍然有错误* pCount--; !表达结果未使用。有任何想法吗? – robles1999

+0

啊,道歉,我忘了更新原型。我会编辑答案。至于其他编译器投诉,这听起来像是警告而不是错误。 '* pCount - '返回以前的值以及递减,编译器指出你没有使用它。它真的拒绝编译? (无论如何,' - * pCounter'可能更好,所以我会将代码更改为此。) – Chowlett

0

指针实际上包含内存地址和内存地址表示的值的类型信息。它可以用来传递内存地址中的实体,而不用再次实例化它来改变它的值或类似的东西。

但是在你的程序中,我看不到使用指针带来的好处,因为在你的情况下,一个C++函数hMany在你的情况下在内部重新实例化它的参数中的值类型'int count'。

正如其他答案指出的那样,您需要将计数指针传递给函数。在这种情况下,C++函数将类似地重新实例化其参数中的值类型,但由于类型是指针,程序将在C++函数内部具有正确的内存地址count,并且可以正确更改正确标识的值。