Demonstrates the use of pointer declarations and operators and debug this progra
ID: 3908855 • Letter: D
Question
Demonstrates the use of pointer declarations
and operators and debug this program
#include <stdio.h>
main()
{
int num=123; /* A regular integer variable */
int *p_num; /* Declares an integer pointer */
printf("num is %d ", num); /* Prints value of num */
printf("The address of num is %ld ", &num); /* Prints
num's location */
p_num = # /* Puts address of num in p_num,
in effect making p_num point
to num */
/* No * in front of p_num) */
printf("p_num is %d ", *p_num); /* Prints value of
num */
printf("p_num is %ld ", p_num); /* Prints location
of num */
Explanation / Answer
Code:
#include <stdio.h>
main()
{
int num=123; /* A regular integer variable */
int *p_num; /* Declares an integer pointer */
printf("num is %d ", num); /* Prints value of num */
printf("The address of num is %ld ", &num); /* Prints
num's location */
p_num = # /* Puts address of num in p_num,
in effect making p_num point
to num */
/* No * in front of p_num) */
printf("*p_num is %d ", *p_num); /* Prints value of
num */
printf("p_num is %ld ", p_num); /* Prints location
of num */
*p_num=*p_num+1;
printf("After incrementing *p_num , num is %d ",num); /*changes made to *p_num is reflected in num too*/
}
There was no error in the given code . An extra statement is added to demonstrate the relationship between the pointer variable and the normal integer variable.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.