Tell me is it better to use a pointer to navigate an array of values, or is it better to use a subscripted array name?
Submitted by: MuhammadIt's easier for a C compiler to generate good code for pointers than for subscripts.
Say that you have this:
/* X is some type */
X a[ MAX ]; /* array */
X *p; /* pointer */
X x; /* element */
int i; /* index */
Here's one way to loop through all elements:
/* version (a) */
for ( i = 0; i < MAX; ++i )
{
x = a[ i ];
/* do something with x */
}
On the other hand, you could write the loop this way:
/* version (b) */
for ( p = a; p < & a[ MAX ]; ++p )
{
x = *p;
/* do something with x */
}
Submitted by: Muhammad
Say that you have this:
/* X is some type */
X a[ MAX ]; /* array */
X *p; /* pointer */
X x; /* element */
int i; /* index */
Here's one way to loop through all elements:
/* version (a) */
for ( i = 0; i < MAX; ++i )
{
x = a[ i ];
/* do something with x */
}
On the other hand, you could write the loop this way:
/* version (b) */
for ( p = a; p < & a[ MAX ]; ++p )
{
x = *p;
/* do something with x */
}
Submitted by: Muhammad
Read Online Data Structure Arrays Job Interview Questions And Answers
Top Data Structure Arrays Questions
☺ | What will be output if you will execute following c code? |
☺ | What is Array? |
☺ | What is two-dimensional array? |
☺ | Explain Array of pointers? |
☺ | Tell me why can\'t constant values be used to define an array\'s initial size |
Top Data Structure Categories
☺ | Data Center Manager Interview Questions. |
☺ | Creative UI/UX Designers Interview Questions. |
☺ | Sort And Searching Interview Questions. |
☺ | Data Structure Linked list Interview Questions. |
☺ | Data Structure Arrays Interview Questions. |