What are the advantages and disadvantages of using direct pointers manipulation instead of array indexing for processing the elements in an array? (specifically in C++)
It usually depends on the situation. I don't think there's a rule of thumb.
In some cases, array indexes are better. For example when you have allocated an array
char* ptr = malloc(SIZE);
and you need the value of ptr to not change, because you want to
free it later, then you can work with indexes.
Or if you get a pointer as a function argument
void func(char* ptr)
and you need to run over the array, then you can increment the
pointer itself and you won't need to create a new variable to use
as an index.
In most cases however, it depends on your own preferences.
pointer indexing is faster than array indexing because of low
level machine code
If we are talking about assembler it is the usage of 2 accumulators
vs. 1 because an array is essentially a variable containing the
address in memory while the index is essentially another variable
in memory,
When incrementing an array index you first increment the variable for the array position and then use that offset on the array to calculate your position and that’s 2 steps.When you have a pointer you are taking the literal address and directly adding to it.So you have a one step process
Note that they both deal with addresses and adding, but 1 does iti n two steps while the other does it in 2.
I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any.
What are the advantages and disadvantages of using direct pointers manipulation instead of array indexing for...
I am trying to write the following code using pointers and traversal by pointers instead of indexing (in C++). The following code is correct: void shift(int arr[], int n) { int temp = arr[n - 1]; int temp1; for (int i = 0; i < n; i++) { temp1 = arr[i]; arr[i] = temp; temp = temp1; } } This is my ATTEMPT at writing it with pointers (and traversal by pointers!) but I know it is wrong. I believe...
Discuss the advantages and disadvantages of using circular logging instead of continuous logging.
why is LIBOR being replaced? Advantages and disadvantages of using SOFR instead of LIBOR
In Control Systems, what are all of the advantages / disadvantages of using a in series lead-lag compensation controller instead of just a Proportional Controller.
I want to know about the advantages and disadvantages of displaying viscous damping free vibration response with natural frequency and dimensionless damping ratio specifically by using [ X**+2(zetta)W(n)X*+W(n)^2X=0 ] instead of [ mX**+cX*+kx=0 ], what is the advantages and disadvantages?
Could we use water instead of mercury to make a barometer? What advantages and disadvantages would be associated with the use of water? Explain. Vacuum 29.92 in Air Pressure Air Pressure Mercury
What advantages and disadvantages exist for using a panel of arbitrators?
What are some advantages and disadvantages with using a HRIS system?
What are the advantages and disadvantages of crude and adjusted rates? b. What types of information are found by using specific rates instead of crude rates?
Write a c program that finds the uncommon elements from two array elements using pointers only . For example : Enter the length of the array one : 5 Enter the elements of the array one: 9 8 5 6 3 Enter the length of the array two: 4 Enter the elements of the array two: 6 9 2 1 output: 8 5 3 2 1 void uncommon_ele(int *a, int n1, int *b, int n2, int *c, int*size); The function...