Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-19-2009, 03:31 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 148
rraagg is on a distinguished road
Default Extra Characters from Char?? Why?

Hi All I'm new to mac and c++ programming.
In the code below I am trying to store the first character of the entered
text. Problem: For some reason it adds extra characters at the end even
though I've only asked for the first character, it adds extra four ?'s at the end:

Example:

What is your name?
Smith
Welcome Smith.
The first letter of your name is S????.
len is 5

Heres the code:

Code:
int main (int argc, char * const argv[]) {
   char firstchar [1];
	
    printf("What is your name?\n");
	fgets( gname, knameLength, stdin ); 
	
	if (strlen(gname) > 0)
	{
		gname[ strlen(gname) -1 ] = '\0';			
		printf("Welcome %s. \n",gname);				
		firstchar[0] = '\0';
		firstchar[0] = gname[0];
		printf("The first letter of your name is %s. \n",firstchar);
		printf("len is %d",strlen(firstchar));
	}

    return 0;
}
Any ideas
rraagg is offline   Reply With Quote
Old 07-19-2009, 04:05 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

c strings must be "null terminated" - the must end with a null char (zero). That's the only way the computer knows where the end of the string is. The length of the string is not stored anywhere.

You need to null-terminate your string, like so:

Code:
		firstchar[0] = gname[0];
		firstchar[1] = '\0';
		printf("The first letter of your name is %s. \n",firstchar);
__________________

Free Games!
smasher is offline   Reply With Quote
Old 07-20-2009, 04:08 AM   #3 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 148
rraagg is on a distinguished road
Thumbs up

Quote:
Originally Posted by smasher View Post
c strings must be "null terminated" - the must end with a null char (zero). That's the only way the computer knows where the end of the string is. The length of the string is not stored anywhere.

You need to null-terminate your string, like so:

Code:
		firstchar[0] = gname[0];
		firstchar[1] = '\0';
		printf("The first letter of your name is %s. \n",firstchar);
Thank you very much Exactly what it is. Is skimmed the chapter on strings! (VB programmer hehe )
rraagg is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 459
16 members and 443 guests
Domele, Duncan C, Feldspar, karatebasker, MacBook MH, Objective Zero, patapple, Paul Slocum, peterwilli, pipposanta, PixelInteractive, Punkjumper, rubyeim54, SLIC, taylor202, Today's Posts
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,694
Threads: 94,137
Posts: 402,950
Top Poster: BrianSlick (7,990)
Welcome to our newest member, peterwilli
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:02 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0