I'm trying to do function remove extension of a name is type wchar_t or char, inside for() i cast void pointer to type wchar_t or char, then there was an error:
Expression must be a pointer to a complete object type.
Here is sample code to show an instance of the issue by itself.
int RemoveExtension(
void *p_str, /* Name */
bool isWchar /* true:wchar_t, false:char */
) {
int ii, len;
// Get length
len = (isWchar) ? wcslen( (wchar_t *)p_str ) : strlen( (char *)p_str );
// Remove extension
/* wchar_t */
if( isWchar ) {
for( ii = 0; ii < len; ii++ ) {
if( L'.' == (wchar_t *)p_str[ii] ) { //Error here
(wchar_t *)p_str[ii] = L'\0'; //Error here
break;
}
}
}
/* char */
else {
for( ii = 0; ii < len; ii++ ) {
if( '.' == (char *)p_str[ii]) { //Error here
(char *)p_str[ii] = '\0'; //Error here
break;
}
}
}
return 0;
}
I'm not understanding what this error is trying to say. There is no object or struct here.
To understand the error message, you have to know two things:
void
is a special case of an incomplete type : it can't ever be completed, because it's unknown ( void *
is the generic pointer type and may point to an object of any type ). So, your compiler just tells you that p_str[ii]
is invalid because you attempt to index on a pointer of type void *
.
The source of the problem is simple: indexing has a higher precedence than casting, therefore just use parantheses to solve this problem:
((wchar_t *)p_str)[ii]
I dont know why you get exactly that error, but according to this table expression
(wchar_t *)p_str[ii]
is (wchar_t *)(p_str[ii])
not ((wchar_t *)p_str)[ii]
,
probably it's not what you want
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.