Solved

Arrays last character will be "/" ?

Posted on 2006-07-16
3
262 Views
Last Modified: 2010-04-15
Usually in a char[] array in C, the last character will be "/" ?

char  sz[7];
sscanf(line, "level %s",  &sz[]);
for (int i = 0; sz[i] == "/"; i++)
{
if (sz[i] == ":")
continue;
.....
.....
}

sz[] will be getting value in the format 120:0
So can I use the above to trace through sz[] array from beginning till the end?
If its wrong please guide me the correct way.
Thanks.
0
Comment
Question by:gopikrish
3 Comments
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 25 total points
ID: 17118103
Hi gopikrish,

The normal way to walk a string looks something like:

for ( i = 0; s[i] != '\0'; i++ )

Although some shorten it to:

for ( i = 0; s[i]; i++ )

which I personally dont like but is acceptable.

Why do you thing that there is a "\" at te end of a string?

Paul
0
 
LVL 8

Assisted Solution

by:manish_regmi
manish_regmi earned 25 total points
ID: 17119170
>> Usually in a char[] array in C, the last character will be "/" ?

No, Usually in a char[] array in C, the last character will be '\0' (null character) not "/"

characters are enclosed in '' not in "".

i dont know what you are trying to achieve but it should be

char  sz[7];
sscanf(line, "level %s",  &sz[]);
for (int i = 0; sz[i] != '\0'; i++)
{
if (sz[i] == ':')
continue;
.....
.....
}

regards
Manish Regmi
0
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17128766
the last character of a C-array according to C99 standards should be '\0', there is no way it could be '/'

so what you have to do is this

char  sz[7];
sscanf(line, "level %s",  &sz[]);
for (int i = 0; sz[i] != '\0'; i++) // or for (int i = 0; sz[i]; i++)
{
if (sz[i] == ":")
continue;
.....
.....
}
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now