3

What actually happens while declaring a pointer of a specific type? Is there any use to specify a type to a pointer other than pointer Arithmetic or Indexing?

haccks
  • 104,019
  • 25
  • 176
  • 264
joness
  • 65
  • 1
  • 6
  • 4
    Without the type, how do you know to what the pointer points? – RedX Aug 08 '16 at 06:41
  • How would you write data to a memory location through pointer without knowing the type of the data that location is allocated for? – haccks Aug 08 '16 at 06:46
  • 1
    Related or Duplicate: [What are the differences between a pointer variable and a reference variable in C++?](http://stackoverflow.com/q/57483/514235) – iammilind Aug 08 '16 at 06:48
  • You can specify that a pointer points to an object of an unknown type: `void* ptr;` Try to use it throughout a program and tell us how far have you got. – n. m. could be an AI Aug 08 '16 at 06:54

6 Answers6

11

One of the reasons could be that when you dereference the pointer we should know how many bytes to read right? Dereferencing char pointer would imply taking one byte from memory while for int it could be 4 bytes.

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
  • That's all? I thought the answer is "because you have to" - `auto x = *ptr;`, `func(*ptr);`... – LogicStuff Aug 08 '16 at 07:03
  • @LogicStuff Sorry not sure I am following you. If you feel something lacks, please consider adding another answer. I also answered this question mainly from C point of view, since the question was also tagged like that. – Giorgi Moniava Aug 08 '16 at 07:08
  • Oh, Ok then. I was simply saying what the first comment under the question is saying. – LogicStuff Aug 08 '16 at 07:13
4

Type of a pointer is needed in following situations

  • Dereferencing the pointer
  • Pointer arithmetic

Here is the example of dereferencing the pointer.

{
    char *ptr; //pointer of type char
    short j=256;
    ptr=&j;    // Obviously You have to ignore the warnings
    printf("%d",*ptr)
}

Now because ptr is of type char so it will only read one byte. But the binary value of 256 is 0000000100000000 but because ptr is of type char so it will read only first byte hence the output will be 0.

Note: if we assign j=127 then output will be 127 because 127 will be hold by first byte.

Now, the example of pointer arithmetic:

{
    int *ptr;
    int var=0;
    ptr=&var;
    var++;
    ptr++;// pointer arithmetic
}

Are statements var++ and ptr++ are same thing? No, var++ means var=var+1 and ptr++ means ptr=ptr+4. Because the compiler "knows" this is a pointer and that it points to an int, it adds 4 to ptr instead of 1, so the pointer "points to" the next integer.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Shravan40
  • 8,922
  • 6
  • 28
  • 48
2
  • Size: How many bytes do you access? Reading/writing a char of 1 byte isn't the same as writing a long of ≥ 4 octets.
  • Alignment: Two types might be the same length but have different alignment. One data type might be put into a special register and memory moves from/to these register have special alignment requirements.
  • Representation: Even with same sizeof and _Alignof, the representation might differ. If you assign a *float_ptr = 4, the 4 gets converted to 4.0.
  • Type safety: Gross misconduct like void **ptr1, **ptr2; *ptr1+*ptr2; can be detected.
a3f
  • 8,517
  • 1
  • 41
  • 46
1

A pointer is a variable whose value is the address of another variable

Here are few examples for pointer types:

int    *ip;    /* pointer to an integer */

double *dp;    /* pointer to a double */

float  *fp;    /* pointer to a float */

char   *ch     /* pointer to a character */

The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address.

The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to

dear_tzvi
  • 745
  • 6
  • 19
  • This is all good for definition. I guess the question is more about usage of pointer, that is what you gain upon knowing the data type also? – Saurav Sahu Aug 08 '16 at 06:59
  • "The actual data type of the value of all pointers, ... is the same". That's not (necessarily) true. Word addressed machines can use one data type for the address of a word (which typically covers integer, float, and struct pointers), and a *different* data type for the address of a word + the offset within a word (which covers char pointers and void pointers). Segmented architectures often use a different type for "pointer to function", and finally there is all the fun and games of "pointer to member" / "pointer to member functions" - which are completely different beasts. – Martin Bonner supports Monica Aug 08 '16 at 07:19
0

The Data type is needed when dereferencing the pointer so it knows how much data it should read.

For example: dereferencing a char pointer should read the next byte from the address it is pointing to, while an int pointer should read two bytes.

Refer this for more: Why is it necessary to declare the types that pointers point to?

K.H.A.J.A.S
  • 514
  • 4
  • 19
0

Pointer also point to valid object of a class. In absence of type one cannot get information to access the members of the class.

class CPerson
{
    public:
        std::string GetName();
};

CPerson oPerson;
CPerson *pObj_1 = &oPerson;
pObj_1->GetName();

void *pObj_2 = &oPerson;
pObj_2-> ??  // what all I can access now ?
sameerkn
  • 2,209
  • 1
  • 12
  • 13