-
Notifications
You must be signed in to change notification settings - Fork 0
C_Pointers.c
ani9977 edited this page Sep 11, 2024
·
1 revision
Here’s the updated C code with added documentation and an explanation:
#include <stdio.h>
/**
* @brief The main function demonstrates pointer usage in C.
*
* This function initializes an integer variable, assigns its address to a pointer,
* and then prints both the pointer's address and the value it points to.
*
* @return int Returns 0 to indicate successful execution.
*/
int main() {
int number = 10; /**< The integer variable initialized to 10. */
int *ptr = &number; /**< Pointer variable that stores the address of 'number'. */
// Print the address stored in the pointer
printf("Pointer value: %p\n", ptr);
// Print the value at the address pointed to by 'ptr'
printf("Pointer dereferenced value: %d\n", *ptr);
return 0; /**< Return 0 to indicate successful execution. */
}-
Variable Initialization:
-
int number = 10;declares an integer variablenumberand initializes it with the value10. -
int *ptr = &number;declares a pointerptrand assigns it the address of thenumbervariable.
-
-
Pointer Usage:
-
printf("Pointer value: %p\n", ptr);prints the address stored in the pointerptr. The%pformat specifier is used for printing pointer values. -
printf("Pointer dereferenced value: %d\n", *ptr);prints the value at the address pointed to byptr. The*operator is used to dereference the pointer, which gives the value ofnumber.
-
-
Return Statement:
-
return 0;indicates successful execution of the program. The value0is returned to the operating system.
-