1. The arrangement of the memory array affirmed
The variable size of the array can not be used;
#include <stdio.h>
int main()
{
int a=3;
int b=2;
int array[3];
array[0]=1;
array[1]=10;
array[2]=100;
int *p=&a;
int i;
for(i=0;i<6;i++)
{
printf("*p=%d\n",*p);
p++;
}
printf("--------------------------------------------------\n");
p=&a;
for(i=0;i<6;i++)
{
printf("p[%d]=%d\n",i,p[i]);
}
return 0;
}
operation result:
linux@ubuntu:~/workspace/array$ ./main.out
*p=3
*p=-1075484268
*p=2
*p=2
*p=134513888
*p=0
--------------------------------------------------
p[0]=3
p[1]=-1075484272
p[2]=2
p[3]=2
p[4]=134513888
p[5]=0
The results can be seen as two addressing mode.
(gdb) x/3d 0xbffff348
0xbffff348: 0 2 134513920
Achieved begins with the following sequential read address number 3, in decimal (D);
2 pointer arithmetic
int *p=&a;
p++;
After performing execution p ++ pp (gdb view the contents of the pointer p), find the address plus 4;
this is because p is a pointer to the int type, int four bytes each.
p+=3 p[3]
Points to an address, but the latter is not shifted p. ''
Array name itself is a pointer constant, can not be changed.
3. Character string arrays and pointers
linux@ubuntu:~/workspace/cstring$ cat main.c
#include<stdio.h>
int main()
{
char str[]="hello";//字符数组以\0结束;
char *str2="world";
char str3[10];
printf("input the value \n");
scanf("%s",str3);
printf("str is %s\n",str);
printf("str2 is %s\n",str2);
printf("str3 is %s\n",str3);
return 0;
}
1:
scanf("%s",srtr3);
str3 an array name, scanf no need to add the address character & taken;
2:
scanf("%s",srtr3)
The str3 into str, str can also write to the data;
but if str3 into str2, an error occurs, the following reasons;
3:
(gdb) p str
$1 = "hello"
(gdb) p &str
$3 = (char (*)[6]) 0xbffff346
(gdb) x/6c 0xbffff346
0xbffff346: 104 'h' 101 'e' 108 'l' 108 'l' 111 'o' 0 '\000' //字符前面的数字是ASCII码
(gdb) p str2
$2 = 0x8048630 "world"
(gdb) p &str2
$4 = (char **) 0xbffff338
0x8048630 in blocks, the code block is not possible freely modified, so scanf ( "% s", srtr2) to be wrong;
Not a statement:
char *str2;
*str2="abc";
or:
char *str2;
*str2='a';
. 4:
char STR [] and char str3 [10] in the stack memory (variable), it can be modified;
char * str2 in a code (a constant) can not be modified;
5. The in-depth understanding of the array of characters
End of the string \ 0 will not be displayed;
for loop-by-character output when not constrained \ 0;