Pointer type declaration?

I just read that we need to specify the type of pointers, declaring them in C (or C ++) ie:

int *point ; 

As far as I know, pointers store the address of variables, and the address takes up the same amount of memory, whatever type it may be. So why do we need to declare its type?

+42
c ++ c pointers
Dec 05 '13 at 18:24
source share
10 answers

Type-security. If you do not know what p should point to, then there would be nothing to prevent category errors such as

 *p = "Nonsense"; int i = *p; 

Static type checking is a very powerful tool to prevent all such errors.

C and C ++ also support pointer arithmetic, which only works if the size of the target type is known.

the address occupies the same amount of memory, whatever the type

This is true for today's popular platforms. But there were platforms for which this was not so. For example, a pointer to a multibyte word may be less than a pointer by one byte, since it does not need to represent the byte offset within the word.

+69
Dec 05 '13 at 18:26
source share

Because:

  • addresses for different types should not be the same size . The standard explicitly indicates this ( standard C 2011 (online project), 6.2.5 / 28).
  • type-safety: this allows the compiler to detect when you provide an incompatible pointer to a function or job. This, in turn, prevents ugly situations where you mess up the order of the function arguments.
  • the compiler must know the type when the pointer is dereferenced.
  • To perform pointer arithmetic, the size of the object pointed to must be known, and therefore its type.

The last two points do not apply to void pointers, so they cannot be dereferenced, and pointer arithmetic cannot be performed on them. The standard states that the void pointer must be large enough to hold any pointer (except for pointers to objects that are a completely different story), and that the destination and void pointers can be done without casting (at least in C , in C ++ throws are always needed) .

+34
Dec 05 '13 at 18:29
source share

One reason is pointer arithmetic. You cannot do p+1 if you do not know the size of the element to which p belongs - this is the size of the type for which p is a pointer. If you try p+1 on void *p , you will probably get a bad answer (this is the same as with char * , but maybe you didn’t want it, it got on -pedantic as a warning and -pedantic-errors as an error).

Another reason is type safety. If a function receives int * as an argument, you cannot pass a pointer to a char (string). You will get a warning (error with -Werror / -pedantic-errors ). Consider this (dummy) code:

 void test(int *x) { } int main() { char *x = "xyz"; test(x); return 0; } 

Compilation (using gcc (GCC) 4.8.2 20131017 (Red Hat 4.8.2-1) ) gives:

 1.c: In function 'main': 1.c:8:2: warning: passing argument 1 of 'test' from incompatible pointer type [enabled by default] test(x); ^ 1.c:1:6: note: expected 'int *' but argument is of type 'char *' void test(int *x) ^ 
+13
Dec 05 '13 at 18:27
source share

So why do we need to declare its type?

You want to know the type of pointer so that you can perform a static type check .

We also need to know the type for pointer arithmetic to work, for example, when we index different sizes into an array (which is equivalent to pointer arithmetic), the pointer will be adjusted for the amount depending on the type. If we look at the draft standard C99 6.5.6 additive operators say (my attention):

To add, both operands must be of arithmetic type, or one operand must be a pointer to the type of an object [...]

Thus, the pointer must be an object type, which means that it is not incomplete or invalid.

You also said:

the address occupies the same amount of memory, whatever type it may be. So why do we need to declare its type?

This is not always true in C ++, the size of pointers to member functions can vary depending on the type of class, one of the good articles covering this is Pointers to member functions are very strange animals .

In addition, we can see that in the section of the standard section C99 of the project 6.2.5 Types 27, where it says:

[...] Pointers to other types should not have the same presentation or alignment requirements.

and in the draft standard section, C ++ 3.9.2 In composite types, paragraph 3 says:

[...] The representation of pointer type values ​​is implementation-defined. Pointers to cv-qualified and cv-unqualified versions (3.9.3) of types compatible with layouts must have the same requirements for the representation and alignment of values ​​(3.11). [...]

do not require pointers to have the same representation, except in special cases.

+11
Dec 05 '13 at 18:37
source share

You need to specify the type in accordance with the requirements of the standard. Moreover, to avoid problems when trying to perform pointer arithmetic, such as addition or subtraction.

+8
Dec 05 '13 at 18:28
source share

A pointer type appears during dereferencing and pointer arithmetic. for example

 int x=10; //Lets suppose the address of x=100 int *ptr=&x; //ptr has value 100 printf("The value of x is %d", *ptr); ptr++; // ptr has value 104(if int is 4bytes) 

In the above example, the pointer type is int, so the compiler will start looking for values ​​stored in the next 4 bytes (if int is 4 bytes), starting at memory address 100. Thus, the type of pointer tells the compilers how many bytes it should look for in dereferencing time. If there was no pointer type, how would the compiler know how many bytes should look like during dereferencing. And when we did ptr ++ , the pointer type indicates how much ptr should be incremented. Here ptr increases by 4.

 char c='a'; //Lets suppose the address of c = 200 char* ptr=&c; //ptr has value 200 ptr++; //ptr has value 201(char assumed to be 1 byte) 

A pointer type indicates that ptr is incremented by 1 byte.

+5
Dec 05 '13 at 18:35
source share

While processors often have different instructions for “downloading a byte from an address”, “downloading a 16-bit half-word from an address” and “loading a 32-bit word from an address”, as well as for “storing” operations, C uses one the same syntax for loading a byte from an address to load any other size value. Given the statement:

 int n = *p; 

the compiler can generate code that loads a byte, half-word, or a word from an address in p and stores it in n; if p is * float, it can generate a more complex code sequence to load the floating point value into c, trim it, convert to int, and save the converted value to n. Without knowing the type of p, the compiler cannot know which operation will be suitable.

Similarly, the p++ operator can increment an address in p by one, two, four, or some other number. The amount by which the address is incremented will be determined by the declared type p. If the compiler does not know the type p, it does not know how to configure the address.

You can declare a pointer without specifying the type of thing it points to. The type of such a pointer is void* . However, you must convert void* to a real pointer type before doing anything useful with it; The main utility of void* is that if the pointer is converted to void* , it can be passed as void* by code that does not know anything about the actual type of the pointer. If the pointer is ultimately provided to a code that knows its type, and this code returns the pointer back to that type, the result will be the same as the pointer that was converted to void* .

Code that needs to handle pointers to things that it knows nothing about can often use void* for such purposes, but code that knows about things that the pointer points to should usually point to pointers of the appropriate type.

+5
Dec 05 '13 at 21:41
source share

So that he can perform arithmetic and other operations. Consider these two examples:

 int* p; /* let the address of the memory location p pointing to be 1000*/ p++; printf("%u",p); /* prints 1004 since it is an integer pointer*/ char *p; /* let the address of the memory location p pointing to be 1000*/ p++; printf("%u",p); /* prints 1001 since it is an char pointer*/ 

Hope this helps you!

+3
Dec 05 '13 at 18:33
source share

In fact, we do not need (see below) to declare a type, but we must . The pointer stores information about the location of objects, and the type determines how much space the memory takes.

The size of an object stored in the specified memory is necessary in various cases: creating an array, copying, copying and, finally, creating an object using new .

However, you can still define a void pointer if you want to hide (for some reason) the type:

 void* dontKnowWhatTypeIsHere; 

The pointer to the void is considered universal. It can point to any object, and when we want to use it with a type, we just do reinterpret_cast .

0
Dec 05 '13 at 18:28
source share

A lot of the above statements, but apan is purely correct. Now your question is why do we determine the type of pointers? First definition of a pointer A pointer that may contain the address of another variable. The above definition is partial. An exact definition of a pointer is a variable that can contain the address of a variable, and if we de-link (select) it will return the value of the present at that address. If the pointer does not return a value for dereferencing, then this is not a pointer. You can try that even in the gcc compiler a simple variable may also contain the address of another variable, but when dereferenced, it will give you an error. Now the size Regardless of the data types, the size of the pointer is always equal to the size of the integer in this particular compiler. Thus, the size of the pointer in the gcc compiler is 4 bytes (integer size), and in turboc its size is 2 bytes (integer size). Now the question is why is equal to the size of an integer. What will be the address of any variable, which may be int, char, float, etc., The address is always an integer and where the integer is the storage in int. Therefore, the size of the pointer is equal to the size of int, since it also stores the address, which is always a pure integer. Then what is the difference between int and char of any other data type pointer. During extraction, your compiler will display the number of bytes according to your data types, other wise you will get an error or not an error, but for you there is an unpredictable result, but not for me. The same rule applies to increment and decrement a pointer, which it always increases and decreases according to the data types of the pointer. The size of the pointer does not depend on the data type and, therefore, on the fact that your list of links falls into the image, because if you try to declare a structure inside the same variable, then you will get a compile-time error, because your compiler does not have size structure before its full declaration, but the link pointer of the same structure is allowed why? The only answer is because the size of the pointer does not depend on the size of the data type. If you have any questions, please ask me. thanks asif aftab

-5
Dec 05 '13 at 20:04 on
source share



All Articles