Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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 = &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 = &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.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote