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...
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...
Question 1 Which statement is false about what Data Types defines Question 1 options: What values...
Question 1 Which statement is false about what Data Types defines Question 1 options: What values a variable cannot hold? How much memory will be reserved for the variable? What value a variable will hold? How the program will use the data type? Question 2 Using the structure below, which of the following statements about creating an array (size 20) of structures are not true? struct Employee{     string emp_id;     string emp_name;     string emp_sex; }; Question 2 options:...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue...
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue = red + 2 * 5 red++; blue = blue + red; cout << blue; 4 points    QUESTION 2 Is the following statement true or false? The Boolean expression in the following if statement will be true for all values of x in the range from 10 to 20 (including the endpoints) and false for all other values: int x; if (x >=...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
1. Vim commands: a. How do you auto indent your program? b. Explain what the following...
1. Vim commands: a. How do you auto indent your program? b. Explain what the following commands do: dd, y3, p, :set cindent (1 pt) VIM exercises These exercises on the computer need to be repeated by each student in the pair. This is to ensure that both students understand how to get around in Linux!!! For this part of the lab, you will create a .vimrc file that will help you develop your C++ programs using VIM. First, we...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT