Question

Question 2 (a) Provide a single line of C++ code that will declare a variable that points to a string value, the variable is named x. [2 marks] (b) You have array c declared as string *c [5]; Give the single line of C+ + code that will assign the string pointer stored in variable x (from above) to the third element of the array c. [2 marks] (c) Write a for loop that initialises the contents of each element of the array c to the NULL pointer. [3 marks] (d) You have the following declarations at the start of a function: int i,j; int *nums [3]; int *x; Briefly explain what will happen if the function attempts to execute the state ment: nums [-1] -&j; [3 marks] (e) Are the contents of a pointer checked at compile-time or run-time? Briefly [2 marks] explain your answer.

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

\color{red}\underline{a:}

string *x;

\color{red}\underline{b:}

c[2] = x;

\color{red}\underline{c:}

for(int i = 0; i < 5; ++i) 
    c[i] = NULL;

\color{red}\underline{d:}

-1 is not a valid index.
so, nums[-1] will give an error.

\color{red}\underline{e:}

pointer variable contains address of a location in main memory.
so, it contents of the pointer can only be checked at the run-time.
Add a comment
Know the answer?
Add Answer to:
Question 2 (a) Provide a single line of C++ code that will declare a variable that...
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
  • Question 1 When you declare a variable of type Single, it contains 0 by default. True...

    Question 1 When you declare a variable of type Single, it contains 0 by default. True False Question 2 The following line declares an array that can contain whole numbers, with 10 elements. Dim Variables(9) As Integer True False Question 3 Once a variable is declared we can not change the variable's data type. True False Question 4 If A is False and B is True, then A Or B is True because A is False. True False Question 5...

  • (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...

    (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....

  • C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point...

    C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point to an int variable named someInt. Assign the value 451 to someInt and output (cout) the variable someInt and output (cout) the value pointed to by intPointer. Write an assignment statement that indirectly stores 900 into the value pointed to by intPointer. Output (cout) the value pointed to by intPointer and output (cout) the variable someInt, 2. Declare a pointer variable charArrPointer and initialize...

  • Write C++ code that creates an integer variable called number. Dereference a given pointer gPointer and...

    Write C++ code that creates an integer variable called number. Dereference a given pointer gPointer and places that value into the variable number. The pointer gPointer will have been declared and set to point to a value before your code runs. Your code will be placed inside the main function with all the appropriate #includes. After your code runs, the test case will execute the following code: cout << "number = " << number << endl; For example: Test Result...

  • in C Structures Homework 1. Write code to accomplish each of the following: a) Using t...

    in C Structures Homework 1. Write code to accomplish each of the following: a) Using t ypedef, declare a structure, Automobile, with the following members: mode1 - string of 25 characters maximum year - integer mpg - integer b) Declare variable car to be of type Automobile. c) Assign the information below to the appropriate fields of variable car: 2012 Infiniti gets 25 mpg. d) Declare an array, vehicle, of 500 Automobiles. e) Assign variable car to the 5th element...

  • a) Declare and instantiate an array named scores of twenty-five elements of type int.   (b)Write a...

    a) Declare and instantiate an array named scores of twenty-five elements of type int.   (b)Write a statement that declares an array namedstreetAddress that contains exactly eighty elements of typechar. 2. In a single statement: declare, create and initialize an arraynamed a of ten elements of type int with the values of the elements (starting with the first) set to 10, 20,..., 100 respectively. 3. Declare an array reference variable, week, and initialize it to an array containing the strings "mon",...

  • Assume the following declarations have been made in main(): int howmany; Scanner kb = new Scanner(System.in);...

    Assume the following declarations have been made in main(): int howmany; Scanner kb = new Scanner(System.in); Random r = new Random(); Write code to do the following: Use a while loop to prompt the user for how many numbers are to be entered. If the user does not enter a positive number (a number greater than 0), display a message saying the number must be positive and to try again. Store this value in the howmany variable. Declare an integer...

  • How do you do the commented part of this question (its the part that is bolded)?...

    How do you do the commented part of this question (its the part that is bolded)? #include <stdio.h> #include <string.h> main(){ int *p, in=10; p = &in; printf("%d\n", *p ); char arr[]="hello"; char *ptr = arr; // different ways to pass the array, at "pointer" level. printf("%s %s %s\n", arr, &arr[0], ptr); //different ways to access arr[0] printf("%c %c %c %c\n", arr[0], *ptr, *arr, ptr[0]); //different ways to access arr[1] printf("%c %c %c %c\n", arr[1], *(ptr+1), *(arr+1), ptr[1] ); //different...

  • | ome Insert Design Layout References Mailings View Help Tell me what you want to do from the l U...

    | ome Insert Design Layout References Mailings View Help Tell me what you want to do from the l Unless you need to edit, it's safer to stay in 11. Write C++ statements that do the following: a. Declare an array alpha of 10 rows and 20 columns of type int. b. Initialize the array alpha to 0. c. Store 1 in the first row and 2 in the remaining rows d. Store 5 in the first column, and make...

  • TRUE/FALSE 1. If pl is an integer pointer variable, with the value of 1000, pl++ changes...

    TRUE/FALSE 1. If pl is an integer pointer variable, with the value of 1000, pl++ changes P1 to point to the memory location 1001. ANSWER: T 2. When you return a dynamic array to the freestore, you must include the number of elements in the array 3. You can assign an array to a pointer variable. 4. The size of dynamic arrays must be declared at compile time. 5. int *pl; declares a static variable. ANSWER: F ANSWER: F ANSWER:...

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