Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the data type of pointer variables?

Tags:

c++

c

pointers

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).

like image 653
VINOTH ENERGETIC Avatar asked Nov 17 '14 15:11

VINOTH ENERGETIC


2 Answers

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.

like image 98
Elias Van Ootegem Avatar answered Nov 10 '22 01:11

Elias Van Ootegem


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?

like image 20
Keith Thompson Avatar answered Nov 10 '22 00:11

Keith Thompson