【C】Pointer (15) C Standard ③ Pointer to T

※ 日本語ページはこちら

Start from following C language standard.

– 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 third sentence above, which is below:

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

Let ‘s use same codes which I used in the previous article.

Re:programs which uses “pointer type which is explained as pointer to int type” and “pointer type which is explained as pointer to char” to assign address of object, increment them and show the contents of objects.

#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;
}

Line 7, pointer type of pa is derived from char, then sometime we will describe this as “pointer type of pa points to char type”.

Line 8, pointer type of pb is derived from int, then sometime we will describe this as “pointer type of pb points to int type”.

Now, you finally find the possible definition of “pointer”.

From this part, I describe my own understanding about pointer/ pointer related topics in C language.

If someone wrote very simple definition about pointer in specifications, I think pointer would be much easier for everyone.

Unfortunately, as we all see above, it is not well defined in specification. Possibly, some smart people who wrote books after the publish of specs preferred to define pointer in a simple sentence by ignoring details.

There are so many great books and resources for C language, but for pointers, I think lots of wrong info have been causing lots of confusions. In the end, there are so many cases that we cannot explain pointers in above definition.

Please imagine that you have a new programmer who has passion to learn C language.

She/he asks the question, “What is pointer?” based on her/his motivation and wrong assumptions that older programmers have clear understandings about pointer, you may would like to answer like below because you have to..

Google it, idiot.

or

Long story short, pointer is a variable which stores a address of variables, you see , here is the book which describes the sentence.

If you are asked by a new engineer who is new to C language, “What is pointer?”, please do not say,

Google it, idiot.

Because, it will be a moon shot to find the right resources which explain about pointer clearly/precisely.

The confusion around pointer is not based on lack of knowledge / smartness of new programmers, it simply based on there is too many wrong/incomplete information here.

I am trying to clarify about pointer in this blog, above.

If you are asked the question, please share below at least.

  • In may books and resources on web, pointer is explained as “Pointer is a variable which stores an address of a variable”. In some cases, you can use a pointer in that way, but there are other cases you use a pointer differently, so it is not easy to define one sentence.
  • As a proof that it is difficult to define in one sentence, we have a book which fully explain about a pointer.
  • It’s not my fault nor your fault for this confusion, you can understand how to use/treat pointer by experiencing.
  • If you need to define in one sentence for some reasons, and if you agree on my definition, you can say that, A stranger named gocha on web says, “A pointer is a mechanism which is used to access/mean a variety of things in programs written in C language.”

That’s all for this time.

!!! All copyrights in this article belong to the writer of this blog.