Ok this has been become sooo confusing to me. I just don't know what is wrong with this assignment:
void *pa; void *pb;
char *ptemp; char *ptemp2;
ptemp = (char *)pa;
ptemp2 = (char *)pb;
Can anyone tell me why I'm getting this error:
error: invalid conversion from ‘void*’ to ‘char*’
Actually, there must be something wrong with your compiler(or you haven't told the full story). It is perfectly legal to cast a void* to char*. Furthermore, the conversion is implicit in C (unlike C++), that is, the following should compile as well
char* pChar;
void* pVoid;
pChar = (char*)pVoid; //OK in both C and C++
pChar = pVoid; //OK in C, convertion is implicit
I just tried your code in a module called temp.c. I added a function called f1.
void *pa; void *pb;
char *ptemp; char *ptemp2;
f1()
{
ptemp = (char *)pa;
ptemp2 = (char *)pb;
}
On Linux I entered gcc -c temp.c, and this compiled with no errors or warnings.
On which OS are you trying this?
Related
The output of this program is:
XBCDO
HELLE
Can someone explain why this is so?
#include<stdio.h>
void swap(char **p, char **q) {
char *temp = *p;
*p = *q;
*q = temp;
}
int main() {
int i = 10;
char a[10] = "HELLO";
char b[10] = "XBCDE";
swap(&a, &b);
printf("%s %s", a, b);
}
You are confused about the difference between pointers and arrays. (It is a confusing part of the language.) swap expects pointers to pointers, but you have given it pointers to arrays. This is such a serious mistake that GCC warns about it even if you don't turn on any warnings (it ought to issue hard errors, but some very, very old code does stuff like this intentionally and they don't want to break that).
$ gcc test.c
test.c: In function ‘main’:
test.c:16:10: warning: passing argument 1 of ‘swap’ from incompatible pointer type [-Wincompatible-pointer-types]
swap(&a, &b);
^
test.c:3:1: note: expected ‘char **’ but argument is of type ‘char (*)[10]’
swap(char **p, char **q)
^~~~
test.c:16:14: warning: passing argument 2 of ‘swap’ from incompatible pointer type [-Wincompatible-pointer-types]
swap(&a, &b);
^
test.c:3:1: note: expected ‘char **’ but argument is of type ‘char (*)[10]’
swap(char **p, char **q)
^~~~
The mistake causes the program to have undefined behavior - it doesn't have to do anything that makes any sense at all.
The program you probably were trying to write looks like this:
#include <stdio.h>
static void swap(char **p, char **q)
{
char *temp = *p;
*p = *q;
*q = temp;
}
int main(void)
{
char a[10] = "HELLO";
char b[10] = "XBCDE";
char *c = a;
char *d = b;
swap(&c, &d);
printf("%s %s", c, d);
}
The output of this program is XBCDE HELLO, which I think is what you were expecting. c and d are actually pointers, and they are set to point to the first elements of the arrays a and b; swap works as expected when applied to c and d.
If it doesn't make any sense that c and d are different from a and b, you need to get your hands on a good C textbook and you need to read the chapter on pointers and do all the exercises. (If it doesn't have at least one entire chapter on pointers, with exercises, it's not a good C textbook.)
I read a unit test, that checks for invalid free or double-free:
int main() {
char *a = (char*) my_malloc(200);
char *b = (char*) my_malloc(50);
char *c = (char*) my_malloc(200);
char *p = (char*) my_malloc(3000);
(void) a, (void) c;
memcpy(p, b - 200, 450);
my_free(p + 200);
printstatistics();
}
Why do we need to cast char* to void and what happens in memory when we do this cast?
(void) a, (void) c;
is a common way to get rid of compiler warnings about unused variables. Since those two variables are initialised only and not used later, most compilers would issue warnings about it. Since this is apparently some kind of a test of memory allocation they are not used on purpose, so someone decided to silence warnings.
i have a simple program that compile in c at visual,
but not compile at CSR10 id and I don't know how to do it simple:
it say error: assignment discards qualifiers from pointer target type;
char *globalString;
int test(void);
void test()
{
globalString = "ABCD";
return;
}
global string (char *) will point to a constant (const char *) "ABCD". So, you discard the "const" qualifier. Since GlobalString isn't a constant, it will warn you about that.
Why this shows warning:
#include<stdio.h>
foo (const char **p)
{
}
int main(int argc , char **argv)
{
foo(argv);
}
But following does not show any warning
char * cp;
const char *ccp;
ccp = cp;
The first code snippet shows warning passing arg 1 of foo from incompatible pointer type. But the second snippet does not show any warning. Both are const pointers
See the C FAQ list
You can cast in order to remove warnings:
foo((const char **)argv);
But as FAQ says: the need for such a cast may indicate a deeper problem which the cast doesn't really fix.
Depending on your compilation flags, you might need an explicit cast when assigning cp's content to ccp.
In the first version you are casting between two different types of pointer not simply adding a const to the pointer.
char ** is a pointer to a (pointer to a char)
const char ** is a pointer to a (pointer to a const char)
As you can see these pointer point to different types similar to the more obviously questionable:
int *i;
double *d;
d = i;
In your second example you see that you can cast from a pointer to a const pointer so if you were to apply this to your situation you would need to have a const pointer to (a pointer to a char).
foo(char * const *p);
#include <stdio.h>
#include <ctype.h>
#define STRING_LEN 500
void stripspaces(char, char, char);
int main(void)
{
char string[STRING_LEN];
char *p1 = string;
char *p2 = string;
printf("Enter a string of up to %d characters:\n", STRING_LEN);
while((*p1++ = getchar()) != '\n') ;
stripspaces(string, *p1, *p2);
getch();
return 0;
}
void stripspaces (char s, char *x1, char *x2){
*x1 = '\0';
x1 = s;
while(*x1 != '\0')
{
if(ispunct(*x1) || isspace(*x1))
{
++x1;
continue;
}
else
*x2++ = *x1++;
}
*x2 = '\0';
printf("\nWith the spaces removed, the string is now:\n%s\n", s);
}
This code is bringing up the following error at the 'stripspaces' function; "passing arg 1 of 'stripspaces' makes integer from pointer without a cast" any help would be excellent.
In case it is not obvious from the code, the program should take in a string and remove all the spaces from it. The function has to remain although I know I can do it without the function.
Your prototype and function definition don't match:
void stripspaces(char, char, char);
vs.
void stripspaces (char s, char *x1, char *x2)
You should change the prototype to
void stripspaces(char, char*, char*);
And in order to make them both work, you should use
void stripspaces(char*, char*, char*);
and
void stripspaces (char *s, char *x1, char *x2)
.
For easier copy & paste, you can use parameter names in the prototype as well.
Both of the answers above are telling you that your function declaration is wrong. Also you are dereferencing pointers when passing them to the function.
stripspaces(string, *p1, *p2);
This turns the call into (char*, char, char) which is not right and will not behave as you expect it to. It is also the source of the particular compiler error you are seeing. The compiler is trying to fit the string(char*) into a char, and thus making an "integer from pointer without cast" since char is basically an 1 byte integer.
Correcting the function declaration would be step one, you want to pass all pointers or you won't be able to manipulate the string.
Fix the declaration and then call the function like this.
stripspaces(string, p1, p2);
You need to change the first argument from char s (single character) to char *s (pointer)