Question

What are the advantages and disadvantages of using direct pointers manipulation instead of array indexing for...

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++)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
What are the advantages and disadvantages of using direct pointers manipulation instead of array indexing for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT