一件事,一直困惑我,字符指针。 诘这四年我又缠绵成C之后。指针的初始值是什么?
举个例子所提到的情况。为什么呢char
指针的行为以这种方式?我们如何直接处理指针所指向的内容,或者它是否像char指针存储地址以外的东西!
#include <stdio.h>
#include <stdlib.h>
int main()
{
char* charPtr="I cant understand why";
int* intPtr=60;
printf("%d\n", intPtr); //displays 60
printf("%p\n", intPtr); // displays the hex value of 60
printf("%s\n", charPtr); // displays the wh0le string
printf("%p\n", charPtr); // displays the start address of the string
return 0;
}
接下来int
指针,它怎么能接受值60和它从哪里获得存储在哪里?
撇开字符指针和malloc的,我以为指针的基本想法是得到一个地址指向!
为什么这些案件
*intptr = 60 ; // should be setting the pointee's value to 60
intptr = 60 ; // sets the address
掷编译错误而
int* intPtr=60;
偷偷在没有得到一个地址(或者是作为地址60,如果是这样,为什么这不能接受不前者的情况下)的指出!
我想我在这里的东西,但嘿!你猜怎么了 ?他们告诉我在SO搜索!
编辑:给指向的地址的字符指针为int的指针也会引发中没有错误!
int8_t* intPtr= (int8_t*)0x80485c8 ; // works without casting too ! I guess addresses are acceptable.
解引用它会给等同于字符串的第一个I
值。是一个很好的做法或有任何其他的解释存在于这个离开了thier字节位大小分配,如int可以支持一个char所以.. ?
正如hmjd指出'初始化语法'是问题!编写我自己的代码没有问题,但修改某人的代码时出现问题!
真的很难弄清楚你究竟在问什么。指针是一个变量,其值通常是另一个变量的地址。 –
*我想我错过了这里的东西*实际上,好的C编程书籍/教程。你问了很多常见问题。 –
@DavidSchwartz是的!这也是我的基本想法:)但是这里发生了什么? – Borrito