Why addition and multiplication of two address is not possible in pointers?
If we perform addition, multiplication, division or modulus on ptr_1 and ptr_2, then the resultant address may or may not be a valid address. That can be out of range or invalid address. This is the reason compiler doesn't allow these operations on valid addresses.
We can perform arithmetic operations on the pointers like addition, subtraction, etc.
- yes it is allowed, *x is not a pointer but a value pointed by x . ...
- Your code is fine, but it never multiplies or divides a pointer. ...
- Think about what would happen. ...
- Thank you! ...
- A pointer is simply a normal variable that holds the address of something else as its value.
Operations not possible with pointers
These are: Addition of two pointer variables. Multiplication of a pointer with a constant value. Division of a pointer with a constant value.
- int main() { int first, second, *p, *q, sum;
- printf("Enter two integers to add\n"); scanf("%d%d", &first, &second);
- p = &first; q = &second;
- sum = *p + *q;
- printf("Sum of the numbers = %d\n", sum);
- return 0; }
No! It is perfectly legal, moral, and wholesome to use malloc() and delete in the same program, or to use new and free() in the same program. But it is illegal, immoral, and despicable to call free() with a pointer allocated via new, or to call delete on a pointer allocated via malloc().
Pointer Arithmetic
Unlike regular numbers, adding 1 to a pointer will increment its value (a memory address) by the size of its underlying data type. To simplify the logic behind this, think of pointer arithmetic the same way you think about array indexing.
A pointer variable has the address of another variable as its value. The pointer variable is itself a variable with an address. Two pointer variables can point to the same address.
- A pointer variable does not store value directly just like int, float variables. ...
- A pointer variable must be initialized by a valid memory reference (memory address).
Declaring multiple object pointers on one line causes compiler error.
Can two pointers be subtracted?
Two pointers can also be subtracted from each other if the following conditions are satisfied: Both pointers will point to elements of same array; or one past the last element of same array. The result of the subtraction must be representable in ptrdiff_t data type, which is defined in stddef.
Disadvantages of pointers:- 1)we can access the restricted memory area. 2) Pointers require one additional dereference, meaning that the final code must read the variable's pointer from memory, then read the variable from the pointed-to memory. This is slower than reading the value directly from memory.

Java doesn't have pointers; Java has references.
Two pointers is really an easy and effective technique that is typically used for searching pairs in a sorted array. Given a sorted array A (sorted in ascending order), having N integers, find if there exists any pair of elements (A[i], A[j]) such that their sum is equal to X.
Differences between delete and free()
When the delete operator destroys the allocated memory, then it calls the destructor of the class in C++, whereas the free() function does not call the destructor; it only frees the memory from the heap. The delete() operator is faster than the free() function.
No, because it can't. For example: void *ptr = malloc(number_of_bytes); if (ptr == NULL) exit(EXIT_FAILURE);
Pointer to object is not destroyed, value or memory block pointed by pointer is destroyed.
Pointers contain addresses. Adding two addresses makes no sense, because you have no idea what you would point to. Subtracting two addresses lets you compute the offset between these two addresses, which may be very useful in some situations.
The first pointer is used to store the address of the variable. And the second pointer is used to store the address of the first pointer. That is why they are also known as double-pointers. We can use a pointer to a pointer to change the values of normal pointers or create a variable-sized 2-D array.
So a pointer really only stores the memory address. Information about the size of the pointee is in the type (just as a non-pointer variable's type tells the compiler how large that variable is).
What problems do pointers have?
The potential health problems that Pointers are prone to include: Hip dysplasia – hip joint laxity as a result of poor development, which will eventually lead to arthritis. Gastric Dilatation Volvulus (GDV) – often known as 'bloat', this is a condition where the stomach twists, trapping the contents and gases.
That means a pointer initialized as an integer can only store an address of integer datatype. So when used for pointing(storing address) another datatype than initialized datatype illegal pointer error can occur.
The void pointer in C is a pointer that is not associated with any data types. It points to some data location in the storage. This means that it points to the address of variables. It is also called the general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.
It depends upon different issues like Operating system, CPU architecture etc. Usually it depends upon the word size of underlying processor for example for a 32 bit computer the pointer size can be 4 bytes for a 64 bit computer the pointer size can be 8 bytes. So for a specific architecture pointer size will be fixed.
Originally Answered: why is pointer indexing faster than array indexing? It's straight forward that array will always will be faster. Because memory allocation of array is continuous. So accessing array is much faster compare to pointer where memory allocation might or might not be continuous.
The only limitations with void pointers are: you cannot dereference void pointer for obvious reasons. sizeof(void) is illegal. you cannot perform pointer arithmetics on void pointers.
Pointers as Function Return
The function multiply() takes two pointers to int . It returns a pointer to int as well which stores the address where the product is stored.
# What are the two common problems with pointers? Dangling pointers (dangerous) and Lost heap-dynamic variable.
You can perform a limited number of arithmetic operations on pointers. These operations are: Increment and decrement. Addition and subtraction.
Techopedia Explains Address-of Operator (&)
To conduct parameter passing by reference, such as by name. To establish pointer values. Address-of operators point to the location in the memory because the value of the pointer is the memory address/location where the data item resides in memory.
What is the rule for subtracting two pointers?
Two pointers can also be subtracted from each other if the following conditions are satisfied: Both pointers will point to elements of same array; or one past the last element of same array. The result of the subtraction must be representable in ptrdiff_t data type, which is defined in stddef.
Pointers are powerful because they allow you to directly access memory addresses. This same usefulness also makes them very dangerous. If you don't use your pointers correctly you can access garbage data or leave them dangling. Another product of incorrect usage is memory leaks.
2.2 Pointers Can Be Dangerous
Because pointers provide access a memory location and because data and executable code exist in memory together, misuses of pointers can lead to both bizarre effects and very subtle errors.
When a pointer is added with a value, the value is first multiplied by the size of data type and then added to the pointer.
Pointer Subtraction
It turns out you can subtract two pointers of the same type. The result is the distance (in array elements) between the two elements.
- A pointer variable does not store value directly just like int, float variables. ...
- A pointer variable must be initialized by a valid memory reference (memory address).
Yes two pointer variable can point to the same memory address. The same applies to linked list address.
Dereferencing a null pointer always results in undefined behavior and can cause crashes. If the compiler finds a pointer dereference, it treats that pointer as nonnull. As a result, the optimizer may remove null equality checks for dereferenced pointers.