| | |
scanning char array for \0 problem
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 6
Reputation:
Solved Threads: 0
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:
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?
C Syntax (Toggle Plain Text)
for( i=0 ; charArray[i] != '\0' ; i++ )
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?
>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.
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.
New members chased away this month: 5
•
•
Join Date: Jan 2008
Posts: 6
Reputation:
Solved Threads: 0
here ya go:
C Syntax (Toggle Plain Text)
#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]); } }
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.
If you want to get a string from the user try using fgets().
Good luck.
•
•
Join Date: Jan 2008
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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.
![]() |
Similar Threads
- urgent help postfix evaluation (C++)
- functions using strings and arrays (C++)
- Scanning a text file for a string (C++)
Other Threads in the C Forum
- Previous Thread: Putting Whitespaces and newlines
- Next Thread: display array element with passing value...no undesstand~help
Views: 2085 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C
#include * .net append array arrays asterisks bash binarysearch changingto char character cm command copyimagefile creafecopyofanytypeoffileinc createprocess() database directory dynamic execv feet fgets file floatingpointvalidation fork forloop framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux hacking histogram homework ide include incrementoperators input intmain() iso kernel keyboard kilometer lazy license linked linkedlist linux list lists looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf openwebfoundation overwrite pause pdf pointer pointers posix process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming spoonfeeding standard strchr string student system testing threads turboc unix urboc user whythiscodecausesegmentationfault windowsapi






