Strings and Pointers

0
37

We can use a pointer to handle the string in the same manner as we do in case of an integer array. We can store the base address of the string in a pointer to a character variable. We then can traverse the string character by character in the same manner. For example,

#include<stdio.h>
void print_String(char *str) 
{
int i=0;
while(str[i] != '\0')  
{		
// str[i] is equivalent to *(str+i)
printf("%c", str[i]);
i++;
}
}

int main()  
{
char str[] = "Welcome - C Programming";
print_String(str);
return 0;
}
Welcome - C Programming

LEAVE A REPLY

Please enter your comment!
Please enter your name here