Question

2. Given the following declarations, explain why some of the following assignments are incorrect int x...

2. Given the following declarations, explain why some of the following assignments are incorrect

int x = 10;

char y = 'A';

int *ptr1;

char *ptr2 = &y;

a. x = y;

b. ptr1 = ptr2;

c. ptr1 = &x;

d. x = &ptr1;

e. ptr2 = &x;

Thanks.

Homework Answers

Answer #1

Answer::::

int x = 10;

char y = 'A';

int *ptr1;

char *ptr2 = &y;

These are given declarations...

For option a.) x = y;

If we assign x=y that is if we assign char to the integer variable if will print ASCII value of that character.

In the above example it will print ->> 65 which is ASCII value of 'A'.

For option b) ptr1 = ptr2;

Here ptr1 is integer pointer(int *ptr1) and ptr2 is character pointer (char *ptr2) so we cannot assign a character pointer to the integer pointer because it is assignment of incompatible types and if we print ptr1 then it will print garbage value.

for option c) ptr1 = &x;

Here we can assign a integer variable x (int x) to the integer pointer ptr1(int *ptr1) .The memory address of variable x is stored in pointer ptr1 and if we print ptr1 is shows the output as 10.

for option d) x = &ptr1;

Here we are assigning memory address of pointer ptr1 to the integer variable. Because of this assignment makes a integer from pointer (i.e memory location or address) without cast because we are storing it in a integer variable x.

If we print x then it will print garbage value.

For option e) ptr2 = &x;

Here we are storing memory address of integer variable x into pointer ptr2 . This assignment is of incompatible types.But if we print only ptr2 then it will print garbage value because incompatibility of their types.And if we print *ptr2 then it will print value at address of that variable in this it will print 10 which value of x.

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
boolean x = true; boolean y = false; boolean z = true; Given the following declarations,...
boolean x = true; boolean y = false; boolean z = true; Given the following declarations, evaluate each boolean expression labelled a-f below as either true or false: (Section 3.7) a. y && z b. y || z c. !y d. (x && y) || (x && z) e. !(x && y) f. (!y) && (x && z)
List the errors in the following array declarations. A. int intArray[] = new double[10]; B. int...
List the errors in the following array declarations. A. int intArray[] = new double[10]; B. int intArray[] = new int[1.5]; C. double[] doubleArray = new double[-10] D. int intMatrix[] [] = new int[10]; - Write a method selectRandom that expects an array of integers as a parameter. The method should return the value of an array element at a randomly selected position.
If and else statements: 1a.) Given the following declaration: int temperature; Write an if statement that...
If and else statements: 1a.) Given the following declaration: int temperature; Write an if statement that prints the message "The number is valid" if the variable temperature is within the range of -50 trough 150 (both values excluded). 1b.) Given the following declarations int x; int y; Write an if statement that assigns 100 to x when y is equal to 0. 1c.) Given the following declaration: int grade; Write an if statement that prints the message "The number is...
Given the following code: int x = 0; int y = 10; int *ptr = &x;...
Given the following code: int x = 0; int y = 10; int *ptr = &x; *ptr = -55; x += -52; ptr = &y; *ptr += 52; printf("%d\n", x); What is printed to the screen?
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...
Here are SQL declarations for three tables R, S, and T: CREATE TABLE R(e INT PRIMARY...
Here are SQL declarations for three tables R, S, and T: CREATE TABLE R(e INT PRIMARY KEY, f INT); CREATE TABLE S(c INT PRIMARY KEY, d INT REFERENCES R(e) ON DELETE CASCADE); CREATE TABLE T(a INT PRIMARY KEY, b INT REFERENCES S(c) ON DELETE CASCADE); Suppose R(e,f) contains tuples (1,0), (2,4), (3,5), (4,3), and (5,7). Suppose S(c,d) contains tuples (1,5), (2,2), (3,3), (4,5), and (5,4). Suppose T(a,b) contains tuples (0,2), (1,2), (2,3), (3,4), and (4,4). As a result of the...
Some of the following statements are incorrect.  Identify three of them and explain why you think they...
Some of the following statements are incorrect.  Identify three of them and explain why you think they are incorrect. a) Dividend policy is irrelevant for a company is sometimes true. b) A share purchase is beneficial to the shareholders because it reduces the number of outstanding shares and increases the earnings per share. c) A company will increase dividends when the company makes a profit in any one year. d) Not many companies surveyed want to maintain a smooth dividend from...
1.    Given the following segment of code: (If there is nothing output, write None.) int x;...
1.    Given the following segment of code: (If there is nothing output, write None.) int x; int y; cin >> x; cin >> y; while (x > y) {     x -= 3;     cout << x << " "; } cout << endl;        a.    What are the output and final values of x and y when the input is 10 for x and 0 for y? [2, 2, 2]               Output                                                                                                                                                                                                    x = ______________                                                                                                                                                                                                   ...
Given the following function in C++: int main(void) { int a = 2; int b =...
Given the following function in C++: int main(void) { int a = 2; int b = myFunction(a); a = b + 1; b = myFunction(a); cout << ”b = ” << b << endl; return 0; } int z = myFunction(int x) { static int n = 0; n = n + 1; int z = x + n; return z; } What is printed by the cout on the screen?
Please find the errors of the following code a) int x[5]; int k = 10; for...
Please find the errors of the following code a) int x[5]; int k = 10; for (k = 0; k <= 5; k++) { x[k] = k -1; } b) int x[5]; for (k = 1; k <= 5; k++) { cout << x[k-1] << endl; } c) int x[5]={1,2,3,4,5}, y[5]; y = x; for (k = 0; k < 5; k++) { cout << y[k] << endl; } d) int size; cin >> size; int myArr[size];