简体   繁体   中英

when assigning to void pointer in C

I thought when assigning a vaule to a pointer, you should use * operator, but I saw a code like

char *a;
void *b;
b = "Hello";
a = b;
printf("%s", a);

This is legal when I compiled it and prints Hello. Doesn't need a pointer to void dereferencing?

This "works" because a void * and a char * are able to hold any type of pointer. You could get into trouble, if for example you used int *a; instead of void *a; .

However, your code isn't dereferencing a void pointer, and the printf function converts it to a char * when it pulls the argument out of the variable arguments in the list. So there is no dereference of a void pointer in your code. If your pointer didn't perfectly convert to a char * (for example if we had an int *a; ) on some types of machines that don't address bytes without "extra information" (some machines have only 'pointers' to whole machne words, and extra information is used to store which byte within that word you want when reading bytes), then your printf may well have failed to operate correctly ["undefined behaviour"].

There is no string type in C. You can treat a char* pointing to the beginning of a char array as a string. And that's exactly how printf treats a here.

As far as I remember, the C Standard demands that char* and void* be interchangeable.

A pointer to void does not need dereferencing, infact, dereferencing a void pointer is illegal. You can obviously cast any pointer to a void pointer, and cast a void pointer to any other pointer type.

That's why:

void *b = "hello world"; worked, so did char *a = b and then printing a out. what happens here is:

char *a; // declares a as a pointer to char
void *b; // declares b as a void pointer(which can hold an address)
b = "Hello"; // 'b' now holds the address, that points to the start of "Hello"
a = b; // now, 'a' contains the address that 'b' does
printf("%s", a); // prints the string, starting from the address pointed by 'a'.

Hence this is perfectly legal.

b = "Hello"; This line allocates a bunch of char memory and assigns its address to void pointer variable b .

void pointers can store address of any other datatype. The only limitations with void pointers are:

  • cannot dereference void pointer for obvious reasons
  • sizeof(void) is illegal
  • you cannot perform pointer arithmetic on void pointers

However GCC assumes that sizeof(void) is 1 and allows pointer arithmetics on void pointers.

a=b; This is the typical char pointer a initialized to the address contained in void pointer b . This is legal but, if misused might have implications.

printf("%s", a); This is a simple printf statement.

Everything in this code is fine.

And yes you need to use * to assign a value to an allocated memory in pointer: Eg:

   char *c=malloc(sizeof(char));
   *c='a';

or

   char a='a';
   char *c=&a;
   *c='b';

Also you would be using the * to initialize another pointer when using double pointers.

   char *a=NULL;
   mymalloc(&a);


   void mymalloc(char **a)
   {
     *a=malloc(10);
     return;
   }

Hope this helps.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM