Search
 
SCRIPT & CODE EXAMPLE
 

CPP

how to define global array in c++ in a scope

class C {
 int [] x;

 void method A(int size)
 {
   x = new int[size];   // Allocate the array
   for(int i = 0; i < size; i++)
      x[i] = i;         // Initialise the elements (otherwise they contain random data)
   B();
   delete [] x;         // Don't forget to delete it when you have finished
                        // Note strange syntax - deleting an array needs the []
 }

 void method B()
 {
   int n;
   cin >> n;
   cout << x[n];
   // Be warned, if the user inputs a number < 0 or >= size, 
   // you will get undefined behaviour!
  }
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: C is widely used for systems-level software and embedded systems development. 
Cpp :: windows install cppcheck 
Cpp :: 12 to december in c++ code 
Cpp :: how to find second smallest element using single loop 
Cpp :: 2160. Minimum Sum of Four Digit Number After Splitting Digits leetcode solution in c++ 
Cpp :: C++ concept simple requirements 
Cpp :: Code Example of Switch Statement in C++/Java 
Cpp :: The program must enter a natural number n from the console and find the number previous to n that is not divisible by 2 , 3 and 5 . 
Cpp :: how can I convert each and every element of string push into set in c++? 
Cpp :: C++ Things to Remember 
Cpp :: how to srt vector array 
Cpp :: online convert c++ code to assembly language 
Cpp :: reference variablesr in c++ 
Cpp :: print all chrchetrs of a string c++ 
Cpp :: passing a 2d array cpp 
Cpp :: random c++ 
Cpp :: c++ set element at index 
Cpp :: declare a variable in cpp 
Cpp :: sum function in c++ 
Cpp :: ceil value in c++ using formula 
Cpp :: english to french typing online 
C :: find string in all files powershell 
C :: print an array in c 
C :: zizag c 
C :: how to auto run something on cmd 
C :: C program to count number of digits in a number 
C :: how to read space separated words in c 
C :: if statement c short form 
C :: how to combine strings in c 
C :: turn a char into an int in c 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =