10

I refereed the following link,

Link1 Link 2

In the above link1 it was mentioned in answer that "Pointers are of pointer type".

I just need to know is pointer is a data type or not.

No one has answered that question in single word. It is datatype or not?

This is the second link which i refered says that

Pointers are simply a variable that hold an address so one could argue that a pointer is a data type, but it is not defined as a data type (per "The C Programming Language". Kernighan & Ritchie).

Community
  • 1
  • 1
VINOTH ENERGETIC
  • 1,775
  • 4
  • 23
  • 38
  • 6
    _"I just need to know is pointer is a data type or not."_ - Yes, a pointer is a data type. – Captain Obvlious Nov 17 '14 at 15:52
  • 3
    It depends on what you mean by "data type". It's certainly a *type*, and indeed an *object type*, as opposed to a *function* or *reference type*, but neither language formally defines the term "data type". (Informally, it's a type that can hold some data (an address), so yes, it is a data type.) – Mike Seymour Nov 17 '14 at 15:55
  • I'm not sure that the phrase "data type" has any different meaning from "type" – Bill Lynch Nov 17 '14 at 16:05
  • 2
    @BillLynch: Is a function type a "data type"? I'd say no. – Keith Thompson Nov 17 '14 at 16:28
  • "Pointers are of pointer type" means that they don't have to be stored as integers (signed or unsigned); they don't even have to be scalars. On some architectures they may be represented as a pair of values (page #:offset). – John Bode Nov 17 '14 at 16:30
  • Your title asks "What is the data type of pointer variables?" The body of your question asks whether a pointer is a data type or not. Those are two different questions (and neither of them is particularly clear). – Keith Thompson Nov 17 '14 at 16:30
  • @JohnBode: In fact pointer types are *scalar types* by definition. N1570 6.2.5p21: "Arithmetic types and pointer types are collectively called *scalar types*. Array and structure types are collectively called *aggregate types*." But that says nothing about how pointers are represented. – Keith Thompson Nov 17 '14 at 19:30
  • @KeithThompson: you're right; I knew that, really, but had a brain fart. As far as the language is concerned, pointers are scalars (as in, not arrays, not structs, not unions). The underlying representation need not be scalar, but that's an implementation detail. – John Bode Nov 17 '14 at 19:40
  • @JohnBode: Similarly, in a very "real" sense floating-point objects are aggregates, consisting of a sign, a significand, and an exponent, but the language defines them as scalars. (And in Perl, numbers, references *and strings* are scalars.) – Keith Thompson Nov 17 '14 at 19:42

10 Answers10

23

Yes, a pointer is a data type. The purest form of which (mainly talking about C here) is void *. A void * can be used to pass a memory address around (which is what a pointer is), but it can't be dereferenced. Dereferencing a pointer is what you do to get at the data contained at the memory location the pointer is pointing at, which implies that you know what type of data you're reading from the memory. The type determines how much memory will be read, and because a void is "nothing". A void * can be set to point at any block of memory, that can contain any type, so you can cast a void * to any other pointer type (int *, for example), and dereference that, instead.

Each type we have is used to store a specific piece of data (value), we use a char to store a single character, an int to store an integer, double to store double precision decimals and so on. None of these types are used to store locations in memory, apart from pointers. So just like the other types, a pointer is used to store a specific piece of data. And the mother of all pointers is void *.

Sadly, this void * is rather restricted: you can't dereference it, you can't use it for pointer arithmetic (not according to the standard anyway). So C provides you with a series of derived pointer types, that make life easier: char *, int *, double * and so on.
What they are, really, is short-hand for: (char *) void * my_ptr;

Some more attempts at making my point as clearly as possible:

Pointers have their own size, irrespective of the type they're said to point at:

char a_character = 'a';  //type: a char
char *a_char_ptr = &a_character; //memory address, in this case, the one holding a_charachter

The distinction is probably best seen by looking at the sizes of both these vars:

printf("%zu <> %zu\n", sizeof a_character, sizeof a_char_ptr);

The code above will give you something like "1 <> 8" or "1 <> 4", depending on what system you're on. Numbers represent the size, in bytes.

Pointers also have their own printf format specifier: %p:

printf("%c is the value stored at %p\n", *a_char_ptr, (void *) a_char_ptr);

To print the actual memory address (the actual value of a pointer), you are required to cast the pointer to the generic void * type. A void pointer is sort of the generic pointer; it's the pointer that makes no assumptions as to the data it is pointing at. This is what malloc, calloc and realloc return, a generic pointer, that can be set to point at any other type. So what is a char *? It's a generic pointer type, set to point at blocks of memory of 1 byte in size (sizeof(char)). In a sense, a typed pointer, then, is a derived type, but think of it like this: char * is short for (char *) void *my_ptr;

But really, what is a type? The gist of it is that a type is way to determine how data in memory is supposed to be interpreted. A variable of the type char represents a character. A variable of the type int represents an integer. Same applies to pointers: char *x is not of the type char, it's of the type char * (pointer to char). This means that char *x itself is a location in memory we can use to read one or more char values.

I could rant on for a while but TL;TR:

Yes, a pointer is a data type (void * in its purest form). The pure form is quite unusable (because you can't dereference it). Instead of having to cast the pointer every time you decide to use it, C offers the convenience of derived pointer types (like char *, int * and so on). But really, they're pointers, and therefore a data-type in their own right.

q-l-p
  • 4,304
  • 3
  • 16
  • 36
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • Nit - you can't dereference a `void *` because the resulting expression has type `void`, which has no value; that's why you have to cast the pointer to a different pointer type before the dereference. – John Bode Nov 17 '14 at 16:32
  • @JohnBode: didn't I mention that? Nips... I thought I droned on and on about not being able to dereference a void pointer. Made the edit – Elias Van Ootegem Nov 17 '14 at 16:33
  • @EliasVanOotegem very beautifully answered ! It can sort out not only the doubt which is asked but many more like what happens when `char *` stores the address of integer. – Prince Vijay Pratap Aug 14 '15 at 21:34
  • >"The code above will give you something like "1 <> 8" or "1 <> 4", depending on what system you're on." - You can compile on x64 bit "system" in 32-bit mode. Then it will not depend on the "system". Also you can very well assign an integer value to a pointer type `void* ptr = (void* )32` and it is fine as long as you don't dereference it of course. The range has to be taken into consideration. – Edenia Apr 18 '18 at 18:01
7

You've asked two different questions.

Your title asks "What is the data type of pointer variables?". The answer is simple: a pointer variable is of some pointer type. For example, given:

int *ptr;

ptr is a pointer object, and its type is int*, which is a pointer type.

The body of your question asks whether "a pointer is a data type or not". By any reasonable definition of the phrase "data type", pointer types are data types.

The C standard never defines the phrase "data type", but it does use it (informally) in several places. It happens that none of the uses of the phrase "data type" in the standard refer to pointer types, but that doesn't tell us anything.

The standard says that all types are either function types or object types. Object types are further divided into a number of categories: integer types, array types, structure types, union types, pointer types, etc. A pointer type can be a pointer to an object type or a pointer to a function type. (It can be a pointer to an incomplete object type; as of the 2011 standard, incomplete types are classified as object types.)

Another ambiguity in your question is your use of the word "pointer". The word "pointer" by itself commonly refers to an object of pointer type, but it can also refer to a value of pointer type (for example, the standard says that malloc returns a pointer). It's better to use "pointer" as an adjective rather than as a noun, so you can have:

  • a pointer type;
  • a pointer object (an object of pointer type);
  • a pointer expression (an expression that yields a result of pointer type); or
  • a pointer value (the value, of pointer type, yielded by a pointer expression).

A pointer type is an object type. A pointer object is an object; an object is defined by the standard as a "region of data storage in the execution environment, the contents of which can represent values". So a pointer object is a region of data storage.

Following your Link 2, some random person on the Internet wrote that "Pointers are simply a variable that hold an address so one could argue that a pointer is a data type, but it is not defined as a data type (per "The C Programming Language". Kernighan & Ritchie)". I don't know whether K&R defines the term "data type"; since this person didn't provide a specific citation, it's difficult to tell without searching the book. But it's the standard, not K&R, that defines the language.

I'm curious: why would you think that a pointer type wouldn't be considered a data type?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • K&R (second edition) Section A8.8: Type names _"specifier-qualifier-list abstract-declarator(opt)"_ is followed by a series of examples including `int`, `int *` and `int*[3]`, listing them as different types. data-type is *not* defined anywhere in the book, AFAIK, but types and type-names are – Elias Van Ootegem Nov 18 '14 at 18:25
  • @iOSdeveloper: What is a "primitive data type"? The C standard doesn't define or use that phrase. – Keith Thompson Oct 06 '15 at 16:03
4

For example in the C Standard there is no formal definition of the term data type. There are object types and function types. At the same time pointers are derived types constructed from object and function types.

Thus in general case pointers are data types that is they are data types that are constructed from object and function types.

Also there is definition of term object in the C Standard

3.15
1 object
region of data storage in the execution environment, the contents of which can represent
values

So there is some contradiction in the Standard. On the one hand pointers are objects because they occupy memory and the memory represents their values. So we may say that pointers are object types. On the other hand pointers are considered as derived types from object types.

In my opinion it would be better if there would be explicitly written in the Standard that pointers are derived object types or derived function types.

In any case you may bravely say that pointers are data types!:)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I get what you're saying on the apparent contradiction in the standard, but the data-type _"pointer"_ would be `void *`, which contains data (a memory address), the derived types are the pointers you can actually use: `char *`, `int *`, ... so a pointer is a data-type, but you mainly use the derived types – Elias Van Ootegem Nov 17 '14 at 16:17
  • Pointer types may be derived *from* function types, but all pointer types are object types. – Keith Thompson Nov 17 '14 at 16:27
  • @Keith Thompson I agree but it would be better that this were written more clear in the Standard. – Vlad from Moscow Nov 17 '14 at 16:29
  • It says "Types are partitioned into *object types* ... and *function types* ...." Pointer types clearly are not function types, therefore they're object types. Why does it need to be clearer than that? – Keith Thompson Nov 17 '14 at 16:54
  • @Keith Thompson There is no written that pointers are object types. There is written that pointers are derived types from object and function types. – Vlad from Moscow Nov 17 '14 at 16:58
  • N1570 6.2.5: p1: "Types are partitioned into *object types* (**types that describe objects**) and *function types* (types that describe functions). p20: "A pointer type **describes an object** whose value provides a reference to an entity of the referenced type." – Keith Thompson Nov 17 '14 at 17:00
  • @Keith Thompson It does not make clear that pointers are object types. I wrote about this in my post. – Vlad from Moscow Nov 17 '14 at 17:02
  • I believe it makes it perfectly clear. Object types are by definition "types that describe objects". A pointer type "describes an object". Pointer types are not function types; that would contradict the explicit normative wording of the standard. – Keith Thompson Nov 17 '14 at 17:05
  • @Keith Thompson The only that is clear that pointers are derived types from object types and function types. – Vlad from Moscow Nov 17 '14 at 17:07
  • I disagree, but I'm not interested in continuing this argument. – Keith Thompson Nov 17 '14 at 17:09
2

Yes, pointer is a data type and a pointer variable store that pointer data type.

haccks
  • 104,019
  • 25
  • 176
  • 264
1

Pointer is a data-type. So we can create pointer variables which can hold the address of memory location.

Gopi
  • 19,784
  • 4
  • 24
  • 36
  • -1, only `void *` is valid for printing by `%p`, and the type of data being pointed at is part of the type. A `double *` is not the same datatype as an `int *`. – unwind Nov 17 '14 at 16:03
  • @unwind But basically %p is for printing pointer address right? let the pointer point to any memory location – Gopi Nov 17 '14 at 16:13
  • @Gopi: cf my answer, to print a pointer's _actual_ value, you **have** to cast it to `void *`. This is because `printf` & are variadic functions, hence there is no implicit conversion possible. – Elias Van Ootegem Nov 17 '14 at 16:20
  • @Gopi: The phrase "pointer address" is redundant. `%p` is for printing the *value* of a pointer. (An address is a pointer value.) – Keith Thompson Nov 17 '14 at 16:26
1

Pointer types are data types; they store pointer values.

There is no one single pointer type; a pointer to int is a different type from a pointer to char, which is a different type from a pointer to double, which is a different type from a pointer to a 10-element array of int, which is a different type from a pointer to an 11-element array of int, etc.

Different pointer types may have different sizes and representations; the only pointer types that are guaranteed to have the same sizes and representations are void * and char *.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • is it true? is different pointer variable has different size. I studied that all pointer are of same size – VINOTH ENERGETIC Nov 17 '14 at 16:10
  • 1
    @VinothKumar A pointer to a member function is not necessarily the same size as other pointer types. – Captain Obvlious Nov 17 '14 at 16:15
  • 1
    @VinothKumar: The C language definition allows different pointer types to have different sizes and representations. See the [online C2011 draft standard](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf), 6.2.5/28. On commodity x86 systems they'll probably all be the same, but on some oddball architectures they may not be. – John Bode Nov 17 '14 at 16:18
  • @CaptainObvlious It is showing same size for integer pointer and function pointer as same in my system. Is this architecture dependent – VINOTH ENERGETIC Nov 17 '14 at 16:18
  • 1
    @VinothKumar: It is absolutely architecture dependent. Harvard architectures store code and data in physically different memory segments, which may have different bus sizes, so a data pointer may be wider or narrower than a function pointer. A word-addressed architecture may use wider types for `char *` than other types to allow for indexing into the word. – John Bode Nov 17 '14 at 16:20
  • Are you looking at a pointer to a function or a pointer to a _member function_, they are two different things. And yes, the size of a pointer to member function is dependent on the underlying architecture and can be affected by whether the function is virtual and if multiple inheritance is used. – Captain Obvlious Nov 17 '14 at 16:21
  • @CaptainObvlious: (Note that C doesn't have member functions.) Neither C nor C++ requires object pointer types to be the same size as function pointer types. Even different object pointer types can be of different sizes; for example, it's possible to have `sizeof (void*) > sizeof (int*)`. On most modern systems, all pointers are the same size, but that's not required. – Keith Thompson Nov 17 '14 at 16:57
1

Your question probably refers to the "data type of a pointer", in contrast to the data type of the pointed-to data, which is what one would understand in the first place.

Based on this assumption, then please look at type uintptr_t or void*.

To quote Drew Dorman's answer: "uintptr_t is an unsigned integer type that is capable of storing a pointer. Which typically means that it's the same size as a pointer"

Of course, its size is platform-dependant: 32 bit or 64 bit. So don't transport this variable across platforms of different size.

Please note, to assign it you have to cast from the 'specific' pointer type, to the 'generic' one:

int var = 1;
int* addrOfVar = &var; // pointer to variable
uintptr_t pVar = (uintptr_t)&var;
uintptr_t pVar2 = reinterpret_cast<uintptr_t>(&var); // alternative cast
caucus
  • 182
  • 1
  • 10
0

See a pointer variable stores the address of another variable. And when we access this pointer variable it points to the address of variable which eventually directs us to the data stored inside that variable. Now depending upon the type of data stored inside the original variable we can specify the data type of that pointer variable.

0

I just need to know is pointer is a data type or not.

All pointer types are data types, but plain "pointer" is not a data type. More precisely, "pointer" is a type modifier that you can apply to absolutely any type. So you can have pointer-to-char, and pointer-to-long, and pointer-to-double, not to mention pointer-to-pointer-to-char, etc.

There are three of these type modifiers in C: pointer-to, array-of, and function-returning. These can be combined in almost any combination. (But, to be sure, a few combinations are invalid. You can't have arrays of functions, or functions returning arrays.)

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
0

The thing is Pointer itself is a data type of it's own. It basically store a memory address.

Now it can be used to store a memory address of any variable too.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 04 '22 at 14:56