function

D

Default User

CBFalconer said:
... snip about Cunningham trolling ...

Maybe so. But I am not willing to kick him in the face. He does
no harm.


You're entitled to believe that that's what I'm doing. It's not.



Brian
 
K

Kenny McCormack

Kenny said:
This is a really, really, reall[y] bad idea. Never, *ever* use gets(),

Surely you exagerate.
I think using gets() is pretty innocuous.

Depends. In a "proof of concept" context, it can be a handy tool
- but in a "mission-critical" context, its use constitutes a
failure to exercise due diligence.

Nobody is defending gets(). I'm just objecting to the stunning
level of exageration that is so common in CLC. Or, in RR's excellent
phrasing, "posturing".
 
A

Antoninus Twink

But I think it is perfectly clear that Default Loser is mentally impaired.
Initially, I assumed that that was what we were discussing here.

I don't think anyone was disputing that Default Loser is mentally
impaired - that much is obvious to anyone with an IQ above room
temperature...
 
A

Antoninus Twink

*blink* CBF says something sensible, and separates himself from the
nastiest manifestation of the schoolground bullying that gives the CLC
regulars their kicks... another demerit point for his dwindling Clique
Membership stock.
You're entitled to believe that that's what I'm doing. It's not.

Yeah, sure. You're just a great guy, a totally normal and well-rounded
human being who we'd all love to have a beer with. Kick someone in the
face? You? Unimaginable.
 
B

Bill Cunningham

Pete,

You code and the suggestions of others hav prompted me to check fgets more
closely. I guess it adds the newline. Now the \0 Is added by my
understanding by the compiler to a string and that's the only time \0 is
added.

Bill
 
S

santosh

Bill said:
Pete,

You code and the suggestions of others hav prompted me to check fgets
more closely. I guess it adds the newline. Now the \0 Is added by my
understanding by the compiler to a string and that's the only time \0
is added.

No. In this case, it's fgets that adds the terminating null character.
The compiler is not the only entity that adds null characters. Several
standard library functions also do this.

In your case the problem is not the '\0' but the '\n' before it. Use
strchr to find and remove it before doing the comparison with strcmp.
 
N

Nick Keighley

as I've remarked before. Please quote properly.

    Might I have to change type with one of the data type changing functions
to change char pass[10] to an arithmetic type to do a comparison?

this makes no sense. Why would you "change type" what is a "data type
changeing
function"? There's no such thing in C.
The string
"ded" either exists or doesn't
what?


and if needs to evaluate that.

how can it do that?
"Pass by
value." someone once told me.

parameters in C are passed by value. So what?

So many hints that a mathematician designed
this language.

since mathematical functions don't pass by value
I don't understand why you say this

oh,

and v-plonk since you pay no attention to what you are told.

(yes I've heard of the short term memory problem)
 
C

CBFalconer

santosh said:
No. In this case, it's fgets that adds the terminating null
character. The compiler is not the only entity that adds null
characters. Several standard library functions also do this.

In your case the problem is not the '\0' but the '\n' before it.
Use strchr to find and remove it before doing the comparison
with strcmp.

That's the slow way. Since fgets always ends operation when it
receives a '\n', any such character is at the end of the line
(otherwise it is a maximul line, and not complete). Therefore the
code:

lgh = strlen(s);
if ('\n' == s[lgh]) s[lgh] = '\0';

will always remove it. Or get my ggets routine at:

<http://cbfalconer.home.att.net/download/ggets.zip>

and always get complete lines with the terminal '\n' removed.
 
B

Bill Cunningham

Don't take this as an insult, but since it seems you suffer from
attention and memory related difficulties, why don't you try a language
that automates more things for you like, say, Python or Java or Ruby
etc.? C is fairly low level and demands rigorous attention to details.

In fact this latest post of yours is a good example. Languages like Java
let you directly compare two strings, while you must manually do this
in C.

Just a suggestion.

I tried C++ first and wizzed through it compared to C. But I am learning
more and more. I have made progress through insights. I can stop posting to
this forum when I have a question but continue learning C the hard way. C++
is a good language. There are still big chunks of C I don't know like
structs yet. As far for mental impairments try taking 3mg of Klonopin a day.
That's what it's called in the US. Read the side effects alone of that and
you'll see you could drive around town in circles and forget an earlier
conversation from that day. I am going to read on style though. I see so
many complicated functions I don't know where to start and something like
clc is a big help.

Bill
 
B

Bill Cunningham

Kenny McCormack said:
Surely you exagerate. "dozens", to me, means at least 24.

I'd say the number of people who've commented significantly in this
thread is about 5, maybe 10.

My problem has been solved. I will ask clc only in extreme circumstance
when I can't solve something. Thanks to everyone I caught right onto this.
As far as style is


main(){

permisssable as long as I put the closing brace as such
} and on a line by it's self. I've seen pretty tough code. Look at linux
kernel code. I'm lost in that because of the shortcuts I don't know yet.

Bill
 
B

Bill Cunningham

/* BEGIN new.c */

#include <stdio.h>
#include <string.h>

int main(void)
{
char pass[10];
char *p;

printf("Authorization ");
fflush(stdout);
fgets(pass, 10, stdin);
p = memchr(pass, '\n', 10);
if (p != NULL) {
*p = '\0';
if (strcmp(pass, "ded") == 0) {
puts("success");
} else {
puts("error");
}
}
return 0;
}

/* END new.c */
OH boy.. A new technique. I will look up memchr() here and see what you're
trying to do. Are you using memchr to eliminate the \n or to just find it?

Bill
 
E

Eric Sosman

CBFalconer said:
[... about stripping the '\n' from an fgets()'ed line ...]
That's the slow way. Since fgets always ends operation when it
receives a '\n', any such character is at the end of the line
(otherwise it is a maximul line, and not complete). Therefore the
code:

lgh = strlen(s);
if ('\n' == s[lgh]) s[lgh] = '\0';

will always remove it. Or get my ggets routine at:

Off by one: The `if' can never test true. Replace
`lgh' by `lgh-1' to fix it -- and then worry about whether
the length could ever be zero, which would make `lgh-1'
very very large ...

A function that's perhaps less familiar yields a
clean one-line solution:

s[ strcspn(s, "\n") ] = '\0';
 
B

Bill Cunningham

as I've remarked before. Please quote properly.

Might I have to change type with one of the data type changing functions
to change char pass[10] to an arithmetic type to do a comparison?

this makes no sense. Why would you "change type" what is a "data type
changeing
function"? There's no such thing in C.

Oh I was thinking about atoi for some reason.

Bill
 
B

Barry Schwarz

My problem has been solved. I will ask clc only in extreme circumstance
when I can't solve something. Thanks to everyone I caught right onto this.
As far as style is


main(){

permisssable as long as I put the closing brace as such
} and on a line by it's self. I've seen pretty tough code. Look at linux
kernel code. I'm lost in that because of the shortcuts I don't know yet.

The compiler does not care whether the closing brace is on a line by
itself or not. However, failing to specify the return type of a
function has been officially outlawed in the C99 standard. It used to
implicitly declare the function as returning int but no longer.
Therefore, the answer to your question is - it is NOT permissible. Not
because of style but because of grammar.


Remove del for email
 
B

Barry Schwarz

santosh said:
No. In this case, it's fgets that adds the terminating null
character. The compiler is not the only entity that adds null
characters. Several standard library functions also do this.

In your case the problem is not the '\0' but the '\n' before it.
Use strchr to find and remove it before doing the comparison
with strcmp.

That's the slow way. Since fgets always ends operation when it
receives a '\n', any such character is at the end of the line
(otherwise it is a maximul line, and not complete). Therefore the
code:

lgh = strlen(s);
if ('\n' == s[lgh]) s[lgh] = '\0';

Why do you think strlen is faster than strchr? Both step through the
string looking for something. strchr will usually stop looking one
character sooner.


Remove del for email
 
B

Barry Schwarz

/* BEGIN new.c */

#include <stdio.h>
#include <string.h>

int main(void)
{
char pass[10];
char *p;

printf("Authorization ");
fflush(stdout);
fgets(pass, 10, stdin);
p = memchr(pass, '\n', 10);
if (p != NULL) {
*p = '\0';
if (strcmp(pass, "ded") == 0) {
puts("success");
} else {
puts("error");
}
}
return 0;
}

/* END new.c */
OH boy.. A new technique. I will look up memchr() here and see what you're
trying to do. Are you using memchr to eliminate the \n or to just find it?

If memchr eliminated the \n, the statement *p = '\0'; would be
unnecessary.

Also be aware that no compare is performed and no message is produced
if the user enters ten characters before pressing ENTER.


Remove del for email
 
B

Barry Schwarz

as I've remarked before. Please quote properly.

Might I have to change type with one of the data type changing functions
to change char pass[10] to an arithmetic type to do a comparison?

this makes no sense. Why would you "change type" what is a "data type
changeing
function"? There's no such thing in C.

Oh I was thinking about atoi for some reason.

And what do you think atoi would do with a string containing "ded"?


Remove del for email
 
C

CBFalconer

Eric said:
CBFalconer said:
[... about stripping the '\n' from an fgets()'ed line ...]
That's the slow way. Since fgets always ends operation when it
receives a '\n', any such character is at the end of the line
(otherwise it is a maximul line, and not complete). Therefore
the code:

lgh = strlen(s);
if ('\n' == s[lgh]) s[lgh] = '\0';

will always remove it. Or get my ggets routine at:

Off by one: The `if' can never test true. Replace `lgh' by
`lgh-1' to fix it -- and then worry about whether the length
could ever be zero, which would make `lgh-1' very very large.

Right. Use (I think):

if (lgh && ('\n' == s[lgh - 1]) s[lgh - 1] = '\0';

which looks uglier than it is :)
 
C

CBFalconer

Barry said:
CBFalconer said:
santosh said:
Bill Cunningham wrote:

You code and the suggestions of others hav prompted me to check
fgets more closely. I guess it adds the newline. Now the \0 Is
added by my understanding by the compiler to a string and that's
the only time \0 is added.

No. In this case, it's fgets that adds the terminating null
character. The compiler is not the only entity that adds null
characters. Several standard library functions also do this.

In your case the problem is not the '\0' but the '\n' before it.
Use strchr to find and remove it before doing the comparison
with strcmp.

That's the slow way. Since fgets always ends operation when it
receives a '\n', any such character is at the end of the line
(otherwise it is a maximul line, and not complete). Therefore
the code:

lgh = strlen(s);
if ('\n' == s[lgh]) s[lgh] = '\0';

Why do you think strlen is faster than strchr? Both step through the
string looking for something. strchr will usually stop looking one
character sooner.

strchr has to check each value against '\n'. strlen just looks for
a zero. Something like:

s = str;
while (*s++) continue;
return s-str-1;

while strchr has make the loop portion:

while ('\n' - *s++) continue;

which does a lot more many times. Zero is usually detectable by
hardware. There is also a complication returning the "NULL if not
found".
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top