Solved

Arrays last character will be "/" ?

Posted on 2006-07-16
3
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

738 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