Help understanding pointers

Can someone please explain to me the switch statement? more specifically, I understand how the data_type switches between int, char, and float, but how does the cout statement work in each of the cases?
Also how is it that in the void function, although the expression void is a pointer (void* a), during print_matrix in the last cout lines, instead of putting in just the matrix name (ex small_matrix) you need to add the reference & sign?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  #include <iostream>
#include <string>

using namespace std;


void Print_Matrix(void* a , int rows, int columns, char data_type)
{
 for(int i = 0 ; i < rows ; i++)
   for(int j =0 ; j < columns ; j++)
   {
       switch (data_type)
       {
        case 'i' : cout << *( (int *)a + (i * columns) + j) << " " ;
                   break;
        case 'c' : cout << *( (char *)a + (i * columns) + j) << " " ;
                   break;
        case 'f' : cout << *( (float *)a + (i * columns) + j) << " " ;
                   break;
        default : cout << "Invalid data type" << endl;
                  return;
        }

       if ( j == columns - 1)
          cout << endl;
   }
   cout << endl;
}


int main()
{

int little_matrix[3][3] = { 1,2,3,4,5,6,7,8,9 };

int big_matrix[5][5] = { 10, 11, 12, 13, 14,
                         16, 17, 18, 19, 20,
                         22, 23, 24, 25, 26,
                         28, 29, 30, 31, 32,
                         34, 35, 36, 37, 38 };

char array_of_chars[2][2] = { 'a', 'b', 'c', 'd'};

float array_of_floats[4][4] = { 1.23, 2.34, 3.33, 4.44,
                                5.55, 6.66, 7.77, 8.88,
                                9.99, 10.10, 11.11, 12.12,
                                13.13, 14.14, 15.15, 16.16};


cout << "The little matrix is : " << endl;
Print_Matrix(&little_matrix[0][0], 3, 3, 'i');

cout << "The big_matrix is : " << endl;
Print_Matrix(&big_matrix[0][0], 5, 5, 'i');


cout << "The character array is : " << endl;
Print_Matrix(&array_of_chars[0][0], 2,2, 'c');

cout << "The float array is : " << endl;
Print_Matrix(&array_of_floats[0][0], 4,4, 'f');

return 0;
}
cout << *( (int *)a + (i * columns) + j) is equivalent to:
1
2
3
int *ip = reinterpret_cast<int *>a;    // interpret a as a pointer to an integer (actually an array of integers
int index = i*column+j;   // This is the index of the item to print
cout << *(ip+index);      // print out the index'th item 

Also how is it that in the void function, although the expression void is a pointer (void* a), during print_matrix in the last cout lines, instead of putting in just the matrix name (ex small_matrix) you need to add the reference & sign?

When the code does Print_Matrix(&array_of_chars[0][0], 2,2, 'c'); it's passing the address of the first time in array_of_chars. You could just as easily have called it like this: Print_Matrix(array_of_chars, 2,2, 'c'); It's a matter of style.
So just to be clear, the 3 cases are simply pointing to the address of the arrays, and then prints them out one by one?
Yes. Lines 54, 58 and 61 call Print_Matrix() but each one passes a pointer to a different kind of array. The switch() statement figures out which kind of array was passed and prints it accordingly.
Topic archived. No new replies allowed.