一般来说,当你创建一个像这样的字符数组。
char string[100]; //allocate contigious location for 100 characters at compile time
这里字符串将指向该连续位置的基地址。假设存储器地址从开始那么这将是象
--------------------------------------
|4000|4001|4002|4003|...........|4099|
--------------------------------------
可变串将指向。要将值存储在4000,您可以执行*(4000)。
在这里,你可以像
*string='a'; //4000 holds 'a'
*(string+1)='b'; //4001 holds 'b'
*(string+2)='c'; //4002 holds 'c'
注:阵列可以通过任何在C三种形式进行访问。
string[0] => 0[string] => *(string+0) => points to first element in string array
where
string[0] => *(4000+0(sizeof(char))) => *(4000)
0[string] => *((0*sizeof(char))+4000) => *(4000)
*string => *(4000)
在整数数组的情况下,假设int带有的存储器4字节
int count[100]; //allocate contigious location for 100 integers at compile time
这里计数将指向contigious位置的基地址。假设从4000的存储器地址开始,然后它会像
--------------------------------------
|4000|4004|4008|4012|...........|4396|
--------------------------------------
可变计数将指向。要将值存储在4000,您可以执行*(4000)。
在这里,你可以像
*count=0; //4000 holds 0
*(count+1)=1; //4004 holds 1
*(count+2)=2; //4008 holds 2
所以来你的代码,你的目标可以这样实现。
#include<stdio.h>
#include<stdlib.h>
int main()
{
char str[100];
*str='a';
*(str+1)='b';
*(str+2)='c';
printf("%s",str);
return 0;
}
Output: abc
您不能将字符串文字分配给数组。 – chris
我敢肯定,这个问题应该在C编程良好的书中得到解答。 – soon
数组名称是常量和'str',所以,你不能修改它,编译器应该给出错误 –