Why do we pass the link when we have the choice to make the variable external?

Suppose we have an array:

int arr[1000];

and I have a function that works in this array:

void Func(void);

Why is it ever necessary to pass by reference (by changing the void) when I can have arr [1000] as an external variable outside main ()?

  • What is the difference? Is there any difference?
  • Why do people prefer to follow the link rather than make it external? (I myself think that making it external is easier).
+3
source share
9 answers

I think you are asking if global variables are . Quoting a great answer :

, , .

, , , . , (, , ).

, , . , , .

+7

arr, Func , , . , :

  • arr " ", , , , .
  • ( -, , ) , , clobber arr.
  • , ( , , ) ; .

, , .

+10

arr , , Func. .

, , .

: " , ". , ". " " :)

+3

, , , , . , , . , , Func.

( ), , ( , /), .

, , .

+2

; / , , ?

, , . , .

+1

( ) , - , .

, , .

, , , ( , ). , , ( ).

puts.

int puts(const char *s);

C , . , , , puts.

 int my_complicated_code( int x, int y, int z);

, , , , , FILE *. puts fputs, FILE *, , , , my_complicated_code, FILE *, .

 int my_complicated_code(int x, int y, int z, FILE * out_file);

, , my_complicated_code, FILE *, ( ).

. memcpy , . , , .

, , , , , . , , , .

, , , .

+1
+1
  • . "int arr [1000]", , (), "another_function()". , . , .

  • . (1.)

0

This is also a maintenance problem. Why do I want to track some external somewhere when I can just look at a function and see what it should be?

0
source

Source: https://habr.com/ru/post/1771544/


All Articles