What is the value of n after the following code is executed?int n = 98;int *p = &n;p = 402;
Question
What is the value of n
after the following code is executed?
int n = 98;
int *p = &n;
p = 402;
Solution
The code you provided will not compile correctly because it is trying to assign an integer value to a pointer variable.
Here's the step by step explanation:
-
int n = 98;
This line declares an integer variablen
and initializes it with the value98
. -
int *p = &n;
This line declares a pointer to an integerp
and assigns it the address ofn
. So,p
is now pointing ton
. -
p = 402;
This line is trying to assign the integer value402
to the pointerp
. This is not allowed becausep
is a pointer and it should be assigned an address of a variable, not a direct integer value.
So, this code will result in a compilation error. If the code was corrected to *p = 402;
then the value of n
would be 402
because p
is pointing to n
and we are changing the value at the address p
is pointing to.
Similar Questions
What will the result of num variable after execution of the following statements? int num = 58; num % = 11; the given code is in c
What is the output of the following code?int arr[] = {1, 2, 3, 4, 5};int *p = arr;p++;printf("%d\n", *p);
How many times is the comparison i >= n performed in the following program?int i = 300, n = 150;main(){ while (i >= n){ i = i-2; n = n+1; }}
write a program to print integer no, from 1 to n where n is the input from the user using do while loop in c language use getch
What will you see on the terminal?int main(void){ int *ptr; *ptr = 98; printf("%d\n", *ptr); return (0);}It doesn’t compileSegmentation Fault980
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.