ポインタへのポインタ
わすれるわぁ、ってことでボケ防止
[hirasawa@ubuntu1204-20 ~]$ cat point2.c
#include <stdio.h>
void main(void)
{
char *mnthp[3] = { /* mnthp[3] is(are) pointers of char */
"1", "123456789abcde", "12345"
};
char **p1, **p2, **p3; /* p1 is pointer to pointer of char */
int i, j;
p1 = p2 = p3 = mnthp; /* p1 = p2 = p3 = &mnthp[0] */
for(i=0;i<3;i++) {
printf("add of mnthp[%d] is %p\n",i,&mnthp[i]);
printf("contents of mnthp[%d] is %p\n",i,mnthp[i]);
}
/***** 例1 *****/
for (i = 0; i < 3; i++) {
printf("%s\n", *(p1 + i)); /* 相対的に文字列を出力 */
printf("%p-\n", (p1 + i)); /* 相対的に文字列を出力 */
}
/***** 例2 *****/
for (i = 0; i < 3; i++) { /* 「ポインタのポインタ」の値そのものを */
printf("%s\n", *p2); /* 更新して絶対的に文字列を出力 */
printf("%p--\n", p2); /* 更新して絶対的に文字列を出力 */
++p2;
}
/***** 例3 *****/
for (i = 0; i < 3; i++) {
j = 0;
while(*(*p3 + j) != '\0') { /* 「ポインタのポインタ」を使って、*/
printf("%c-", *(*p3 + j)); /* 1文字ずつ出力する */
printf("%p\n", (*p3 + j)); /* 1文字ずつ出力する */
j++;
}
printf("\n");
++p3;
}
}
[hirasawa@ubuntu1204-20 ~]$ ./point2
add of mnthp[0] is 0x7fff8a522e40
contents of mnthp[0] is 0x40083c
add of mnthp[1] is 0x7fff8a522e48
contents of mnthp[1] is 0x40083e
add of mnthp[2] is 0x7fff8a522e50
contents of mnthp[2] is 0x40084d
1
0x7fff8a522e40-
123456789abcde
0x7fff8a522e48-
12345
0x7fff8a522e50-
1
0x7fff8a522e40--
123456789abcde
0x7fff8a522e48--
12345
0x7fff8a522e50--
1-0x40083c
1-0x40083e
2-0x40083f
3-0x400840
4-0x400841
5-0x400842
6-0x400843
7-0x400844
8-0x400845
9-0x400846
a-0x400847
b-0x400848
c-0x400849
d-0x40084a
e-0x40084b
1-0x40084d
2-0x40084e
3-0x40084f
4-0x400850
5-0x400851
[hirasawa@ubuntu1204-20 ~]$ もういっちょ!って適当にでっちあげてみたけど、全然意味がなかったw
ま いいか
[hirasawa@ubuntu1204-20 ~]$ cat point3.c
#include <stdio.h>
void main(void)
{
int * intp;
int ** intpp;
int a[10];
int i;
for(i=0;i<10;i++)
{
a[i]=i;
printf("%d",a[i]);
}
printf("\n");
intp = a;
for(i=0;i<10;i++)
{
printf("%d",*intp);
intp++;
}
printf("\n");
intp = a;
//printf("%p\n",intp);
intpp = &intp;
for(i=0;i<10;i++)
{
printf("%d",**intpp);
intp++;
}
printf("\n");
}
[hirasawa@ubuntu1204-20 ~]$
[hirasawa@ubuntu1204-20 ~]$ cat point3.c
#include <stdio.h>
void main(void)
{
int * intp;
int ** intpp;
int a[10];
int i;
for(i=0;i<10;i++)
{
a[i]=i;
printf("%d",a[i]);
}
printf("\n");
intp = a;
for(i=0;i<10;i++)
{
printf("%d",*intp);
intp++;
}
printf("\n");
intp = a;
//printf("%p\n",intp);
intpp = &intp;
for(i=0;i<10;i++)
{
printf("%d",**intpp);
intp++;
}
printf("\n");
}
[hirasawa@ubuntu1204-20 ~]$ ./point3
0123456789
0123456789
0123456789
[hirasawa@ubuntu1204-20 ~]$ もういっちょ でっちあげてみたけど、
やはり意味がない
ただのポインターになっちまったい
んー、結局stringぐらいしかつかわないってこと?
よくわかんなくなってきたゾ orz
[hirasawa@ubuntu1204-20 ~]$ cat point3.c
#include <stdio.h>
void main(void)
{
int x=3;
int y=3;
int inta[x][y];
int *intp;
int i,j;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
inta[i][j]=i+j;
printf("%d",inta[i][j]);
}
}
printf("\n");
intp = inta;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
printf("%d",*intp);
intp++;
}
}
printf("\n");
}
[hirasawa@ubuntu1204-20 ~]$ ./point3
012123234
012123234
[hirasawa@ubuntu1204-20 ~]$