0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pointers and Arrays 1: simple pointer and array, The C Puzzle Book, p.53

Last updated at Posted at 2025-06-11
pa1.c
#include "defs.h"

int a[]={0,1,2,3,4};

void main(){
	int i, *p;

	for( i=0; i<=4; i++ ) PR(d,a[i]);			//Pointers and Arrays 1.1
	NL;
	for( p= &a[0]; p<=&a[4]; p++ )
		PR(d,*p);								//Pointers and Arrays 1.2
	NL; NL;

	for( p= &a[0],i=1; i<=5; i++ )
		PR(d,p[i]);								//Pointers and Arrays 1.3
	NL;
	for( p=a,i=0; p+i<=a+4; p++,i++ )
		PR(d,*(p+i));							//Pointers and Arrays 1.4
	NL; NL;

	for( p=a+4; p>=a; p-- ) PR(d,*p);			//Pointers and Arrays 1.5
	NL;
	for( p=a+4,i=0; i<=4; i++ ) PR(d,p[-i]);	//Pointers and Arrays 1.6
	NL;
	for( p=a+4; p>=a; p-- ) PR(d,a[p-a]);		//Pointers and Arrays 1.7
	NL;
}
bash
$ gcc pa1.c
$ ./a.out
a[i] = 0        a[i] = 1        a[i] = 2        a[i] = 3        a[i] = 4
*p = 0  *p = 1  *p = 2  *p = 3  *p = 4

p[i] = 1        p[i] = 2        p[i] = 3        p[i] = 4        p[i] = 0
*(p+i) = 0      *(p+i) = 2      *(p+i) = 4

*p = 4  *p = 3  *p = 2  *p = 1  *p = 0
p[-i] = 4       p[-i] = 3       p[-i] = 2       p[-i] = 1       p[-i] = 0
a[p-a] = 4      a[p-a] = 3      a[p-a] = 2      a[p-a] = 1      a[p-a] = 0

本に結果が?になっているところがある。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?