Why specify pointer type




















Pointer initialization is done with the following syntax. We can create a null pointer by assigning null value during the pointer declaration. This method is useful when you do not have any address assigned to the pointer. A null pointer always contains value 0. In C programming , a void pointer is also called as a generic pointer. It does not have any standard data type. A void pointer is created by using the keyword void. It can be used to store an address of any variable.

A pointer is said to be a wild pointer if it is not being initialized to anything. These types of C pointers are not efficient because they may point to some unknown memory location which may cause problems in our program and it may lead to crashing of the program. One should always be careful while working with wild pointers. Below table shows the arithmetic and basic operation that can be used when dealing with C pointers. Oh, Ok then. I was simply saying what the first comment under the question is saying.

Type of a pointer is needed in following situations Dereferencing the pointer Pointer arithmetic Here is the example of dereferencing the pointer. Lightness Races in Orbit k 70 70 gold badges silver badges bronze badges. Shravan40 Shravan40 7, 5 5 gold badges 25 25 silver badges 44 44 bronze badges. Size : How many bytes do you access? Alignment : Two types might be the same length but have different alignment.

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? That's not necessarily true. The Data type is needed when dereferencing the pointer so it knows how much data it should read. S 4 4 silver badges 19 19 bronze badges. The Overflow Blog. Most object-oriented C libraries use pointers to specific data structures to identify objects.

It is useful in such cases to reify the different pointer types as disjoint Scheme types. The define-wrapped-pointer-type macro simplifies this. Define helper procedures to wrap pointer objects into Scheme objects with a disjoint type.

Specifically, this macro defines:. Finally, print should name a user-defined procedure to print such objects. So how does anyone else know that the pointer points to an int if they are just told that ptr is a pointer and the address it contains is 0xABCDEF0? From the memory-allocation point-of-view, you're right. A pointer variable on a bit architecture occupies 8 bytes, no matter what type of pointer it is.

But the C compiler needs to know more about a variable than its size. An analogy: long and float typically both need 4 bytes so why have different types??? So you surely need to tell the compiler about the pointer type, for some quite important behaviour depends on that info. And there is pointer arithmetic, e. If we have. Then, pc[4] will be the character byte you find four characters after the 0x address, being at 0x But you'll find pd[4] not at 0x , but at 0x , 32 bytes after the base address, as indexing a double pointer will count in 8-byte steps instead of single bytes.

So, once again, you need to tell the compiler about the pointer type to allow for correct pointer arithmetic. One important feature of the C language is that the type information is kept only in the compiler. At runtime, the pi pointer's 8 bytes in memory don't contain the information that pi is an int pointer. It's the compiler that produces different machine code for operations on pointers of different types.

So, if you get a pointer with value 0xABCDEF0 and no other information, you can't tell if it's a double , an int or a function pointer or whatever else. Only if your compiled code contains a "debug" attachment, you might find the info there that the pi variable is an int pointer, and if your 0xABCDEF0 pointer value comes from that variable, then you know that you have to look for a 4-byte?



0コメント

  • 1000 / 1000