Is it a good idea to process two-dimensional arrays using pointers?
for( p = &a[0][0]; p < &a[N][N]; p++){
*p = 0;
}
Or is it better to use indexing?
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.
The way you have denoted your array doesn’t guarantee that pointer arithmetic will always work. Given how you’ve declared your two-dimensional array, it is probably not an issue, but it is certainly not good style.
However, imagine instead the array is allocated dynamically. Something like this:
This is a two-dimensional array (of sorts), but will not guarantee that allocated memory will be contiguous and hence would break your loop.