/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Problem 10 For each of the following, write... [FREE SOLUTION] | 91Ó°ÊÓ

91Ó°ÊÓ

For each of the following, write a single statement that performs the specified task. Assume that long integer variables value1 and value2 have been declared and value1 has been initialized to 200000. a. Declare the variable longPtr to be a pointer to an object of type long. b. Assign the address of variable value1 to pointer variable longPtr. c. Print the value of the object pointed to by longPtr. d. Assign the value of the object pointed to by longPtr to variable value2. e. Print the value of value2. f. Print the address of value1. g. Print the address stored in longPtr. Is the value printed the same as value1's address

Short Answer

Expert verified
The address stored in `longPtr` is the same as `value1`'s address.

Step by step solution

01

Declare Pointer Variable

Declare a pointer variable named `longPtr` that can store the address of a long integer using the following statement in C:\[\text{long *longPtr;}\] This initializes `longPtr` as a pointer capable of holding addresses of long integer type variables.
02

Assign Address to Pointer

Assign the address of `value1` to the pointer `longPtr` using the following statement in C:\[\text{longPtr = \&value1}\] This statement uses the address-of operator `&` to obtain the memory address of `value1` and stores it in `longPtr`.
03

Print Value at Pointer Address

Print the value stored at the memory address pointed to by `longPtr` using:\[\text{printf("Value pointed to by longPtr is: \%ld", *longPtr);}\] The asterisk `*` is used to dereference the pointer, accessing the value stored at the address `longPtr` points to.
04

Assign Value to Another Variable

Assign the value of the object pointed to by `longPtr` to `value2` with:\[\text{value2 = *longPtr;}\] This dereferences `longPtr` to get the current value of `value1` and assigns it to `value2`.
05

Print Value of Second Variable

Print the value of `value2` using:\[\text{printf("Value of value2 is: \%ld", value2);}\] This statement confirms that `value2` now holds the same value as `value1` after assignment.
06

Print Address of Original Variable

Print the address of `value1` using:\[\text{printf("Address of value1 is: \%p", (void*)\&value1);}\] This prints the memory address where `value1` is stored, using `&value1`.
07

Compare Address Stored in Pointer

Print the address stored in `longPtr` using:\[\text{printf("Address stored in longPtr is: \%p", (void*)longPtr);}\] This prints the same address as `value1` since `longPtr` was assigned this address in Step 2.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with 91Ó°ÊÓ!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Pointer Declaration
In C++, pointers are variables that store memory addresses rather than data values directly. When declaring a pointer, you must indicate that it is a pointer and specify the data type it will point to. For example, to declare a pointer
  • that can point to a `long` integer, we write the statement: `long *longPtr;`.

This tells the compiler that `longPtr` is a pointer variable capable of holding the address of a `long` integer. The asterisk (`*`) in the declaration signifies that we are defining a pointer, not a regular variable.
Memory Addressing
Every variable in a C++ program is stored somewhere in the computer's memory, and each location has its own address. When you need to access the location of a variable, you use its memory address. To obtain the address,
  • you use the address-of operator `&`.
  • For example: `longPtr = &value1;` assigns the address of `value1` to the pointer `longPtr`.

This operation allows us to access or manipulate the variable `value1` indirectly through the pointer.
Pointer Dereferencing
Dereferencing a pointer means accessing the value stored in the memory location the pointer refers to. In C++, this is done using the asterisk (`*`) operator. If you have a pointer like `longPtr`, you can access
  • the value it points to by writing: `*longPtr`.

Whenever you see the dereference operator `*` in the context of a pointer, think of it as the key to open the box into which the pointer is looking.
Variable Assignment
In any programming language, assigning values to variables is a fundamental task. In the context of pointers, this can mean assigning an address to a pointer or a value to a variable.
Once you have dereferenced a pointer to get a value,
  • you can use that value in expressions or assign it to another variable as follows: `value2 = *longPtr;`.

This assigns the value pointed to by `longPtr` to `value2`, effectively copying the value from the original variable to another.
Printf Function
The `printf` function is a staple of C and C++ programming, used for formatted output to the screen. When working with pointers and addresses, `printf` can display both values and memory locations.
For example, to print the value pointed by a pointer
  • we use: `printf("Value pointed to by longPtr is: %ld", *longPtr);`.

In another scenario, to print the address stored in a pointer, use `%p` in the format string: `printf("Address stored in longPtr is: %p", (void*)longPtr);`. This helps visualize what's happening in memory.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

(A Metric Conversion Program) Write a program that will assist the user with metric conversions. Your program should allow the user to specify the names of the units as strings (i.e., centimeters, liters, grams, etc., for the metric system and inches, quarts, pounds, etc., for the English system) and should respond to simple questions such as "How many inches are in 2 meters?" "How many liters are in 10 quarts?" Your program should recognize invalid conversions. For example, the question "How many feet are in 5 kilograms?" is not meaningful, because "feet" are units of length, while "kilograms" are units of weight

State whether the following are true or false. If false, explain why. a. Two pointers that point to different arrays cannot be compared meaningfully. b. Because the name of an array is a pointer to the first element of the array, array names can be manipulated in precisely the same manner as pointers.

Write two versions of each string-comparison function in Fig. 8.30. The first version should use array subscripting, and the second should use pointers and pointer arithmetic.

Perform the task specified by each of the following statements: a. Write the function header for a function called exchange that takes two pointers to double-precision, floating-point numbers x and y as parameters and does not return a value. b. Write the function prototype for the function in part (a). c. Write the function header for a function called evaluate that returns an integer and that takes as parameters integer x and a pointer to function poly. Function poly takes an integer parameter and returns an integer. d. Write the function prototype for the function in part (c). e. Write two statements that each initialize character array vowel with the string of vowels, "AEIOU".

Write a program that encodes English language phrases into pig Latin. Pig Latin is a form of coded language often used for amusement. Many variations exist in the methods used to form pig Latin phrases. For simplicity, use the following algorithm: To form a pig-Latin phrase from an English-language phrase, tokenize the phrase into words with function strtok. To translate each English word into a pig-Latin word, place the first letter of the English word at the end of the English word and add the letters ay." Thus, the word "jump" becomes "umpjay," the word "the" becomes "hetay" and the word "computer" becomes "omputercay." Blanks between words remain as blanks. Assume that the English phrase consists of words separated by blanks, there are no punctuation marks and all words have two or more letters. Function printLatinword should display each word. [Hint: Each time a token is found in a call to strtok, pass the token pointer to function printLatinword and print the pig-Latin word.]

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.