Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a pointer considered a compound type in C++?

Tags:

c++

My understanding: Compound type is composed of primitive and other compound types. I understand that arrays, functions, classes, unions and enumerations are compound types. Why is a pointer a compound? What primitive types is it composed of?

like image 382
Therefore Avatar asked Feb 13 '18 00:02

Therefore


People also ask

What is a compound type in C?

The C struct is the most common compound data type and organizes data contiguously in memory. A union allows the same place in memory to be treated as different types. An enum defines a list of values but acts more like a macro (see #define ) than a compound data type.

Why is pointer a derived data type?

Structures, Unions, Arrays, and Pointers are the derived data types. It uses Character in the form of char, Unsigned char, Signed char, etc., for the characters present in the Fundamental Data Type. The Derived Data Types make use of pointers for storing the address of the available variables.

Is pointer primary data type?

Pointer is a data type and the purest form of it in C is void *. A void * can pass the memory address around which is what a pointer does but it cannot be dereferenced. Dereferencing means to get at the data contained at the memory location the pointer is pointing at.


1 Answers

I believe the rationale can be found in the standard under "Basic concepts" (section 3 in C++14, for example):

Finally, this clause presents the fundamental types of the language and lists the ways of constructing compound types from these.

Hence a compound type is really just a type created from another underlying type. A pointer certainly meets that definition since you construct pointer-to-T from T.

In terms of history, it's likely that this was carried over from C since it has the same concept, though it refers to the types and object/function and derived types. Derived types there seems to mirror the same things (at least those things that are common across both languages), including pointers.

like image 165
paxdiablo Avatar answered Oct 12 '22 00:10

paxdiablo