Why is pointer arithmetic dangerous
For example , a pointer of type integer can hold the address of a variable of type integer. A pointer of character type can hold the address of a variable of character type. Pointers must be declared before they can be used, just like a normal variable. A pointer is associated with a type such as int and double too. This adds complexity to your codebase and as the code grows, it becomes really easy for an error to slip in because somewhere deep in the call stack, the pointer struct is changed.
Faster and more efficient code can be written because pointers are closer to the hardware. That is, the compiler can more easily translate the operation into machine code.
There is not as much overhead associated with pointers as might be present with other operators. The use of pointers can lead to confusion of ownership which can directly or indirectly lead to memory leaks. To pass the value by pointer , argument pointers are passed to the functions just like any other value. However, pointer arithmetic is what the OP asked for, so pointer arithmetic is what he got. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Pointer Arithmetic with structures Ask Question. Asked 9 years, 10 months ago. Active 9 years, 10 months ago. Viewed 5k times. Improve this question. Sangeeth Saravanaraj RStyle RStyle 1 1 gold badge 10 10 silver badges 24 24 bronze badges. Perhaps you meant 'manipulate the pointer'? Add a comment. As far as I know, an expression that has array type is converted to pointer type that points to the inital element of the array.
When using the sizeof operator. Had it been a pointer, it would be the size of a pointer 4 or 8 in most common platforms. Why doens't this work in C? The RHS of the expression evaluates to a pointer to the second element of arr. However, that line does not work due to 3 above. It cannot be used as the LHS of an assignment operator. This works, but it is wrong solution:. Because arr is not a pointer but a char array. You can verify this by checking sizeof arr. The biggest difference between a pointer and an array is that you can directly assign a value to a pointer, but you can't do this to an array.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.
Why isn't this pointer arithmetic allowed in C? Asked 5 years, 2 months ago. Active 5 years, 2 months ago. Viewed 5k times. Improve this question. Jin Jin 1, 2 2 gold badges 13 13 silver badges 25 25 bronze badges. Because whomever taught you than arrays and pointers are exactly the same, was over-simplifying to the point of being wrong. Some thing similar to stackoverflow.
The pointer arithmetic is fine; the problem is the assignment. MooingDuck I do realize what happens on the right side, but this is not the OP's problem. The problem is left side. Arrays are not assignable. So there is no reason to speak about pointers here as long as the problem is the Array itself. Even if the Array name is a Pointer this doesn't mean that one should speak about this part.
The Answers below covers all this. Show 3 more comments.
0コメント