大家好,我目前自学了三个月的C语言入门,想问一个比较基础的问题,我已经尝试搜索是否有同样出现过的问题,但并没有找到我能理解的答案。我在学习C语言的字符数组时,想以十进制形式输出数组的首地址,我定义了一个 long 型变量来存储这个地址,但是在运行后得到的是如下的提示信息,我看不懂这个是什么意思,想请问有没有人能解答一下?谢谢!我使用的是 windows 64 系统下的 visual studio code 2022 来运行这段代码的,前面的代码我都已经理解了,但是唯独最后的两个输出指令反馈回来的信息我还不能理解。
#include <stdio.h>
int main(){
char str[30] = "I love_Stella, Always";
char *s1 = str; //字符串指针,指向 str[0],可以直接输出整个字符串
printf("\n s1 = %s\n", s1);
char *s2 = str+2; //字符串指针,指向 str[2],只能输出从第2个元素开始的整个字符串
printf(" s2 = %s\n", s1);
char c1 = str[4]; //取单个字符,str[4] = 'v'
printf(" c1 = %c\n", c1);
char c2 = *str; //取单个字符,*str = str[0] = 'I'
printf(" c2 = %c\n", c2);
char c3 = *(str+4); //取单个字符,*(str+4) = str[4] = 'v'
printf(" c3 = %c\n", c3);
char c4 = *str+2; //取单个字符后,对照ASCII的值+2,'I'+2 = 'k'
printf(" c4 = %c\n", c4);
char c5 = (str+1)[5]; //取单个字符,(str+1)[5] = str[6] = '_'
printf(" c5 = %c\n", c5);
int num1 = *str+2; //取单个字符后,对照ASCII的值+2,'I'=73,73+2 = 75
printf("num1 = %d\n", num1);
printf(" str = %#X\n", str);//以十六进制形式,输出str[0]的地址
long num2 = (long)str; //以十进制形式,输出str[0]的地址
printf("num2 = %ld\n", num2);
long num3 = (long)(str+2); //以十进制形式,输出str[2]的地址
printf("num3 = %ld\n", num3);
return 0;
}
PS C:\Users\Frede> cd "c:\Users\Frede\Desktop\" ; if ($?) { gcc Draft.c -o Draft } ; if ($?) { .\Draft }
Draft.c: In function 'main':
Draft.c:21:17: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
21 | long num2 = (long)str; //浠ュtr[0]? ^
Draft.c:23:17: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
23 | long num3 = (long)(str+2); //浠ュtr[2]? ^
s1 = I love_Stella, Always
s2 = I love_Stella, Always
c1 = v
c2 = I
c3 = v
c4 = K
c5 = _
num1 = 75
str = 0XF29FFA70
num2 = -224396688
num3 = -224396686
PS C:\Users\Frede\Desktop>
以上就是运行代码后得到的结果。
我已经确认过最后两行以十进制形式输出的地址和十六进制形式输出的地址相同,只是不清楚 “warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]” 这一行信息是在告诉我什么。