Pages

2013-04-16

A simple exercise of C's Pointer


A pointer point to string "We are best friends."
Please calculate the number of characters in the above string.


/*
 * Pointer_exercise.c
 *
 *  Created on: 2013/4/15
 *      Author: Chris
 */

#include <stdio.h>
#include <stdlib.h>

int main(void){
char *ptr="We are best friends.";

int i=0,sum=0;

while(*(ptr+i)!='\0'){
	printf("%c",*(ptr+i));
	sum+=1;
	i++;
}
printf(" There are %d characters.",sum);
return 0;

}
Output:



We are best friends. There are 20 characters.

No comments:

Post a Comment