Question

2. AC++ program contains the following C++ code (assume an int occupies 2 bytes each in memory): int x[ ] = { 10, 20, 30, 40, 50, 60, 70, 80); int *ptrx ptrx = x; Array x starts at memory location 2140. a. What is the value assigned to ptrx? b. What is the value of (x+2)? c. What is the value of *x? d. What is the value of (*x+2)? e. What is the value of *(x+2)? f. What is the value of ptrx +3? g. What is the value of ptrx after the statement ptrx &(x[4]); Write a loop that will access each member of x using pointer offset notation h. 3. Given the following declarations: char ptrstuff int n Write the one statement that will dynamically allocate a character array of size n. a. b. Write the one statement that would delete the dynamically allocated array

basic c++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

int x[] = {10,20,30,40,50,60,70,80};

int *ptrx;

ptrx = x;

pointer ptrx would be equivalent and would have very similar properties. The main difference being that ptrx can be assigned a different address, whereas array x can never be assigned anything, and will always represent the same block of 20 elements of type int.

Here array x starts at memory location 2140. I am taking same addresses of both x and ptrx. So ptrx has same memory address as x .

Answers:

a. .

b. For value of (x+2), x starts form location 2140. Here (x+2) represents the memory address of third     element of array x, because index starts from 0 , 1 ,2 …. And int occupies 2 byte each in memory. So the value will be 2140+2+2 = 2144.

c. *x represent the value stored at first index i.e. 0, that is 10

d. In C part the value of *x is 10 so the value of (*x + 2) = 10+2 = 12

e. *(x+2) represents the value stored at third index. So value of *(x+2) = 30

f. The value of ptrx+3 will be the address of fourth element in x. So per+3 = 2140 + 2 + 2 + 2 = 2146

g. value of ptrx after statement ptrx = &(x[4]); indicates the address of fifth element address of x that will be = 2140 + 2 + 2 + 2 + 2 = 2148

h. loop:-

                int i;

                for(i=0;i<8;i++){

                                cout<< *(ptrx+i) << ‘\n’;                               // It print the value pointed by ptrx on ith index

}

2. char * ptrstuff;

    int n;

                1. For allocate dynamically:

                *ptrstuff = (char*)malloc(sizeof(char) * n);

                2. To delete dynamically allocated array:

                delete[] ptrdtuff;

Add a comment
Know the answer?
Add Answer to:
basic c++ 2. AC++ program contains the following C++ code (assume an int occupies 2 bytes...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX...

    Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX_NAME_LENGTH]; int customer_number[N_CUSTOMERS] A program uses a text file that contains the following data on each line: The customer number is an int, and the first and last names are alphabetic strings that contain no whitespace. The last and first names themselves are however separated by whitespace. Write a C function with the following prototype: void read_customer (char name[][MAX_NAME_LENGTH], int number[], int position, FILE *cust_file) Your function should read a...

  • C++ Chapter 16 Problem: Implement #25 as a template function (Demonstrate using int, double, string, and...

    C++ Chapter 16 Problem: Implement #25 as a template function (Demonstrate using int, double, string, and x,y pair object) 24. Write a function that searches a numeric array for a specified value. The function should return the subscript of the element containing the value if it is found in the array. If the value is not found, the function should throw an exception. 25. Write a function that dynamically allocates a block of memory and returns a char pointer to...

  • using visual studio 2) For each of the following, write C++ statements that perform the specified...

    using visual studio 2) For each of the following, write C++ statements that perform the specified task.. (Ref: classwork named pointer2) a) Declare a built-in array of type unsigned int called values with five elements, and initialize the elements to the even integers from 2 to 10. b) Declare a pointer vPtr that points to a variable of type unsigned int. c) Write two separate statements that assign the starting address of array values to pointer variable vPtr. d) Use...

  • WRITE THE NECESSARY C++ STATEMENTS 1. (10 points) Allocate memory dynamically for an int array with...

    WRITE THE NECESSARY C++ STATEMENTS 1. (10 points) Allocate memory dynamically for an int array with 100 elements. Next, assign the value of 1 to all the elements of the array and, finally, delete the array.

  • Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You...

    Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You can initialize the variables to any legal value of your choosing. Next, declare and initialize a pointer variable to each of the variables Using printf), output the following: The address of each of the first three variables. Use the "Ox%x" format specifier to print the addresses in hexadecimal notation The value of each variable. They should equal the value you gave...

  • #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='...

    #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='z';      pc=&(c[8]);      pk=&(k[0]);      for (i=0; i<9; i++)     {           *pc=a-(char)i;           pc--;           *pk=(int)a-i;           pk++;           }                     return 0; }//end of main Answer the below questions with the above code Write out the memory map for the above C code in the following table. For array variables, list the address range for the entire array. Assume the memory address starts from 100, that is, the address for a...

  • In C++ Write a program that contains a class called VideoGame. The class should contain the...

    In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....

  • In C++ and comment so I UNDERSTAND Implement a class named DynamicArray that has the following...

    In C++ and comment so I UNDERSTAND Implement a class named DynamicArray that has the following members: A pointer to hold a dynamically allocated array, of type int. A member variable to hold the size of the array. A default constructor, which will allocate an array of size 10 A parameterized constructor, which takes a size and use the size to allocate array. A copy constructor, which performs deep copy. A copy assignment operator, which performs deep copy and supports...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Using Microsoft Visual Studio. 1) Complete the following C++ program by adding more line of code...

    Using Microsoft Visual Studio. 1) Complete the following C++ program by adding more line of code for 8-bit signed array, 16-bit unsigned array, 16-bit signed array, 32-bit signed array and 32-bit signed array. 2) Fill in all the blanks in Table 1 using your completed code, following the hints provided within the table. 3) Fill in all the blanks in Table 2 using your completed code, following the hints provided within the table. C++ Program #include <stdio.h> #include <iostream> int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT