c++ - 将指针传递给函数不返回值

标签 c++ pointers

在下面的例子中我得到 NumRecPrinted = 0 ,即 num 是 0

int main()
{
    int demo(int *NumRecPrinted);
    int num = 0;
    demo(&num);
    cout << "NumRecPrinted=" << num;    <<<< Prints 0
    return 0;
}

int demo (int *NumRecPrinted)

{
    int no_of_records = 11;
    NumRecPrinted = &no_of_records;
}

最佳答案

您正在将地址分配给指针,而不是将值分配给所指向的对象。试试这样吧

int demo (int *NumRecPrinted)
{
     int no_of_records = 11;
     *NumRecPrinted = no_of_records; 
} 

关于c++ - 将指针传递给函数不返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9212687/

相关文章:

android - 使用 NDK 在 C/C++ 中将 YUV 解码为 RGB

c++ - 帮助指针算术

c - 为什么将结构地址转换为 int 指针、取消引用并将其用作语句中的 LVALUE 会使我的微 Controller 崩溃?

c++ - "*&var"会返回存储在 "var"中的值吗?

c++ - 目标名称 "C:/path/to/lib/file.lib"已保留或对某些 CMake 功能无效

C++ 独立函数文件

c++ - 编译器向空类声明添加了什么?

c++ - 获取 undefined symbol : __asan_memset when trying to use Clang address sanitizer

c++如何将指针分配给二叉树中的父节点

c++ - 内存问题