RSS Forums RSS

scanning char array for \0 problem

Please support our C advertiser: Programming Forums
Thread Solved
Reply
Posts: 6
Reputation: Matt You is an unknown quantity at this point 
Solved Threads: 0
Matt You Matt You is offline Offline
Newbie Poster

scanning char array for \0 problem

  #1  
Jan 22nd, 2008
I'm trying to make a function that can scan a char array, and finish it's business when it comes to a '\0'. so here is the for loop i came up with:
for( i=0 ; charArray[i] != '\0' ; i++ )
But here is an example of entered data: "1234 432"

It seems to end the loop at the space. So are spaces and null characters similar? how can i differentiate between the two so that it keeps going right through a space and only stops at a null?
AddThis Social Bookmark Button
Reply With Quote  
Posts: 7,460
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 676
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: scanning char array for \0 problem

  #2  
Jan 22nd, 2008
>So are spaces and null characters similar?
No, not at all.

>how can i differentiate between the two
You don't need to differentiate between the two. You need to figure out why your code is stopping when you don't think it should. We can help, but you need to post more code. Preferably a complete program that exhibits the problem.
I'm here to prove you wrong.
Reply With Quote  
Posts: 6
Reputation: Matt You is an unknown quantity at this point 
Solved Threads: 0
Matt You Matt You is offline Offline
Newbie Poster

Re: scanning char array for \0 problem

  #3  
Jan 22nd, 2008
here ya go:
#include <stdio.h>

int dummy;

int stringSplitter(char arrayToSplit[], int arraySize);

int main()
	{
	char userString[20];

	printf("Enter 3 numbers, seperated by spaces: ");
	scanf( "%s", userString);
	
	stringSplitter(userString, 20);
	
    scanf("%d", dummy);
    }

int stringSplitter(char arrayToSplit[], int arraySize)
	{
	unsigned int i;

	for( i=0 ; arrayToSplit[i]!='\0' ; i++ )
		{
		printf("%c", arrayToSplit[i]);			

		}	

	}
Reply With Quote  
Posts: 1,908
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Solved Threads: 204
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: scanning char array for \0 problem

  #4  
Jan 22nd, 2008
Ah, the loop ends at the space because the string ends at the space. scanf stops reading input at spaces. So when you enter "1234 432" the userString gets "1234", and " 432" is left waiting to be read.

If you want to get a string from the user try using fgets().

Good luck.
Reply With Quote  
Posts: 6
Reputation: Matt You is an unknown quantity at this point 
Solved Threads: 0
Matt You Matt You is offline Offline
Newbie Poster

Re: scanning char array for \0 problem

  #5  
Jan 22nd, 2008
Originally Posted by Duoas View Post
Ah, the loop ends at the space because the string ends at the space. scanf stops reading input at spaces. So when you enter "1234 432" the userString gets "1234", and " 432" is left waiting to be read.

If you want to get a string from the user try using fgets().

Good luck.


Thank you! I never would have figured that out. It all works the way it should now.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 1585 | Replies: 4 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 6:14 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC