如何修复警告 "Format specifies type 'void *' but the argument has type 'char' warning"
How to fix warning "Format specifies type 'void *' but the argument has type 'char' warning"
我得到了我想要的输出,但不知道如何消除这些警告。感谢任何帮助。
警告:
Format 指定类型 'void *' 但参数的类型为 'char' [-Wformat]
printf("\n指针变量的值为%p\n", *myString);
"格式指定类型 'void *' 但参数的类型为 '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;
}
输出:
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)
对于初学者来说,这些电话
printf("\nThe pointer variable's value is %p\n", *myString);
和
printf("%p\n", myString[x]);
没有意义,因为您正试图将字符的值用作指针值。
至于其他警告,则只需将指针转换为类型 void *
。例如
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 ));
}
我得到了我想要的输出,但不知道如何消除这些警告。感谢任何帮助。
警告:
Format 指定类型 'void *' 但参数的类型为 'char' [-Wformat] printf("\n指针变量的值为%p\n", *myString);
"格式指定类型 'void *' 但参数的类型为 '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; }
输出:
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)
对于初学者来说,这些电话
printf("\nThe pointer variable's value is %p\n", *myString);
和
printf("%p\n", myString[x]);
没有意义,因为您正试图将字符的值用作指针值。
至于其他警告,则只需将指针转换为类型 void *
。例如
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 ));
}