如何在main()中定义全局变量 [英] how to define global variable in main()

查看:652
本文介绍了如何在main()中定义全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如何在main()中定义全局变量?

我在问,因为我在main中有一个数组,其大小已确定通过

输入,所以定义必须在main(或其他一些功能)。

我需要在我的其他函数中使用该数组,所以我想要它是全球的b $ b。我尝试使用extern关键字,但是我发现了一些错误,所以我提出

那不是它。

提前感谢

Davor :-)



How to define global variable in main()?
I''m asking because I have an array in main, whose size is determined by
input, so the definition has to be in main ( or in some other funcion ).
And I need to use that array in my other functions, so I want it to be
global. I tryed using extern keyword, but I gut some error, so I supose
that''s not it.
thanks in advance
Davor :-)

推荐答案

Davor写道:


如何定义main()中的全局变量?
我在问,因为我在main中有一个数组,其大小由
输入决定,因此定义必须在main(或其他一些函数)中。
我需要在我的其他函数中使用该数组,所以我希望它是全局的。我尝试使用extern关键字,但是我发现了一些错误,所以我提到
那不是它。

提前感谢
Davor: - )


How to define global variable in main()?
I''m asking because I have an array in main, whose size is determined by
input, so the definition has to be in main ( or in some other funcion ).
And I need to use that array in my other functions, so I want it to be
global. I tryed using extern keyword, but I gut some error, so I supose
that''s not it.
thanks in advance
Davor :-)




有没有理由说你不需要通过那个阵列来获取需要访问它的每个函数





Is there a reason why you don''t just past that array to every function
that needs to access it?


Jeff写道:
Davor写道:
Davor wrote:


如何定义全局变量在main()?
我在问,因为我在main中有一个数组,其大小是由输入确定的,所以定义必须在main(或其他一些
)功能)。我需要在我的其他函数中使用该数组,所以我希望它是全局的。我尝试使用extern关键字,但是我发现了一些
错误,所以我说这不是它。

提前感谢Davor: - )


How to define global variable in main()?
I''m asking because I have an array in main, whose size is determined
by input, so the definition has to be in main ( or in some other
funcion ). And I need to use that array in my other functions, so I
want it to be global. I tryed using extern keyword, but I gut some
error, so I supose that''s not it.

thanks in advance Davor :-)



你有没有理由不将这个数组转移到需要访问它的每个函数上?



Is there a reason why you don''t just past that array to every function
that needs to access it?




s / past / pass / g


唉!



s/past/pass/g

Ugh!


文章< 3F ********** **@yahoo.com>,Davor写道:
In article <3F************@yahoo.com>, Davor wrote:
如何在main()中定义全局变量?我问,因为我在main中有一个数组,其大小由输入决定,所以定义必须在main(或其他一些功能)中。
我需要在我的其他函数中使用该数组,所以我希望它是全局的。我尝试使用extern关键字,但我发现了一些
错误,所以我说它不是它。
How to define global variable in main()? I''m asking because I
have an array in main, whose size is determined by input, so
the definition has to be in main ( or in some other funcion ).
And I need to use that array in my other functions, so I want
it to be global. I tryed using extern keyword, but I gut some
error, so I supose that''s not it.




#include< stdlib.h>


int * global_array;


int get_user_input(void);

void go(void);


int main(无效)

{

int x = get_user_input();

global_array = malloc (x * * global_array);

if(global_array!= 0){

go();

}

返回0;

}

/ *等... * /


-

Neil Cerutti



#include <stdlib.h>

int *global_array;

int get_user_input(void);
void go(void);

int main(void)
{
int x = get_user_input();
global_array = malloc(x * *global_array);
if (global_array != 0) {
go();
}
return 0;
}
/* etc... */

--
Neil Cerutti


这篇关于如何在main()中定义全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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