Solved

Arrays last character will be "/" ?

Posted on 2006-07-16
3
283 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

713 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