c - 怎么了?为什么指针保持不变?

标签 c

我学习 C 编程一段时间,我必须创建一个程序,其中包含一个函数 void hello(),显示单词“Hello :)”和数字,以及函数 hello() 被调用的次数。下面的代码显示“Hello”,但函数调用的数量保持不变。我只是想知道出了什么问题以及为什么它没有按预期工作。

#include <stdio.h>

int main(void) {

void hello(int *p_number);
int number = 1, i;
int* p_number = number;

for (i = 1; i <= 10; i++){
    hello(&p_number);
    printf("Number in cyclus = %d\n", number);
    number++;
}

return 0;
}

void hello(int *p_number){
    printf("number of calling = %d, Hello :)\n", *p_number);
}

最佳答案

你需要

int* p_number = &number;

hello(p_number);

在调用站点。

即将 p_number 设置为 number地址。并且一定要调高编译时的警告级别并阅读它们!在 hello 中维护指针有相当多的冗余;大概这是为了练习?

关于c - 怎么了?为什么指针保持不变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49651520/

相关文章:

c - 如果在循环中多次调用 malloc() 和 realloc() 则多次 free()ing

c - C中指向char和字符数组的指针

php - PHP uniqid()源码相关问题

c++ - 准确设置 winsock 选择超时

c - 仅在将数组设为私有(private)后才进行多线程加速

c - 在 linux 环境中使用 C 中的 TCP 的服务器客户端应用程序,

c - Lua 用户数据数组访问和方法

objective-c - 从源代码中删除函数后的 undefined symbol

CMake 找不到库

c++ - 如何找出已使用的内存量