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
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?
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...
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];
Find Domain x-int y-int VA HA Hole Sign graph graph a-) y=1/x2+3x-10 b-) y=x+2/(x+2)(x-3) c-) y=x-1/x2-2x+1...
Find Domain x-int y-int VA HA Hole Sign graph graph a-) y=1/x2+3x-10 b-) y=x+2/(x+2)(x-3) c-) y=x-1/x2-2x+1 d-) y=-2/x
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 >=...
Evaluate the following expressions and state the type of each expression: int a = 2; int...
Evaluate the following expressions and state the type of each expression: int a = 2; int b = 3; double c = 3.0; 1. b / 2.0 Type: ( double, boolean, decimal, int )? Value: ( answer )? 2. b ! = 3 Type: ( double, boolean, decimal, int )? Value: ( answer )? 3. a >= 2.0 Type: ( boolean, double, char, int )? Value: ( answer )?
Given that the range of the function Y=M(X) is -4 ≤ y ≤ 25 determine the...
Given that the range of the function Y=M(X) is -4 ≤ y ≤ 25 determine the range of y= √ m(x) Select one: a.0≤ y≤ 5 b.-2≤ y≤ 5 c.0 < y≤ 5 d.-2 < y≤5 The answer is a please explain why the answer is not b,c and d. Thanks in advance
Programming in C: 1. Given the following variable declarations: const size_t n = 50; 2. Write...
Programming in C: 1. Given the following variable declarations: const size_t n = 50; 2. Write the declaration of an an array of pointers to n memory blocks containing 16-bit signed integer values. Use 'x' for the name of the variable. 3. What is the type of the variable x? uint8_t *x[256]; 4. What type does the variable x decay to when used in an expression? uint8_t *x[256] 5. What is the data type of an array of five strings?...
Determine if the following statements are true or false. If it is true, explain why. If...
Determine if the following statements are true or false. If it is true, explain why. If it is false, provide an example. a.) If a and b are positive numbers, then (a+b)^x=a^x+b^x b.) If x < y, then e^x < e^y c.) If 0 < b <1 and x < y then b^x > b^y d.) if e^(kx) > 1, then k > 0 and x >0
For each of the following, assume that we are using a 32-bit system with single-precision (32-bit)...
For each of the following, assume that we are using a 32-bit system with single-precision (32-bit) floating point numbers (floats) in IEEE format, double-precision (64-bit) doubles in IEEE format, and 32-bit integers. Which of the following evaluate to true for all argument values? (Circle each that is always true). char c = .. int x = .. short y = .. float f = .. double d = .. c == (char)(float) c y == (short)(int) y f == (float)(double)...