Question

1. Explain the error. char c = 'A'; double *p = &c; 2. Give the value...

1. Explain the error.

char c = 'A';

double *p = &c;

2.

Give the value of the left-hand side variable in each assignment statement. Assume the lines are executed sequentially. Give brief explanation for each of your answer.

int main()

{

   char blocks[3] = {'A','B','C'};

   char *ptr = &blocks[0];

   char temp;

   temp = blocks[0];

   temp = *(blocks + 2);

   temp = *(ptr + 1);

   temp = *ptr;

   ptr = blocks + 1;

   temp = *ptr;

   temp = *(ptr + 1);

   ptr = blocks;

   temp = *++ptr;

   temp = ++*ptr;

   temp = *ptr++;

   temp = *ptr;

   return 0;

}

Homework Answers

Answer #1

1)

c is declared as a charachter (char data type)

while declaring a pointer it is important for the compiler to know what data type it is pointing at

hence , data type of the pointer is always given as the data type of the variable which is to be pointed by the pointer

the correct code will be

char c = 'A';

char*p = &c;

2)

int main()

{

   char blocks[3] = {'A','B','C'};

   char *ptr = &blocks[0]; // address of first element of the array (address where A is stored)

// adding '&' at the start of any variable name will give the address of that variable

   char temp;

   temp = blocks[0]; // 'A' the value at zeroth position of the array is passed to temp

   temp = *(blocks + 2); // 'C'

// the name of the array can be used to get the address of the first element of the array

// *blocks will return the value at zeroth position of the array

// if a * is added at the start of the variable name and the variable is an address of some other variable

// the value of the other variable will be returned

   temp = *(ptr + 1); // 'B'

// ptr had the address of the first element of the array form the second line of main function

// adding 1 to the address of second element will give the adress of second element

.// * the address of second varable will return the second element of the array

   temp = *ptr; // address of first element of the array

   ptr = blocks + 1; // address of the second element

   temp = *ptr; // 'B'

// as ptr was given the address of the second element , *ptr will give the contents of the address in ptr

   temp = *(ptr + 1); // 'C'

// adding 1 to ptr which had the address of second element will give the address of third element

// adding a * to it will give the value at the address of third element ( which is third element itself)

   ptr = blocks; // address of first element

   temp = *++ptr; // 'B'

// ptr was given address of first element in previous line

// increment '++' at beginning means the increment the value first then execute rest of the line

// the value of ptr is incremented to next address i.e. address of second element

// * gives the value of second element

   temp = ++*ptr; // 'C'

// *ptr will give 'B' but incrementation of 1 will make it C

// not because next element in the array is C

// its is due to the fact that adding 1 to ascii code of B will give the ascii code of C

// the value of ptr will still remain the address of the second element

   temp = *ptr++; // B

// increment operater is after the variable name

// so the statement is executed first and then the incrementation takes place

// ptr had the address of the second element

// the value has updated to the address of third element after the execution of the statement

   temp = *ptr; // C

// the value of ptr was updated to the address of the third element of the array after the execution of previous statement

   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
How do I fix my error of binding on line 74? #include <iostream> #include <fstream> #include...
How do I fix my error of binding on line 74? #include <iostream> #include <fstream> #include <cctype> #include <cstring> #include <iomanip> using namespace std; #include "AvlTree.h" class WordCount { public:     char *word;     int *lines;     int count;     int size;     bool operator<(const WordCount &rhs) const {return strcmp(word, rhs.word) < 0;}     bool operator!= (const WordCount &rhs) const {return strcmp(word, rhs.word) != 0;}     WordCount():lines(NULL), count(0), size(0) {word = new char[1]; word[0] = '\0';}     friend ostream& operator<<...
For each part labeled P(n), there is a warning/error/problem that goes with it. Write down what...
For each part labeled P(n), there is a warning/error/problem that goes with it. Write down what the issue was in the `Error:` section of each problem. And fix the code to make it work. // P0 #include <stdio.h> #include <stdlib.h> /* Error: */ void fib(int* A, int n); int main(int argc, char *argv[]) { int buf[10]; unsigned int i; char *str; char *printThisOne; char *word; int *integers; int foo; int *bar; char *someText; // P1 for (i = 0; i...
1.Select all C++ functions that must be defined for implementing deep copies in your class which...
1.Select all C++ functions that must be defined for implementing deep copies in your class which has instance variables pointing to dynamic memories. Group of answer choices a.Default Constructor b.Copy Constructor c.Overloading assignment operator d.Writing own friend functions to directly access dynamically allocated memories e.Writing own destructor to free the allocated memory f.Overloading new operator 2. Match the memory source (right side) of the variables (left side) of the following code snippet which is a part of 'myProg' program. Note...
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...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...
c++ on MS VS it gives an error that on line 33 uninitialized local variable used(...
c++ on MS VS it gives an error that on line 33 uninitialized local variable used( or see comment in code). On other compilers, code runs fine. ===================================== #include <iostream> using namespace std; // Node structure w/ power and coefficient struct Node {    int coeff;    int pow;    struct Node *next; }; void create(int x, int y, struct Node** temp) {    //declare structs    struct Node *a, *b;    //temp    b = *temp;    //if b...
Consider the following variable definitions: char a, *b, *c; int d[2], *e; int i, *j; How...
Consider the following variable definitions: char a, *b, *c; int d[2], *e; int i, *j; How many total bytes does this code allocate for variables?  Assume a 32-bit representation for integer and pointer values. a     sizeof(char) b     sizeof(char *) c     sizeof(char *) d    2*sizeof(int) e     sizeof(int *) i     sizeof(int) j     sizeof(int *) Total number of bytes ? What is the output of the following piece of code? (Use the above variable definitions). j = &i; *j = 50;                      /* i = 50 */ e = &d[0]; *e...
1) Explain the difference between: Array of characters like char c[] = {'A', 'B', 'C'}; AND...
1) Explain the difference between: Array of characters like char c[] = {'A', 'B', 'C'}; AND c-string like string = "ABC"; 2) We can have two pointers that point to one memory location such as: int *intPtr1, *intPtr2; intPtr1 = &x; intPtr2 = &x; a) What method we have to make sure only one pointer points to a variable? b) Give an example. 3) a) Can a pointer to a constant receive an address of a constant item and/or non-constant...
A.6 ... static int x = 1; int y = x * 2; void t1() {...
A.6 ... static int x = 1; int y = x * 2; void t1() {                 y++;                 cout << "x: " << x << " | y: " << y << endl;                 y += 1;                 x -= -1; } void t2() {                 int* x = &y;                 cout << "x: " << x << " | y: " << y << endl; } void t3() {                 int y = x;                 static int x...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT