cout 2 dimensions char array using void *

Hi I'm trying to print the words in the array using void*, but I get only the first word ,
and the rest is *&^&*&^, what is the problem ?

this is print function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void print(void *arr, int num, int type)
{
     switch (type)
     {
        case sizeof(int) : 
             for(int x=0; x<num; x++)
	       cout<<(*((int *)arr+x))<<" ";
	     break;

	case sizeof(double)  : 
             for(int x=0; x<num; x++)
	       cout<<(*((double *)arr+x))<<" ";
	     break;

	case sizeof(char [10]) : 
             for(int y=0; y<num; y++)
		cout<<(*(char **)arr+(y*sizeof(char*)*10))<<" ";
	     break;
    }
}


and this is the main code:
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
void main()
{
	int num;
	int choice=menu();
	
	cout<<"Enter values number: ";
	cin>>num;
	if (choice==1){
			int *arr = (int *)new int[num]; 
			for (int x=0; x<num; x++)
				cin>>arr[x];

			cout<<"array before: ";
			print (arr, num, sizeof(int));
			cout<<endl;
			bubbleSort(arr, num, sizeof(int), compareNum);
			cout<<"array after: ";
			print (arr, num, sizeof(int));
			cout<<endl;
			delete []arr;
	}
	else if (choice==2){
			double *arr = (double *)new double[num]; 
			for (int x=0; x<num; x++)
				cin>>arr[x];
			
			cout<<"array before: ";
			print (arr, num, sizeof(double));
			cout<<endl;
			bubbleSort(arr, num, sizeof(double), compareNum);
			cout<<"array after: ";
			print (arr, num, sizeof(double));
			cout<<endl;
			delete []arr;
	}
	else if (choice==3){
			char **arr=new char*[num];
			char word[10]={NULL};

			for (int x=0; x<num; x++)
			{
				arr[x] = new char[10]; 
				cin>>word;
				strcpy(arr[x],word);
			}
			cout<<endl<<sizeof(char[10])<<endl;
			cout<<"array before: ";
			print (arr, num, sizeof(char[10]));
			cout<<endl;
			bubbleSort(arr, num, sizeof(char[10]), compareStr);
			cout<<"array after: ";
			print (arr, num, sizeof(char[10]));
			cout<<endl;

			for (int x=0; x<num; x++)
				delete []arr[x];
			delete arr;
	}

	
	system("pause");
}
Last edited on
Your code is horrendous.
(*(char **)arr+(y*sizeof(char*)*10))
I know this line is the problem core, I don't know how to write it right.
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
template< typename T > void print( const void *arr, int num )
{
    const T* array = static_cast< const T* >(arr) ;
    for( int i=0 ; i < num ; ++i ) std::cout << array[i] << '\n' ;
}

template< typename T > void safer_print( const T *array, int num )
{ for( int i=0 ; i < num ; ++i ) std::cout << array[i] << '\n' ; }

int main()
{
    enum { N = 3, NCHARS = 10 } ;

    char a[N][NCHARS] = { "abcd", "efgh", "ijkl" } ;
    print< char[NCHARS] >( a, 3 ) ;
    safer_print( a, 3 ) ;

    typedef char cstr_t[NCHARS] ;
    cstr_t* b = new cstr_t[N] ;
    for( int i=0 ; i < N ; ++i )
    {
        std::string word ;
        std::cin >> word ;
        std::strcpy( b[i], word.substr( 0, NCHARS-1 ).c_str() ) ;
    }
    print<cstr_t>( b, 3 ) ;
    safer_print( b, 3 ) ;
}
Thank u but I have to use in void* and not in templates
void main()
my eyes twitching
This line

(*(char **)arr+(y*sizeof(char*)*10))

should be

(*(char **)arr+(y*sizeof(char)*10)) // Note: sizeof(char) not sizeof(char*)
> Thank u but I have to use in void* and not in templates

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
enum type_t { INT, DOUBLE, CHAR_ARRAY_10 } ;

void print( const void *arr, int num, type_t type )
{
    switch( type )
    {
        case INT :
        {
            const int* p = static_cast< const int* >(arr) ;
            for( int i=0 ; i<num ; ++i ) std::cout << p[i] << ' ' ;
        }
        break ;

    case DOUBLE :
        {
            const double* p = static_cast< const double* >(arr) ;
            for( int i=0 ; i<num ; ++i ) std::cout << p[i] << ' ' ;
        }
        break ;

    case CHAR_ARRAY_10 :
        {
            typedef char cstr[10] ;
            const cstr* p = static_cast< const cstr* >(arr) ;
            for( int i=0 ; i<num ; ++i ) std::cout << p[i] << ' ' ;
        }
    }

    std::cout << '\n' ;
}

int main()
{
    enum { N = 3, NCHARS = 10 } ;

    const char a[N][NCHARS] = { "abcd", "efgh", "ijkl" } ;
    print( a, N, CHAR_ARRAY_10 ) ;

    typedef char cstr_t[NCHARS] ;
    cstr_t* b = new cstr_t[N] ;
    for( int i=0 ; i < N ; ++i )
    {
        std::string word ;
        std::cout << "? " && std::cin >> word ;
        std::strcpy( b[i], word.substr( 0, NCHARS-1 ).c_str() ) ;
    }
    print( b, N, CHAR_ARRAY_10 ) ;

    const int c[] = { 0, 1, 2, 3, 4, 5 } ;
    print( c, sizeof(c)/sizeof(*c), INT ) ;

    const double d[] = { 1.2, 3.4, 5.6, 7.8 } ;
    print( d, sizeof(d)/sizeof(*d), DOUBLE ) ;
}
thank you very helpful,

now Iv'e a problem with bubble sort, I need to sort the array and replace the strings pointers (replace the pointers) but I can't set the pointer to the new address.

this is the function code (the problem is only in char[10] part):
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
void bubbleSort(void *base, int num, int size, int (comparator) (const void*, const void*, int size))
{
	void *a, *b, *temp=NULL;

	for(int x=0; x<num; x++)
	{
		for(int y=0; y<num-1; y++)
		{
			if (comparator( ((char *)base+(y*size)), ((char *)base+((y+1)*size)), size ))
			{
				if (size==sizeof(char[10]))
				{
					a=(*((char **)base+y));
					b=(*((char **)base+y+1));

					temp=a;
					a=b;
					b=temp;
					
				}
				else if (size==sizeof(int))
				{
					temp = new int;
					a = ((char *)base+(y*size));
					b = ((char *)base+((y+1)*size));

					memcpy(temp, a, size);
					memcpy(a, b, size);
					memcpy(b, temp, size);

					delete temp;
				}
				else if (size==sizeof(double))
				{					
					temp = new double;
					a = ((char *)base+(y*size));
					b = ((char *)base+((y+1)*size));

					memcpy(temp, a, size);
					memcpy(a, b, size);
					memcpy(b, temp, size);

					delete temp;
				}
			}
		}
	}
}
Last edited on
Topic archived. No new replies allowed.