MESSAGE
DATE | 2010-03-04 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] C++ Workshop _ Syntax Basics
|
> > Explicit Cast: > > For a variety of reasons, one might need to cast data intentionally. > There are two styles to do this, the older C style and the newer C++ > standard. First the new style. > > The kinds of New Style Casting: > > static_cast, dynamic_cast, const_cast, and reinterpret_cast. Syntax for > these casts follows the following conventions: > > int int_variable = static_cast(char_variable); > > cast_name(variable) in the general form. I'm not going to yet > explain the differences at this point, but will come back to it soon > enough. I will say that the result is to forcefully convert the data > from one type to another, in the case above, from a char to a int. > > In the C style, parenthesis are used to make the cast: > > char letter; > int var = (int) letter; > > casts the value of letter to an int. > > > Aggregate Data Types: > Most of the action involving your program will involve more than a single > indepent integer, char or float. Groups of data types together creates > most of the useful. C and C++ gives multiple tools for handling these > agregate data types. The key element is the C style array. An array's > syntax is declared, defined and assigned like the elementry data types, > and looks like this, using the square bracket operator: > > char mystraing[]; // Declares an array of chars without dimension > char mystring[100]; //Declares an array of chars with 100 chars within > //it > char * mystring[]; //Declares an array of pointers to chars similar to > //the paramenter of main char * argv[]; > > > One can assign and declare your array with a single statement. When > doing so, C and C++ has several syntax tools to help you create many > necessecary subtle data contructions that you need for your programming. > The comments below outlines these examples and behaviors. > > char mystring[] = "This is our first string"; //Declares a char array of > //27 chars which is terminated with a null value > > char mystring[] = {'a','b','c','d','e'}; //Creates an array of 5 chars. > > int matrix[100] = {1.2,3,4,5}; //This creates an array of 100 > //integers filling the first 5 locations with 1,2,3,4,5 > //and then adds 0's or NULLS to the remaining 95 indexed > //locations > > int matrix[100] = {'1'.'2','3','4','5'}; //This creates an array of 100 > //integers where the equivilent of the short > //intergers which represent the ascii values for > //the characters '1' and '2' etc, and then fills > //the rest of the array with zeros. It is > //similar to the next statement (but not > //exactly) > > char matrix[100] = "12345"; // This example creates a string literal > //"12345" which ends in a null, and then > //pads the rest of the array with nulls. The > //result is the same as above, but via a > //different mechanism because all string > //literals end in null. The above examples > //has implicit promotion from char to integer > //types. This example must be a char type, > //otherwise the the compiler will not accept > //the assignment. Furthmore, only the care > //type will print a string when asked. The > //top example needs an explicit cast. See > //and thry this example for a demonstration. > > #include > using namespace std; > > int main(int argc, char * argv[]){ > unsigned short int matrix[100] = {'1','2','3','4','5'}; > char matrix2[100] = "12345"; > cout << "First Martix "<< matrix << endl; > cout << "Second Matrix " << matrix2 << endl; > for(int i=0;i<5;i++){ > cout << matrix[i] << endl; > } > for(int i=0;i<5;i++){ > cout << static_cast(matrix[i]) << endl; > } > return 0; > } > > ruben-at-www2:~/cplus> g++ -Wall test.cc -o test.bin > You have mail in /var/mail/ruben > ruben-at-www2:~/cplus> > ruben-at-www2:~/cplus> ./test.bin > First Martix 0xbfc98c3c > Second Matrix 12345 > 49 > 50 > 51 > 52 > 53 > 1 > 2 > 3 > 4 > 5 > ruben-at-www2:~/cplus> > > > Notice that the second matrix prints a seemingly random number. That > number is actually the memory address that matrix points at. It acts > like a pointer in the context of cout. The for loop itself will be > looked at more closely when we discuss flow control operators. > > We can not mix data types in an array. An array is defined by as a > single data type only. > > Arrays are indexed starting with zero. You have to know the size of > your arrays, otherwise you can walk past the end of them into the > undefined sections of your memory. Usually this will cause a > segmentation fault, but not always. Arrrays have syntax that allow them > to be converted to pointers. Pointers is the next section, after we > look at arrays, and we wil look closely at pointers and arrays at soon. > > Arrays can have two dimensions like this: > > float matrix[4][7]; > > That declares an array of 4 columns of nine rows (c before r), > > for example, we can initialize such an array like this: > > float matrix[4][7] = { > { 2.11, 2.22, 2.33, 2.44, 2.55, 2.66, 2.77 }, > { 3.11, 3.33, 3.33, 3.44, 3.55, 3.66, 3.77 }, > { 4.11, 4.44, 4.33, 4.44, 4.55, 4.66, 4.77 }, > { 5.11, 5.55, 5.33, 5.44, 5.55, 5.66, 5.77 } > }; > > > or you can drop in inside curly braces and the compiler will do the > rest.. > > > float matrix[4][7] = { > 2.11, 2.22. 2.33, 2.44, 2.55, 2.66, 2.77 , > 3.11, 3.33. 3.33, 3.44, 3.55, 3.66, 3.77, > 4.11, 4.44. 4.33, 4.44, 4.55, 4.66, 4.77 , > 5.11, 5.55. 5.33, 5.44, 5.55, 5.66, 5.77 > }; > > > Although we stupid humans conceptualize this as columns and rows, in RAM > this is stored as a single linear block of memory. > > There are alot of minefields with two dimensional arrays, and this > program shows some of them: > > #include > using namespace std; > > int main(int argc, char * argv[]){ > unsigned short int matrix[100] = {'1','2','3','4','5'}; > char matrix2[1000] = "12345"; > float dmatrix[4][7] = { > { 2.11, 2.22, 2.33, 2.44, 2.55, 2.66, 2.77 }, > { 3.11, 3.33, 3.33, 3.44, 3.55, 3.66, 3.77 }, > { 4.11, 4.44, 4.33, 4.44, 4.55, 4.66, 4.77 }, > { 5.11, 5.55, 5.33, 5.44, 5.55, 5.66, 5.77 } > }; > > float * track; > > > cout << "First Martix "<< matrix << endl; > cout << "Second Matrix " << matrix2 << endl; > for(int i=0;i<5;i++){ > cout << matrix[i] << endl; > } > for(int i=0;i<5;i++){ > cout << static_cast(matrix[i]) << endl; > } > for(int i=0;i<100;i++){ > cout << &matrix[i] << endl; > } > for(int i=0;i<5;i++){ > cout << "STRING " << reinterpret_cast(&matrix2[i]) << endl; > } > track = *dmatrix; > float * last = &dmatrix[3][6]; > > for(int count = 0; track <= last; track++){ > cout << "Position ==>" << count++ << "\tMemory Location==>"< |
|