Saturday 31 August 2013

Pointer and Character array | C Programming Language Material pdf



Pointer and Character array

Pointers are very useful in accessing character arrays. The character strings can be assigned with a character pointer.
Ex:
char array[ ] = “Love India”; array version
char *p= “Love India”; pointer version
Here p points to the first character in a string.
Ex1:
#include<stdio.h>
#include<conio.h>
main()
{
int i;
char *p= “ Love India”;
clrscr();
while (*p! = '\0')
{
printf (“ %c “, *p);
“Programming with C” course material
Department of CSE, GIT, GU Page 141
p++;
}
}

Output: Love India

Ex2:
# include <stdio.h>
# include <conio.h>
int slen(char *);
main ( )
{
char name[50];
int k;
printf (“ enter string: “);
gets (name);
k = slen (name);
printf (“ the string length is %d ” , k);
}
int slen (char *s)
{
int n;
for (n=0; (*s) != '\0'; n++)
s++;
return (n) ;
}

Output:
enter string: College
the string length is 7

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved