problems with logic operations within loops

I

Interrupt

Hi folks I’m working though some exercise trying to teach myself C,
I’m having problem with a function I’ve written. It takes and array
and removes the white spaces. I’m having problems with logic
operations within loops.

My first problem is once If statement executes and “i” is incremented
the else statement doesn’t execute again!! WHY???
*****************************CODE********************************************

/* copy: copy 'from' into 'to'removing white space */
void copy(char to[], char from[], int maxi)
{
int i;

for(i=0;i<=maxi;++i)
{
if(from==' ' || from=='\0' || from=='\t'|| from
=='\n')
{
to = from[i+1]; // if there is white space copy
the next value
i++; //>>>>>>>>>>>>>>>>>>>> problems start here
}

else
{
to = from; // if no white space copy
}
}

}
********************************CODE******************************************

So I thought I’d approach the problem from the opposite angle if it’s
not white space, but the if statement executes whether there is white
space or not…..WHY???

********************************CODE2*****************************************
/* copy: copy 'from' into 'to'removing white space */
void copy(char to[], char from[], int maxi)
{
int i;

for(i=0;i<=maxi;++i)
{
if(from!=' '||from!='\0'||from!='\t'||from!='\n') {
to = from; // if no white space
copy
}

else
{
to = from[i+1]; // if there is white space copy the
next value
i++;
}
}

}
**************************************CODE2*************************************
 
A

Andrew Poelstra

Hi folks I?m working though some exercise trying to teach myself C,
I?m having problem with a function I?ve written. It takes and array
and removes the white spaces. I?m having problems with logic
operations within loops.

My first problem is once If statement executes and ?i? is incremented
the else statement doesn?t execute again!! WHY???
*****************************CODE********************************************

/* copy: copy 'from' into 'to'removing white space */
void copy(char to[], char from[], int maxi)
{
int i;

for(i=0;i<=maxi;++i)

You almost certainly do not want <= here. Think about how
arrays are indexed in C.
{
if(from==' ' || from=='\0' || from=='\t'|| from
=='\n')

{
to = from[i+1]; // if there is white space copy
the next value


You can't use // comments in most C compilers today, and even
if you could you shouldn't on Usenet because they wrap around
like this and then nobody can compile your code.

Also, how do you know i + 1 is a valid index into from?
i++; //>>>>>>>>>>>>>>>>>>>> problems start here

You can't use the same index into to[] and from[], if you
expect to be referencing different offsets in each array.
}

else
{
to = from; // if no white space copy
}
}

}
************************CODE**********************************

So I thought I?d approach the problem from the opposite angle if it?s
not white space, but the if statement executes whether there is white
space or not?..WHY???

************************CODE2*********************************
/* copy: copy 'from' into 'to'removing white space */
void copy(char to[], char from[], int maxi)
{
int i;

for(i=0;i<=maxi;++i)
{
if(from!=' '||from!='\0'||from!='\t'||from!='\n')


Of course. When do you expect that (X != 1 || X != 2) will be false?
Perhaps if you would space your code decently it would be clearer.
{
to = from; // if no white space
copy
}

else
{
to = from[i+1]; // if there is white space copy the
next value
i++;


....and you still haven't fixed the original problem.
}
}

}
******************************CODE2*****************************

nb I have removed sixteen stars from each of these lines.
 
T

Tom St Denis

You can't use // comments in most C compilers today, and even
if you could you shouldn't on Usenet because they wrap around
like this and then nobody can compile your code.

Um what? // are standard in C99 aren't they? GCC certainly will
accept them except when in -ansi mode. Though yes, it's good to use /
* */ instead, mostly for backwards compatibility but also to avoid
wrapping.

Tom
 
A

Andrew Poelstra

Um what? // are standard in C99 aren't they? GCC certainly will
accept them except when in -ansi mode. Though yes, it's good to use /
* */ instead, mostly for backwards compatibility but also to avoid
wrapping.

Tom

They are standard in C99, but gcc is not standard in C99 mode ;)

(It is close enough for most purposes, though. I perhaps should
have been clearer.)
 
R

Richard Bos

to = from[i+1]; // if there is white space copy
the next value


You can't use // comments in most C compilers today,


Wrong (and it was wrong in many cases even before C99 made them
official), but...
and even if you could you shouldn't on Usenet because they wrap around
like this and then nobody can compile your code.

....right, as demonstrated.

Richard
 
T

Tom St Denis

They are standard in C99, but gcc is not standard in C99 mode ;)

(It is close enough for most purposes, though. I perhaps should
 have been clearer.)

My point is GCC [and other compilers] silently accepted // by
default. You can only get gcc to whine about it when you enter ANSI
mode [e.g. C89/C90].

Though the rest of your point stands, C developers should use /**/ by
default.

Tom
 
A

Andrew Poelstra

They are standard in C99, but gcc is not standard in C99 mode ;)

(It is close enough for most purposes, though. I perhaps should
 have been clearer.)

My point is GCC [and other compilers] silently accepted // by
default. You can only get gcc to whine about it when you enter ANSI
mode [e.g. C89/C90].

I (implicitly) meant that in default mode, gcc is not a C
compiler. (Rather a "GNU-C" compiler, which is a slightly
different beast.)

My bad - I try to word things to avoid these discussions,
since anyone who cares has in all likelihood heard them a
thousand times before.
 
J

jacob navia

Richard Heathfield a écrit :
Yes. Most C compilers, however, are not C99 compilers.

This is yet another lie. Please name one compiler that doesn't
accept // comments by default.

And no, putting gcc in pedantic mode doesn't count.

This is standard C, and it is widely implemented. I bet you can't
even name one compiler that doesn't accept those comments.
 
K

Keith Thompson

jacob navia said:
Richard Heathfield a écrit :

This is yet another lie. Please name one compiler that doesn't
accept // comments by default.

Accepting // comments doesn't make a C compiler a C99 compiler.
If Richard posted anything untrue, it wasn't in the text that
you quoted.

You need to stop throwing the word "lie" around.

[...]
 
R

Rob Kendrick

Richard Heathfield a écrit :

This is yet another lie. Please name one compiler that doesn't
accept // comments by default.

It is not a lie if you include the context you must have accidentally
snipped and not read. :)
This is standard C, and it is widely implemented. I bet you can't
even name one compiler that doesn't accept those comments.

The Norcroft/Codemist-based compiler I am occasionally forced to use
doesn't.

B.
 
K

Keith Thompson

Rob Kendrick said:
It is not a lie if you include the context you must have accidentally
snipped and not read. :)

It is not a lie given the context he actually quoted. Most C
compilers are *not* C99 compilers.

[...]
 
N

Nick

Richard Heathfield said:
Richard said:
to = from[i+1]; // if there is white space copy
the next value
You can't use // comments in most C compilers today,
Wrong


Wrong.

(and it was wrong in many cases even before C99 made them
official), but...


No, in C99 you can use them - but only six people have a C99
compiler. In other implementations, you have to invoke the compiler in
a non-conforming mode in order to use them. If we accept as C
something that a compiler can only accept in non-conforming mode, we
can cheerfully accept Fortran as C, which is not something I'm
prepared to do.


I do think you're being unnecessarily dogmatic, and confusing for
newcomers, here.

Firstly, in the case of just about every compiler I've come across, the
question isn't one of "invoking in a non-conforming mode", it's a case
of "not very carefully specifying half-a-dozen flags to force it into a
conforming mode".

Even if there are no C99 compilers, what the OP posted was perfectly
good C99 as far as I can see (certainly it appeared to be perfectly good
C with no features that worked differently between C standards, but with
// comments).

To claim you'd like to widen topicality, and then claim that any code
with // comments in it is equivalent to Fortran seems a bit rich, IMO.
 
F

Fred

Hi folks I m working though some exercise trying to teach myself C,
I m having problem with a function I ve written. It takes and array
and removes the white spaces.  I m having problems with logic
operations within loops.

If you use a "for" loop for this, it should look like:

for (i=0;  i<maxi;  i++)

Are you processing the whole string? If so then a "while" loop would be
better, with your test being about whether the current character is a null.

More importantly, you need to have a pointer moving along the input
string, and another moving along the output string. These can't be the
same pointer as they don't move along at the same rate necessarily (in
general they won't).

So it might be something like:

j = 0;
for (i=0;  i<maxi;  i++)
      {
      ch = from;
      if  (isspace(ch)==true)  continue;
      to[j] = ch;
      j++;
      }


Not necessarily. The OP did not include the definition of maxi
in the original posting, so it is possible that the "<=" is
correct (the name could be taken to imply that maxi is
the maximum value that i is allowed to have).
 
J

jacob navia

Richard Heathfield a écrit :
If by "accept" you mean "fail to issue a diagnostic message for", then
no C90-conforming implementations accept them. I will win the bet by
naming Borland C.

#include <stdio.h>

int main(void)
{
// get a diagnostic message
puts("Hello, diagnostic message.");
return 0;
}

When I compile this in conforming mode, I get this output:

Error E2188 scratch.c 5: Expression syntax in function main
*** 1 errors in Compile ***

Sorry but which "borland C" did you use?
I downloaded Turbo C from 1995 (the earliest I could get
that is still available) and compiled that without any problems.

You are using turbo C from 1989, or similar rubbish

You are lying again heathfield.
 
M

Michael Angelo Ravera

Hi folks I’m working though some exercise trying to teach myself C,
I’m having problem with a function I’ve written. It takes and array
and removes the white spaces.  I’m having problems with logic
operations within loops.

My first problem is once If statement executes and “i” is incremented
the else statement doesn’t execute again!! WHY???
*****************************CODE******************************************­**

/* copy: copy 'from' into 'to'removing white space */
void copy(char to[], char from[], int maxi)
{
    int i;

    for(i=0;i<=maxi;++i)
    {
     if(from==' ' || from=='\0' || from=='\t'|| from
=='\n')
        {
           to = from[i+1];       // if there is white space copy
the next value
           i++; //>>>>>>>>>>>>>>>>>>>> problems start here
        }

      else
        {
        to = from;            // if no white space copy
        }
    }

}

********************************CODE***************************************­***

So I thought I’d approach the problem from the opposite angle if it’s
not white space, but the if statement executes whether there is white
space or not…..WHY???

********************************CODE2**************************************­***
/* copy: copy 'from' into 'to'removing white space */
void copy(char to[], char from[], int maxi)
{
    int i;

    for(i=0;i<=maxi;++i)
    {
     if(from!=' '||from!='\0'||from!='\t'||from!='\n')>>>>>>>>executes whether there is white space or not

        {
           to = from;       // if no white space
copy
        }

        else
            {
            to = from[i+1];   // if there is white space copy the
next value
            i++;
            }
    }

}

**************************************CODE2********************************­*****


OK. First, most array functions would have the signature as follows:

void copy(char to[], char from[], int from_size, int to_max_size, int
* to_size)

or

int copy(char to[], char from[], int to_max_size, int from_size)

or even

int copy(char to[], char from[], int from_size)

It is important to know how large the resultant array is, since it can
not be predicted from the size of the input how large the output will
be, and you will want to know how big the result is.

If I were writing the function, and had access to the standard
library, I'd write this function as follows:

int copy (char to [], char from [], int from_size)
{
/*assume that we have at least from_size in the to[] array */
int res;
int idx;
for (res = 0, idx = 0; idx < from_size; idx ++)
{
if (!isspace (from [idx])
to [res ++] = from [idx];
}
return res;
}

If you have some special definitiion of what a whitespace characfter
is, you could substitute it in the test. If you want to stop on a null
or on the length, you could make the test say from "(idx < max_from)
&& from [idx]"

If instead of or in addtion to returning the length of the resulting
string, you'd rather put a null at the end of the buffer, you just do
to [res] = '\0'; ahead of the return line.

If you want to include maximum result length as an argument to the
function, you would augment the test in the for loop with "&& (res <
max_to_size)"

Lots of variations on this are possible. Have fun.
 
E

Eric Sosman

Tim said:
Ahem. Any chance all you chaps could focus on the OP's problem ?

I assumed it had already been dealt with. Still, since you ask:

void copy(char to[], char from[], int maxi)
{
int i = 0;
int j = 0;
while(maxi--)
{
if(isspace(from))


IHYM `isspace( (unsigned char) from )'.
 
J

jacob navia

Richard Heathfield a écrit :
5.6, in conforming mode.
conforming to C 1990. In 2002 Borland did not have a C99 compiler.

Still, it accepts // comments (like all compilers since ages).
And that is what I said. "Unless deliberately crippled" and
in this context you force the compiler to reject // comments by
using a conforming C89 mode.

I will stop here, since you are deliberately confusing people.
 
S

Seebs

Richard Heathfield a écrit :
This is yet another lie.

No, it's not.
Please name one compiler that doesn't
accept // comments by default.

He didn't say they didn't accept // comments by default, he said they
were not C99 compilers.

I think the point he's making is that, for most compilers, there is no mode
where they both genuinely and plausibly claim standards conformance, and
accept // comments, because the only way they could do that would be to
have full C99 support. Since they don't, their C99 mode isn't going to
claim conformance, so you're stuck with their C89 mode, which won't take //
comments.

Now, that may not be totally responsive to your point, because it's pretty
easy to get most compilers into a mode where they accept // comments, but
it is true that doing so will often put them into a mode where some other
reasonably normal stuff gets broken.

-s
 
J

jaysome

On Tue, 26 Jan 2010 17:02:58 +0000, Richard Heathfield

[snip]
In other implementations, you have to invoke the compiler in a
non-conforming mode in order to use them.

Wrong.

I use // comments in C code and invoke the (gcc) compiler in a
conforming mode. The trick to getting this to "work" is to pipe the
output from the compiler into a script that weeds out the warning
about "C++ style comments". The filter applied to the output of gcc is
like this:

grep -v 'warning:.*C\+\+ style comments'

The gcc options I use are specified in a makefile, and look something
like this (the "-Wc," part is particular to the gcc compiler and OS
I'm using, and simply means "pass the following option to the *real*
gcc compiler"):

WARN_BASE=-Wc,-Wall -Wc,-W
WARN_BASE += -Wc,-Wno-unknown-pragmas
WARN_BASE += -Wc,-Wpointer-arith
WARN_BASE += -Wc,-Wshadow
WARN_BASE += -Wc,-Wwrite-strings
WARN=$(WARN_BASE)
WARN += -Wc,-pedantic
WARN += -Wc,-Wbad-function-cast
WARN += -Wc,-Wcast-align
WARN += -Wc,-Wcast-qual
WARN += -Wc,-Wconversion
WARN += -Wc,-Wformat-nonliteral
WARN += -Wc,-Wmissing-declarations
WARN += -Wc,-Wmissing-prototypes
WARN += -Wc,-Wnested-externs
WARN += -Wc,-Wstrict-prototypes
WARN += -Wc,-Wundef

The "pedantic" reader will notice that the "-Wall" option to gcc
doesn't really get you "all" warnings, as you would rightfully expect.
When using the "-Wall" option with gcc, think of it as something like
"-Wmost" or "-Wmany". Go figure.
 
F

frank

Richard said:
to = from[i+1]; // if there is white space copy
the next value

You can't use // comments in most C compilers today,


Wrong (and it was wrong in many cases even before C99 made them
official), but...



Do you remember the program you used to illustrate this difference? It
basically consisted of a printf statement that would toggle a value to
indicate which decade was definitive. (??)
...right, as demonstrated.

Richard

Wrong, as demonstrated.

Heathfield's strarr.h got preprocessed and compiled tonight.
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top