Question

In this lab, we want to get some practice with pointers -- specifically, the basic syntax...

In this lab, we want to get some practice with pointers -- specifically, the basic syntax of declaring them, storing memory addresses into them, dereferencing them, and seeing how they work with pointer arithmetic in the context of built-in, C-style arrays.

Unless specified otherwise, you can use any valid variable names you wish. The requirements for this program are as follows:

Part A

  1. Declare a pointer-to-int, initialized to nullptr.
  2. Declare an int variable, initialized to some value of your choice.
  3. Assign the address of the int variable into the pointer-to-int.
  4. In a cout statement, dereference the pointer to display the value stored in the int variable.

Part B

  1. Declare a double variable, initialized to some value of your choice.
  2. Declare a pointer-to-double, initialized with the address of the double variable.
  3. In a cout statement, dereference the pointer to display the value stored in the double variable.

Part C

  1. Declare a built-in, C-style array of chars, initialized to hold at least 10 char values using list-initialization syntax.
  2. Declare a pointer-to-char, initialized to point to the first element in your array. Although there are two ways to do this, please use the version with only the name of the array itself (i.e., if your array is named myCharArray, don’t use &myCharArray[0]).
  3. In four cout statements, display the value stored in the fifth element of the array using the each of the following notations:
    • array subscript notation
    • pointer/offset notation with the built-in array name
    • pointer subscript notation
    • pointer/offset notation with the pointer name

Sample output is provided below. Note that your output will vary, depending on the values you chose to store... the important thing here is the syntax you used to produce the output.

Value stored in x: 121

Value stored in y: 2.7

Using array subscript notation: p

Using pointer offset notation with array name: p

Using pointer subscript notation: p

Using pointer offset notation with pointer name: p

C++

Homework Answers

Answer #1
#include<stdio.h>  
int main(){    
    // Part A
    int *p1; 
    int x=121;       
    p1=&x;    
    printf("Value stored in x %d \n",*p1);
    
    // Part B
    double y=2.7;
    double *p2=&y;
    printf("Value stored in y %f \n",*p2);
    
    // Part C
    char z[]={'a', 'v', 'g', 'x', 'p'};
    char *p3=z;
    printf("Using array subscript notation: %c \n",z[4]);
    printf("Using pointer offset notation with array name: %c \n",*(p3+4));
    printf("Using pointer subscript notation: %c \n",p3[4]);
    printf("Using pointer offset notation with pointer name: %c \n",*(z+4));
    return 0; 
} 
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
For questions 1 and 2 declare the array size as name constant first then use it...
For questions 1 and 2 declare the array size as name constant first then use it to declare the array 1. Declare an array named scores of type double and size 30. 2. Declare an array named names of string objects and size 15 3. Given the following array definition: int values[] = {2, 5, 8, 11}; What does each of the following display? cout << values[1];         B) cout << value[3]+values[0];   Define a three element array named letters, initialize it...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
Write a program that accepts as input the mass, in grams, and density, in grams per...
Write a program that accepts as input the mass, in grams, and density, in grams per cubic centimeters, and outputs the volume of the object using the formula: volume = mass / density. Format your output to two decimal places. ** Add Comments ** Print Name and Assignment on screen ** Date ** Submit .cpp file. Demo // This program uses a type cast to avoid an integer division. #include <iostream> // input - output stream #include <fstream> //working file...
In this lab, you complete a partially prewritten C++ program that uses an array. The program...
In this lab, you complete a partially prewritten C++ program that uses an array. The program prompts the user to interactively enter eight batting averages, which the program stores in an array. The program should then find the minimum and maximum batting average stored in the array as well as the average of the eight batting averages. The data file provided for this lab includes the input statement and some variable declarations. Comments are included in the file to help...
1. Given the following multi-way if statement, provide a switch statement, using proper java syntax, that...
1. Given the following multi-way if statement, provide a switch statement, using proper java syntax, that will provide the same function. Char grade; String tstmsg; if (grade == ‘A’) {   tstmsg = “Excellent”; } else if (grade == ‘B’) {   tstmsg = “Good”; } else if (grade == ‘C’) {   tstmsg = “OK”; } else {   tstmsg = “Study More”; } 2.Write the following for statement as a while statement. for (k = 0; k < 3; k++) {   System.out.println...
n this lab, you use what you have learned about parallel arrays to complete a partially...
n this lab, you use what you have learned about parallel arrays to complete a partially completed C++ program. The program should: Either print the name and price for a coffee add-in from the Jumpin’ Jive Coffee Shop Or it should print the message Sorry, we do not carry that. Read the problem description carefully before you begin. The file provided for this lab includes the necessary variable declarations and input statements. You need to write the part of the...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
C ++ program that will read in prices and store them into a two-dimensional array //...
C ++ program that will read in prices and store them into a two-dimensional array // It will print those prices in a table form and the lowest price. // EXAMPLE // Input: // Please input the number of rows from 1 to 10 // 2 // Please input the number of columns from 1 to 10 // 3 // // Input price of item (0,0): 2 // Input price of item (0,1): 4 // Input price of item (0,2):...
Using c++ You may #include only: <iostream> <cmath> “functions.h” Write the function void readForceValuesFromStdIn(double* leftTeam, double*...
Using c++ You may #include only: <iostream> <cmath> “functions.h” Write the function void readForceValuesFromStdIn(double* leftTeam, double* rightTeam, unsigned const int noParticipants) that will read the list of forces exerted by each wizard alternating by the team into the correct array of doubles (see example in problem description) Define the function bool validForces(const double* forces, unsigned const int noParticipants) using the provided code. This function will be used to ensure that the forces exerted by each team’s wizard are non-negative The...
Language:C++ HAS TO WORK IN VISUAL BASIC I have errors on line 102 "expression must have...
Language:C++ HAS TO WORK IN VISUAL BASIC I have errors on line 102 "expression must have a constant value line 107,108,123,124,127,128: array type int[n] not assignable #include<iostream> #include <fstream> using namespace std; #define size 10000 // Displays the current Inventory Data void Display(int box_nums[],int nboxes[],double ppBox[],int n) // Prints Box number , number of boxes and price per box. { cout<<"Box number Number of boxes in stock Price per box"<<"\n"; cout<<"-------------------------------------------------------------\n"; for(int i=0; i<n; i++) { cout<<box_nums[i]<<" "<<nboxes[i]<<" "<<ppBox[i]<<"\n"; }...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT