【C】Pointer (14) C Standard ② About Pointer Type

※ 日本語ページはこちら

Following is about “pointer” in standard as I have shown previously.

– A pointer type may be derived from a function type, an object type, or an incomplete type, called the referenced type.

A pointer type describes an object whose value provides a reference to an entity of the referenced type.

A pointer type derived from the referenced type T is sometimes called ‘‘pointer to T’’.

The construction of a pointer type from a referenced type is called ‘‘pointer type derivation’’.

These methods of constructing derived types can be applied recursively.

ref: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf#page=48

In this article, I will explain the second sentence above, which is:

A pointer type describes an object whose value provides a reference to an entity of the referenced type.

I divide this sentence to 2 parts.

  1. A pointer type describes an object
  2. whose value provides a reference to an entity of the referenced type.

I think it is much easier to see codes to understand this.

pointer-type.c

#include <stdio.h>

int main (void){

        char a;
        int b;
        char * pa;
        int * pb;

        a = 10;
        b = 10;

        pa = &a;
        pb = &b;

        printf(" The value of a = %d\n", a);
        printf(" The value of b = %d\n", b);
        printf(" The contents of pa , char *, = %p\n", pa);
        printf(" The contents of pb , int  *, = %p\n", pb);

        pa++;
        pb++;

        printf(" The contents of pa++, char *, = %p\n", pa);
        printf(" The contents of pb++, int  *, = %p\n", pb);

        return 0;
}

Compile result and execution result of pointer-type.c

$ gcc pointer-type.c 
$ ./a.out 
 The value of a = 10
 The value of b = 10
 The contents of pa , char *, = 0x7ffc96f7893b
 The contents of pb , int  *, = 0x7ffc96f7893c
 The contents of pa++, char *, = 0x7ffc96f7893c
 The contents of pb++, int  *, = 0x7ffc96f78940

At first, I will explain “1. A pointer type describes an object”.

Please see last 4 sentences of compile, execution result of pointer-type.c . You can see that the contents of pa is incremented by 1, the contents of pb is incremented by 4 if you increment both pa and pb by 1.

As a result, pa is incremented by 1, pb is incremented by 4. This explains that pointer type describes/explains some info of an object.

“2. whose value provides a reference to an entity of the referenced type.”

The reason why pa is incremented by 1 and pb is incremented by 4 relates to this sentence.

You can confirm the value of these objects in line 5 and 6 of “Compile result and execution result of pointer-type.c”.

Reference type is a referenced types by pa or pb, more precisely “char” in char a and “int” in int b, and entity is a or b.

For details , I will explain if it is needed, but sizeof(a) is 1 , therefore contents of pa is incremented by 1, and sizeof(b) is 5 , therefore contents of pb is incremented by 4.

In short, based on size of referenced type, the portion of increments of pa or pb is different and it is obvious that pointer type is heavily influenced by the referenced type.

Next, let’s print the values of entities of referenced types by using contents of pa and pb.

A program which prints values of entities of reference types by using contents of pa and pb. pointer-type3.c

#include <stdio.h>

int main (void){

	char a;
	int b;
	char * pa;
	int * pb;

	a = 10;
	b = 10;	

	pa = &a;
	pb = &b;

	printf(" The value of a = %d\n", a);	
	printf(" The value of b = %d\n", b);	
	printf(" The sizeof a is  = %ld\n", sizeof(a));	
	printf(" The sizeof b is  = %ld\n", sizeof(b));	
	printf(" The contents of pa is  = %p\n", pa);	
	printf(" The contents of pb is  = %p\n", pb);	
	printf(" The value of a by referring contents of pa , char *, = %d\n", *pa);	
	printf(" The value of a by referring contents of pb , in   *, = %d\n", *pb);	

	pa++;
	pb++;
	
	printf(" The contents of pa++ is  = %p\n", pa);	
	printf(" The contents of pb++ is  = %p\n", pb);	
	
	return 0;
}

Compilation and execution results of type3.c

$ gcc pointer-type3.c
$ ./a.out 
 The value of a = 10
 The value of b = 10
 The sizeof a is  = 1
 The sizeof b is  = 4
 The contents of pa is  = 0x7ffd053c520b
 The contents of pb is  = 0x7ffd053c520c
 The value of a by referring contents of pa , char *, = 10
 The value of a by referring contents of pb , in   *, = 10
 The contents of pa++ is  = 0x7ffd053c520c
 The contents of pb++ is  = 0x7ffd053c5210

Execution results have below relationships.

Row 3: Printing value of a, 10
Row 9: Printing value of a, 10 , with dereference operator for pa
Row 4: Printing value of b, 10
Row 10: Printing value of b, 10 , with dereference operator for pb

That’s all for this time.

!!! All copyrights belong to the writer of this contents.