1

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)
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
SimTim124
  • 51
  • 5
  • 2
    `*myString` and `myString[x]` are of type `char`, but you print them with `%p` which is for pointers. You probably want to replace them with `myString` and `&myString[x]` (or to be even stricter - `(void*)myString` and `(void*)&myString[x]`) – Eugene Sh. Oct 21 '21 at 17:17

1 Answers1

3

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 ));
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335