Home > Software design >  How to fix warning "Format specifies type 'void *' but the argument has type 'ch
How to fix warning "Format specifies type 'void *' but the argument has type 'ch

Time:10-22

I am getting the output that I want but can't figure out how to get rid of these warnings. Any help is appreciated.

Warnings:

  1. Format specifies type 'void *' but the argument has type 'char' [-Wformat] printf("\nThe pointer variable's value is %p\n", *myString);

  2. "Format specifies type 'void *' but the argument has type 'char' [-Wformat] printf("%p\n", myString[x]);

     #include <stdio.h>
     #include <stdlib.h>
    
     int main() {
    
       char *myString = "Daniel";
       int x;
    
       printf("\nThe pointer variable's value is %p\n", *myString);
       printf("\nThe pointer variable points to %s\n", myString);
       printf("\nThe memory location for each character are: \n");
    
    
       for (x = 0;x < 7;x  ){
        printf("%p\n", myString[x]);
       }
    
     return 0;
     }
    

Ouput:

     The pointer variable's value is 0x44

     The pointer variable points to Daniel

     The memory location for each character are: 
     0x44
     0x61
     0x6e
     0x69
     0x65
     0x6c
     (nil)

CodePudding user response:

For starters these calls

printf("\nThe pointer variable's value is %p\n", *myString);

and

printf("%p\n", myString[x]);

do not make a sense because you are trying to use a value of a character as a pointer value.

As for the other warning then just cast pointers to the type void *. For example

printf("\nThe pointer variable's value is %p\n", ( void * )myString);
printf("\nThe pointer variable points to %s\n", myString);
printf("\nThe memory location for each character are: \n");


for (x = 0;x < 7;x  ){
 printf("%p\n", ( void * )( myString   x ));
}
  • Related