So I am in school learning how to program... we just did up a little program to count the number of characters in an array. I wrote it so that it would loop, counting as it goes, and end when it hits the NULL thingy....
It does not however end when it should... and with a printf() I display the contents of each element, and they are just random characters it seems.
Any help will be appreciated :)
It does not however end when it should... and with a printf() I display the contents of each element, and they are just random characters it seems.
Any help will be appreciated :)
Code:
#include "stdafx.h"
#include <stdio.h>
char first[20];
char *nameptr;
int counter=0;
int main()
{
printf("Enter your first name: ");
scanf("%s",first);
nameptr=first;
while(nameptr!=NULL)
{
nameptr++;
counter++;
printf("pointer %c\n",nameptr);
printf("counter %d\n",counter);
}
printf("\n%d characters in your name",counter);
return 0;
}
Comment