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 variablenand initializes it with the value98. -
int *p = &n;This line declares a pointer to an integerpand assigns it the address ofn. So,pis now pointing ton. -
p = 402;This line is trying to assign the integer value402to the pointerp. This is not allowed becausepis 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 be the output of the following code?int main() { int a = 10; int *p = &a; *p = 20; printf("%d\n", a); return 0;}
In the below code -For what value of n does the program print 21 ?
What is the size of *p in this code?int **p;
What is the output for the following code?123456789101112131415#include <stdio.h> int main() { int n = 5; int arr[5] = {1, 2, 3, 4, 5}; int e = 0; for (int i = 0; i < n; ++i) { if (arr[i] % 2 == 0) { ++e; } } double p = (double)e / n * 100; printf("%.2f", p); return 0;}
What will be the output of the following code?1234567891011121314151617#include <stdio.h>int main() { int a = 100; int *p = &a; int count = 0; while (count < 1) { *p += 1; ++count; } int b = *p; printf("%d, %d", a, b); return 0; }
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.