Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CRAZY POINTER ARRAYS #include int main() { char *fruit[] = { watermelon, banana, pear, apple, coconut, grape, blueberry }; int x; for(x=0;x An array of

CRAZY POINTER ARRAYS

#include  int main() { char *fruit[] = { "watermelon", "banana", "pear", "apple", "coconut", "grape", "blueberry" }; int x; for(x=0;x 

An array of pointers is declared in Crazy Pointer Arrays. It works similarly to an array of strings, although in this construction you dont need to specifically count individual string lengths. Thats because the array is really an array of pointers, or memory locations. Each string dwells somewhere in memory. The array simply lists where each one starts.

Exercise 1: Type the source code from Crazy Pointer Arrays into your editor. Build and run to confirm that it works.

Which part of Crazy Pointer Arrays do you think could be improved?

Exercise 2: Using information below as your guide, replace the array notation at Line 17 in Crazy Pointer Arrays with pointer notation.

Array alpha[] Pointer a
alpha[0] *a
alpha[1] *(a+1)
alpha[2] *(a+2)
alpha[3] *(a+3)
alpha[n] *(a+n)

The reason that your solution to Exercise 2 works (assuming that you got it correct) is that the fruit array contains pointers. The value of each element is another pointer. But thats nothing; consider Pointers-to-Pointers Example.

POINTERS-TO-POINTERS EXAMPLE

#include  int main() { char *fruit[] = { "watermelon", "banana", "pear", "apple", "coconut", "grape", "blueberry" }; int x; for(x=0;x 

Line 18 in Pointers-to-Pointers Example contains the dreaded, feared, avoided, and cursed ** notation, or double-pointer notation.

Exercise 3: Carefully type the source code from Pointers-to-Pointers Example into your editor. Compile and run.

To understand the **(fruit+x) construct, you must work from the inside out:

fruit+x

Variable fruit contains a memory address. Its a pointer! The x is a value incrementing by one unit. In this case, the unit is an address because all elements of the fruit array are pointers.

*(fruit+x)

Youve seen the preceding construction already. Its the contents of the address fruit+x. From the code, fruitis an array of pointers. So the result of the preceding operation is . . . a pointer!

**(fruit+x)

Finally, you get a pointer to a pointer or put better a peeker to a peeker. If the inside peeker is a memory address, the outside peeker (the first asterisk) is the content of that memory address.

image text in transcribed

It helps to remember that the ** operator is almost always (but not exclusively) tied to an array of pointers; or, if you want to make it simple, to an array of strings. The first column is the address of an array of pointers, the second column is the pointer itself (a string), and the column on the right is the first character of the string.

If youre still confused, consider mulling over pointer notation and array notation:

Pointer Notation Array Notation Description
**ptr *array[] Declares an array of pointers.
*ptr array[0] The address of the first pointer in the array; for a string array, the first string.
*(ptr+0) array[0] The same as the preceding entry.
**ptr array[0][0] The first element of the first pointer in the array; the first character of the first string in the array.
**(ptr+1) array[1][0] The first element of the second pointer in the array; the first character of the second string.
*(*(ptr+1)) array[1][0] The same as the preceding entry.
*(*(ptr+a)+b) array[a][b] Element b of pointer a.
**(ptr+a)+b array[a][0]+b This item isnt really what you want. What this item represents is the value of element 0 at pointer a plus the value of variable b. Use the *(*(ptr+a)+b) notation instead.

In the table, pointer notation (using variable ptr) is compared with the equivalent array notation (using variable array).

Exercise 4: Rework your source code from Exercise 3 so that each individual character in a string is displayed, one at a time, by using the putchar() function. If you can write the entire putchar() operation as a while loops condition, you really understand the material.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Basics Computer EngineeringInformation Warehouse Basics From Science

Authors: Odiljon Jakbarov ,Anvarkhan Majidov

1st Edition

620675183X, 978-6206751830

More Books

Students also viewed these Databases questions