Click here to Skip to main content
16,016,134 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi,
I am trying to assign a string to a pointer of type void but the result is fatal.
The below is the code is compiled and ran on VC++5.0 for your reference. Please help me fix this.

C#
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char s1[] = "Hi Thomas";
    void* vpTmp;
    char* s2;
    int idx = 0;


    vpTmp = ( char * ) malloc( strlen( s1 ) + 1 );
    while( s1[idx] != '\0' )
    {
        *( char * )vpTmp = s1[idx++];
        ( (int *)vpTmp )++;
    }
    ( (int * )vpTmp )++;
    *( char * )vpTmp = '\0';
    idx++;
    (int * )vpTmp = ( int * )vpTmp - idx;
    printf("The string is : %s\n", vpTmp );
    s2 = ( char * ) vpTmp;
    printf("The string is: %s\n", s2 );
    return 0;
}
Posted
Comments
Timberbird 15-Sep-11 17:22pm    
Maybe the reason is that you are incrementing your vpTmp as int* and accessing values to it like char*?
[no name] 15-Sep-11 17:25pm    
i am doing it because each time in a loop i want to assign character value to it and then increment the address. Since i am trying to convert the void pointer to string before beginning the loop.
Timberbird 16-Sep-11 2:19am    
Yes, I can see that, but these types are of different size :). As Chuck o'Toole mentioned in his solution, int usually takes four bytes while char occupies one byte only

C#
vpTmp = ( char * ) malloc( strlen( s1 ) + 1 );
    while( s1[idx] != '\0' )
    {
        *( char * )vpTmp = s1[idx++];
        ( (int *)vpTmp )++;
    }
    ( (int * )vpTmp )++;
    *( char * )vpTmp = '\0';
    idx++;
    (int * )vpTmp = ( int * )vpTmp - idx;
    printf("The string is : %s\n", vpTmp );
    s2 = ( char * ) vpTmp;
    printf("The string is: %s\n", s2 );
    return 0;
}
 
Share this answer
 
Comments
[no name] 17-Sep-11 6:18am    
thanks for your valuable suggestions but i have already implemented the using the same technique to solve the puzzle. :)
Whenever you perform the pointer arithmetic operation on pointer, the result is always dependent on the pointer type (actually the size of type of the pointer). Here you're incrementing the void pointer after casting it to int pointer. Because of which the pointer will be incremented by sizeof(int) not by the sizeof(char).

Please refer to 'Pointer Arithmetic' section of this [^]article.

Have a nice pointer calculations !!!
 
Share this answer
 
v2
"++" does not increment by the constant 1, it increments by the "sizeof(item)" so, if you type is as (int *) then "++" will increment to the next INTEGER. In math terms, the address contained in the pointer will be incremented by 4. Not at all what you want.

If you are treating it as a pointer to char (char *) in every use, declare it as such. Doing math with pointers is tricky, doing math on re-cast pointers is downright dangerous (oh, wait, you found that out).
 
Share this answer
 
Comments
Albert Holguin 27-Sep-11 15:25pm    
see... very right... +5... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900