Why is the data type needed in pointer declarations?

前端 未结 8 658
萌比男神i
萌比男神i 2020-12-04 22:07

As far as I know about data types in C/C++, while declaring a variable, we need to declare its data type, which tells the compiler to reserve the number of bytes in the memo

相关标签:
8条回答
  • 2020-12-04 22:25

    Assume that this code compiles without error (as you would like):

    int a;
    int b = 42;
    void * d = &b;
    
    a = *d;
    

    What should be the value of a?

    Now with this one:

    int a;
    float b = 42.0;
    void * d = &b;
    
    a = *d;
    

    What do you expect in a?

    Actually the type specifies how should the pointed area be interpreted. You should specify int * in the first example and float * in the second one, instead of void *.

    0 讨论(0)
  • 2020-12-04 22:26

    First of all the size and representation of the pointers themselves aren't always the same for different types. It's just something that happens on many implementations.

    Second, when using pointers you don't care about the size of the pointers themselves. You need the size of the pointed type.

    For example, try this:

    int var[5];
    char *c = (char *)var;
    int  *x = var;
    
    printf("%p\n%p\n", p + 1, x + 1);
    

    You'll see pointer arithmetic strongly depends on the size of the pointed type.

    0 讨论(0)
  • 2020-12-04 22:27

    Data type of a pointer is needed in two situations:

    1. Deferencing the pointer
    2. Pointer arithmetic

    How it is used in dereferencing the pointer?
    Consider the following example:

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

    Now because k is of type char so it will only read one byte. Now binary value of 256 is 0000000100000000 but because k 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 come to pointer arithmetic:
    Consider the following example:

        {
            short *ptr;
            short k=0;
            ptr=&k;
            k++;
            ptr++;// pointer arithmetic
        }
    

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

    For more info refer second chapter of this tutorial.

    0 讨论(0)
  • 2020-12-04 22:32

    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 adress it is pointing to while an int pointer should read 2 bytes.

    0 讨论(0)
  • 2020-12-04 22:32

    it is the concept of a strong typing used in c++. the size of the pointer may be the same but the size of the pointed type may differ. you can always cast a pointer of one type into a pointer of another type however.

    0 讨论(0)
  • Data types are needed simply for type checking.

    0 讨论(0)
提交回复
热议问题