C语言的全局指针? [英] Global Pointer in C?

查看:454
本文介绍了C语言的全局指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道通常在其声明后分配一个指针,但是我想知道是否有任何方法可以在C中创建全局指针.例如,下面的代码:这是一种好习惯吗?

I know a pointer is usually assigned upon its declaration, but I wondering if there's any way to create a global pointer in C. For example my code below: is it a good practice?

static int *number_args = NULL;

void pro_init(int number)
{
    number_args = &number;   /* initialize the pointer value -- is this okay? */

}

推荐答案

  1. 避免使用全局变量-他们是一个坏主意,通常会引发问题.
  2. 您要在堆栈上获取变量的地址.它将在某处重复使用,因此会产生意想不到的结果.

如果您觉得需要全局指针(为什么?),请在离开堆时进行初始化.

If you feel the need (why?) to have a global pointer then initialise if off the heap.

这篇关于C语言的全局指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆