Problem 56

author
By Supreme2023-03-16
Problem Statement :
Write a C program to print the address of character and the character of string using pointer
Code:

#include <stdio.h>

int main() {

//56. Write a C program to print the address of character and the character of string using
//pointer.

printf("\t\tAddress Of Character\n");
printf("================================================\n\n");

char str[] = "Supreme";
char *ptr;

ptr = str;

printf("The string is: %s\n", str);
printf("\n");

while (*ptr != '\0') {
printf("The address of character %c is: %p\n", *ptr, ptr);
printf("The character is: %c\n", *ptr);
printf("\n");
ptr++;
}

return 0;
}

Description :
  • This program demonstrates the use of pointers to print the address and value of each character in a string.
  • The program includes the standard input/output library <stdio.h>.
  • The main() function begins.
  • An array of characters called str is declared and initialized with the string "Supreme".
  • A pointer to a character called ptr is declared.
  • The ptr pointer is assigned the address of the first element of the str array.
  • The string "The string is: Supreme" is printed to the console using printf().
  • A while loop is used to iterate over each character in the str array.
  • In the loop body, the printf() function is used to print the address of the current character pointed to by ptr using the %p format specifier.
  • The printf() function is used again to print the character itself using the %c format specifier.
  • The ptr pointer is incremented to point to the next character in the array.
  • The loop continues until the null character (\0) is reached, indicating the end of the string.
  • The main() function returns 0, indicating successful program execution.
Output Image:
Output-image

© Copyright 2022. All right reserved, GTU Coder.