在c中证明指针地址
proving pointers address in c
试图理解指针。
下面,我只是想证明给自己看
str 的地址 (&str) = str2 指向的地址(str2)
str2 的实际地址内存有所不同(&str2)
但是,当我在下面编译时,出现段错误 "morePointers.c:6:56: warning: format specifies type 'int' but the argument has type 'char *' [-Wformat]"
如何在代码中证明这一点时更正这一点?
int main(int argc, char** argv) {
char str = "goodbye";
char *str2 = str;
printf("%d %d %s %d %d\n", &str2, str2, str2, str, &str);
}
正确的代码是:
#include <stdio.h>
int main(int argc, char** argv) {
char *str = "goodbye"; // a '*' was missing in your code
char* str2 = str;
// you need to use %p for pointer values
// and for each argument for a %p you need to cast to (void*)
printf("%p %p %s %p %p\n", (void*)&str2, (void*)str2, str2, (void*)str, (void*)&str);
}
此代码在 32 位系统上的典型可能输出
00FFF710 00996B30 goodbye 00996B30 00FFF71C
此代码在 64 位系统上的典型可能输出
0x7ffc8f58ada0 0x555ba93f6004 goodbye 0x555ba93f6004 0x7ffc8f58ad98
在此声明中
char str = "goodbye";
您正在尝试使用在初始化表达式中具有类型 char *
.
的字符串文字来初始化类型 char
的对象
你必须写
char *str = "goodbye";
而不是这个声明
char *str2 = str;
你的意思好像是
char **str2 = &str;
也就是说你正在创建一个指向指针str
的指针。
因此取消引用指针 str2
您将获得存储在指针 str
中的值,即字符串文字的第一个字符的地址 "goodbye"
.
这是一个演示程序
#include <stdio.h>
int main(void)
{
char *str = "goodbye";
char **str2 = &str;
printf( "str = %p is the same as *str2 = %p\n", ( void * )str, ( void * )*str2 );
printf( "str2 = %p contains the address of str (&str) = %p\n", ( void *)str2, ( void *)&str );
printf( "The string literal pointed to by str = %s\n"
"is the same as pointed to by *str2 = %s\n",
str, *str2 );
return 0;
}
程序输出可能看起来像
str = 0x561655562008 is the same as *str2 = 0x561655562008
str2 = 0x7ffdb7fc57a8 contains the address of str (&str) = 0x7ffdb7fc57a8
The string literal pointed to by str = goodbye
is the same as pointed to by *str2 = goodbye
请注意,将转换说明符 %d
与指针一起使用会导致未定义的行为。
试图理解指针。
下面,我只是想证明给自己看
str 的地址 (&str) = str2 指向的地址(str2) str2 的实际地址内存有所不同(&str2)
但是,当我在下面编译时,出现段错误 "morePointers.c:6:56: warning: format specifies type 'int' but the argument has type 'char *' [-Wformat]"
如何在代码中证明这一点时更正这一点?
int main(int argc, char** argv) {
char str = "goodbye";
char *str2 = str;
printf("%d %d %s %d %d\n", &str2, str2, str2, str, &str);
}
正确的代码是:
#include <stdio.h>
int main(int argc, char** argv) {
char *str = "goodbye"; // a '*' was missing in your code
char* str2 = str;
// you need to use %p for pointer values
// and for each argument for a %p you need to cast to (void*)
printf("%p %p %s %p %p\n", (void*)&str2, (void*)str2, str2, (void*)str, (void*)&str);
}
此代码在 32 位系统上的典型可能输出
00FFF710 00996B30 goodbye 00996B30 00FFF71C
此代码在 64 位系统上的典型可能输出
0x7ffc8f58ada0 0x555ba93f6004 goodbye 0x555ba93f6004 0x7ffc8f58ad98
在此声明中
char str = "goodbye";
您正在尝试使用在初始化表达式中具有类型 char *
.
char
的对象
你必须写
char *str = "goodbye";
而不是这个声明
char *str2 = str;
你的意思好像是
char **str2 = &str;
也就是说你正在创建一个指向指针str
的指针。
因此取消引用指针 str2
您将获得存储在指针 str
中的值,即字符串文字的第一个字符的地址 "goodbye"
.
这是一个演示程序
#include <stdio.h>
int main(void)
{
char *str = "goodbye";
char **str2 = &str;
printf( "str = %p is the same as *str2 = %p\n", ( void * )str, ( void * )*str2 );
printf( "str2 = %p contains the address of str (&str) = %p\n", ( void *)str2, ( void *)&str );
printf( "The string literal pointed to by str = %s\n"
"is the same as pointed to by *str2 = %s\n",
str, *str2 );
return 0;
}
程序输出可能看起来像
str = 0x561655562008 is the same as *str2 = 0x561655562008
str2 = 0x7ffdb7fc57a8 contains the address of str (&str) = 0x7ffdb7fc57a8
The string literal pointed to by str = goodbye
is the same as pointed to by *str2 = goodbye
请注意,将转换说明符 %d
与指针一起使用会导致未定义的行为。