In C, when is it preferrable to use one over the other?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
and you need the value of
ptrto 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
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.