Question:
C++ problem. help to understand a line?
?
2011-11-25 05:52:09 UTC
Actually i was looking for a example of tic tac toe program and found my requirement but was unable to understand one line. please tell. it's type of declaration.

char square[10] = {'0','1','2','3','4','5','6','7','8','9'};

it's writen outside main.
please give me alternative also for this line if you know it.
Three answers:
Silent
2011-11-25 06:10:23 UTC
This line is declaring a variable called square, which is of type char[] (i.e., an array of characters). The array has ten elements.



It also initializes the array to contain characters representing the ten decimal digits, 0 - 9, in sequence.



If it is written outside of any function, then square is a global variable.
?
2011-11-25 15:09:52 UTC
As it is outside the main function it is global declaration i.e the character array square[10] can be accessed by all the functions or methods.The allocation would be like this

square[0]=0

square[1]=1

.

.

.

.

square[8]=8

square[9]=9
?
2011-11-25 14:09:27 UTC
if it's written out side main it mean global declaration.

i.e it is available throughout program.



if anything declared with in main or particular structure it's only available in that main/structure.



quick ex :



main ()

{

int a;

.

.

.

}

structure A

{

int b;

.

.

.

}



a is only available in main and b is only available in structure A.



generally variables die when they reach" } "sign in program.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...