Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > small string question

Reply
Thread Tools

small string question

 
 
Bill Cunningham
Guest
Posts: n/a
 
      01-07-2008
As I have been studing my tutorial I came up with this question. I took
char passw(char name[]);

and initialied this function in a header with other includes for standard
library headers. This doesn't seem to work so is what I want this?

char passw(char *name);

I haven't got to pointers in the tutorial and I'm taking my time. But in
this particualr function should it be initialized with a pointer? Does
char *name; equal char name[] ?

Bill


 
Reply With Quote
 
 
 
 
Tomás Ó hÉilidhe
Guest
Posts: n/a
 
      01-07-2008
"Bill Cunningham" <(E-Mail Removed)> wrote in comp.lang.c:

> As I have been studing my tutorial I came up with this question. I
> took
> char passw(char name[]);



In C, you can't pass an array by value. The language's misleading syntax,
however, would have you believe that you can. The following three
functions are identical:

void Func(char *name) { *name = 0; name = 0; }
void Func(char name[]) { *name = 0; name = 0; }
void Func(char name[72]) { *name = 0; name = 0 }

In the third one, 72 is ignored. All you've got is a non-const pointer in
all three cases


> and initialied this function in a header with other includes for
> standard library headers.



We declare a function in a header file, and define it in a source file.
Another name for a declaration is a "prototype". I've never see people
use the word "initialiser" though in referring to a function declaration.
Here's what an initialiser is in C:

int arr[5] = {7,3,2,3,4};


> This doesn't seem to work so is what I want
> this?
>
> char passw(char *name);



This is no different from your previous declaration.


> I haven't got to pointers in the tutorial and I'm taking my time. But
> in this particualr function should it be initialized with a pointer?
> Does char *name; equal char name[] ?



Sorry I'm not sure what you're asking.

--
Tomás Ó hÉilidhe
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      01-07-2008

"Tomás Ó hÉilidhe" <(E-Mail Removed)> wrote in message
news:Xns9A1E6BE8E9E4toelavabitcom@194.125.133.14.. .

>> char passw(char *name);

>
>
> This is no different from your previous declaration.
>
>
>> I haven't got to pointers in the tutorial and I'm taking my time. But
>> in this particualr function should it be initialized with a pointer?
>> Does char *name; equal char name[] ?

>
>
> Sorry I'm not sure what you're asking.
>

OK you answered my question. I thought char *name; and char name[]; were
the same. maybe my return in this function is wrong.

char passwd(char name[]){...

return passw();} /*is there an error with this return */

Bill


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      01-07-2008
Wait a minute I think I might've just caught my problem. It's in the
function body where I'm making the error. Not assigning name[] to anything
in the body. What about this.

char passw(char name[])
{char i=name;
....
if (strcmp(i,name2)==0) {printf"ok");}
else if(strcmp(i,name2)!0) {puts("unequal rtry again"); return
passw();}

Bill


 
Reply With Quote
 
cr88192
Guest
Posts: n/a
 
      01-07-2008

"Bill Cunningham" <(E-Mail Removed)> wrote in message
news:eSegj.5898$Xo1.2521@trnddc06...
> Wait a minute I think I might've just caught my problem. It's in the
> function body where I'm making the error. Not assigning name[] to anything
> in the body. What about this.
>
> char passw(char name[])
> {char i=name;
> ....
> if (strcmp(i,name2)==0) {printf"ok");}
> else if(strcmp(i,name2)!0) {puts("unequal rtry again"); return
> passw();}
>


'char *s=name;', would be ok.

'char i=name;' has 2 problems:
'char i;' is an integer type, not a string;
via common practice/tradition, many names have certain reserved meanings and
usages, and you have just violated one of the major ones...

i, j, and k, are almost universally defined, if present, in any function or
context, to be integers...
l, is, most of the time, an integer.

so, j, j, k, and l, should not be declared as anything other than 'int'.
s and t, are commonly, but not as strongly or universally, reserved for
strings.
f, g, and often h, are usually reserved for floats.
p, q, and sometimes r, are often used for generic pointers (usually 'void
*').

there are many such conventions, but going too much into specifics tends to
quickly become programmer/project/codebase specific...

there have been more than a few papers written on the topics of variable
naming and code indendation/formatting, and one should try to at least try
to adhere to the common conventions unless there is some good reason to do
otherwise.


note that the C, C++, and Java communities tend to have different practices
wrt indentation and naming (though, afaik, the i,j,k convention is almost
universal).

but, in any case, there are conventions for these things...


> Bill
>
>



 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      01-07-2008
On Mon, 7 Jan 2008 11:38:31 +1000, "cr88192" <(E-Mail Removed)>
wrote in comp.lang.c:

>
> "Bill Cunningham" <(E-Mail Removed)> wrote in message
> news:eSegj.5898$Xo1.2521@trnddc06...
> > Wait a minute I think I might've just caught my problem. It's in the
> > function body where I'm making the error. Not assigning name[] to anything
> > in the body. What about this.
> >
> > char passw(char name[])
> > {char i=name;
> > ....
> > if (strcmp(i,name2)==0) {printf"ok");}
> > else if(strcmp(i,name2)!0) {puts("unequal rtry again"); return
> > passw();}
> >

>
> 'char *s=name;', would be ok.
>
> 'char i=name;' has 2 problems:
> 'char i;' is an integer type, not a string;
> via common practice/tradition, many names have certain reserved meanings and
> usages, and you have just violated one of the major ones...


The C standard spells out certain name spaces for identifiers that are
reserved for the implementation in various circumstances. The OP's
code did not use any of these reserved names. He has violated
nothing.

Nothing but the C standard specifies reserved identifiers in C.

> i, j, and k, are almost universally defined, if present, in any function or
> context, to be integers...


By whom? It has been a very long time since I wrote any FORTRAN. I
seem to remember that language specifying that undeclared variables
beginning with certain letters were of integer type, while all other
undeclared variables had real type. This has nothing to do with C.

> l, is, most of the time, an integer.
>
> so, j, j, k, and l, should not be declared as anything other than 'int'.
> s and t, are commonly, but not as strongly or universally, reserved for
> strings.


There is nothing reserved about these letters, or most identifiers
beginning with these letters, in C.

Identifiers beginning with "is" or "str" followed by a lower case
latter are reserved, for example, but neither 'i' nor 's' is as a
single letter identifier.

> f, g, and often h, are usually reserved for floats.


Reserved by whom? Not by the C standard, certainly. Perhaps in your
mind they are.

> p, q, and sometimes r, are often used for generic pointers (usually 'void
> *').
>
> there are many such conventions, but going too much into specifics tends to
> quickly become programmer/project/codebase specific...
>
> there have been more than a few papers written on the topics of variable
> naming and code indendation/formatting, and one should try to at least try
> to adhere to the common conventions unless there is some good reason to do
> otherwise.


I do not believe that these "common extensions" exist anywhere other
than in your mind.

> note that the C, C++, and Java communities tend to have different practices
> wrt indentation and naming (though, afaik, the i,j,k convention is almost
> universal).


In the parallel universe that you come from?

> but, in any case, there are conventions for these things...


The best of them tend to forbid all single character variable names.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
Reply With Quote
 
cr88192
Guest
Posts: n/a
 
      01-07-2008

"Jack Klein" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Mon, 7 Jan 2008 11:38:31 +1000, "cr88192" <(E-Mail Removed)>
> wrote in comp.lang.c:
>
>>
>> "Bill Cunningham" <(E-Mail Removed)> wrote in message
>> news:eSegj.5898$Xo1.2521@trnddc06...
>> > Wait a minute I think I might've just caught my problem. It's in the
>> > function body where I'm making the error. Not assigning name[] to
>> > anything
>> > in the body. What about this.
>> >
>> > char passw(char name[])
>> > {char i=name;
>> > ....
>> > if (strcmp(i,name2)==0) {printf"ok");}
>> > else if(strcmp(i,name2)!0) {puts("unequal rtry again");
>> > return
>> > passw();}
>> >

>>
>> 'char *s=name;', would be ok.
>>
>> 'char i=name;' has 2 problems:
>> 'char i;' is an integer type, not a string;
>> via common practice/tradition, many names have certain reserved meanings
>> and
>> usages, and you have just violated one of the major ones...

>
> The C standard spells out certain name spaces for identifiers that are
> reserved for the implementation in various circumstances. The OP's
> code did not use any of these reserved names. He has violated
> nothing.
>
> Nothing but the C standard specifies reserved identifiers in C.
>


<snip>

http://en.wikipedia.org/wiki/Coding_conventions

or, as an example of a fairly well-known convention system:
http://en.wikipedia.org/wiki/Hungarian_notation

http://www.chris-lott.org/resources/...hill-annot.pdf

other examples:
http://www.textrush.com/coding-standard.htm


they are generally viewed as programmer or self-imposed conventions.
the standards as such, don't care.
neither do compilers.

however, none the less, there are common practices, and one is ill-advised
to so blatently violate them, as the OP has done. to do so, leads to ugly,
unmaintainable, and incomprehensible code.

and, if one reads lots of code by lots of people, one will see that there
are many common, specific, and generally informally enforced conventions.

one violates them at their own risk...



 
Reply With Quote
 
Thad Smith
Guest
Posts: n/a
 
      01-07-2008
Bill Cunningham wrote:
> Wait a minute I think I might've just caught my problem. It's in the
> function body where I'm making the error. Not assigning name[] to anything
> in the body. What about this.
>
> char passw(char name[])
> {char i=name;


As cr88192 noted, i is the wrong type. It should be char* instead of char.

> ....
> if (strcmp(i,name2)==0) {puts"ok");}


Your code doesn't show a definition for name2, but if it is a string, the
comparison is OK. Note that "string.h" must be included.

> else if(strcmp(i,name2)!0) {puts("unequal. Try again"); return
> passw();}


That should be !=0 instead of !0. Also, you don't need to make the
comparison again, since it exactly the opposite of the first one.

I started to show corrections in a rewritten version, but saw a more
fundamental problem. I realized that by "return passw();" you were
attempting to retry the password verification. The call is not passing the
required parameter and is not returning the promised character value.

Why is the function defined to return a char? If you are thinking of the
password, that would be returned in the array whose address is passed. If
the code only exits with a verified password, you don't need any status.
You might, however, have an option for the user to cancel, in which case
you might want the return a status value.

The recursion you have written is a poor way to retry a password entry. It
requires extra resources for recursion and also subjects the code to
crashing if repeated failures are made. An appropriate code structure is
an iterative loop, as shown in the following pseudocode:
do forever {
read password
read second copy
if (match) print "match" and return
else print "reenter"
}

--
Thad
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      01-08-2008
On Mon, 7 Jan 2008 13:36:18 +1000, "cr88192" <(E-Mail Removed)>
wrote in comp.lang.c:

>
> "Jack Klein" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > On Mon, 7 Jan 2008 11:38:31 +1000, "cr88192" <(E-Mail Removed)>
> > wrote in comp.lang.c:
> >
> >>
> >> "Bill Cunningham" <(E-Mail Removed)> wrote in message
> >> news:eSegj.5898$Xo1.2521@trnddc06...
> >> > Wait a minute I think I might've just caught my problem. It's in the
> >> > function body where I'm making the error. Not assigning name[] to
> >> > anything
> >> > in the body. What about this.
> >> >
> >> > char passw(char name[])
> >> > {char i=name;
> >> > ....
> >> > if (strcmp(i,name2)==0) {printf"ok");}
> >> > else if(strcmp(i,name2)!0) {puts("unequal rtry again");
> >> > return
> >> > passw();}
> >> >
> >>
> >> 'char *s=name;', would be ok.
> >>
> >> 'char i=name;' has 2 problems:
> >> 'char i;' is an integer type, not a string;
> >> via common practice/tradition, many names have certain reserved meanings
> >> and
> >> usages, and you have just violated one of the major ones...

> >
> > The C standard spells out certain name spaces for identifiers that are
> > reserved for the implementation in various circumstances. The OP's
> > code did not use any of these reserved names. He has violated
> > nothing.
> >
> > Nothing but the C standard specifies reserved identifiers in C.
> >

>
> <snip>
>
> http://en.wikipedia.org/wiki/Coding_conventions


To about repetition, let's define a short-hand notation. When "says
nothing to support your assertion" appears in the text below, please
expand it in your mind to the phrase "says nothing at all about
single-letter variable names, or about names beginning with certain
letters being in any way associated with specific data types."

This wikipedia article says nothing.

Of the 8 links in the "references" section, one of them mentions C++
and says nothing to support your assertion. Another, which does not
specify language, pulls up an indication from the host site that the
page name is invalid.

It does link to another wikipedia page:

http://en.wikipedia.org/wiki/Naming_...programming%29

....which says nothing on your assertion.

> or, as an example of a fairly well-known convention system:
> http://en.wikipedia.org/wiki/Hungarian_notation


Few things are more discredited than Hungarian notation, even
Microsoft has abandoned it. Article ignored.

> http://www.chris-lott.org/resources/...hill-annot.pdf


This is later, and less obnoxious, than earlier versions of the Indian
Hills guide I remember seeing in the past, but it says nothing to
support your assertion.

> other examples:
> http://www.textrush.com/coding-standard.htm


This page has links to about a dozen or so coding standards, one for C
and many others for C++. It also has an illegal copy of the actual
ISO C++ 1998 and standard, and a copy of a draft of C99 TC2, off-hand
I am not sure of the legality of that.

I looked at three of the linked coding standards, Stroustrup, Stallman
(the only one for C), and Hoff. None of them support your assertion.

> they are generally viewed as programmer or self-imposed conventions.
> the standards as such, don't care.
> neither do compilers.
>
> however, none the less, there are common practices, and one is ill-advised


Common to whom, other than yourself?

> to so blatently violate them, as the OP has done. to do so, leads to ugly,
> unmaintainable, and incomprehensible code.
>
> and, if one reads lots of code by lots of people, one will see that there
> are many common, specific, and generally informally enforced conventions.
>
> one violates them at their own risk...


I have not spent the time, nor will I, tracking down every linked
document that might be relevant from the direct URLs that you
presented. In a modest sample, I found not one such document for
either C or C++ that suggests the use of single-letter identifiers at
all, or suggests that specific single-character identifiers, or
identifiers starting with specific letters, be reserved for specific
types.

So I put it to you:

Can you provide a direct link to one or more documents about coding
standards in C that make the claim that identifiers 'i', 'j', and 'k',
or even identifiers starting with those letters, should be used only
or even primarily for integer variables?

I will not add the requirement for "well known" or "well respected"
sources, because they are subjective and you can't know what sources I
would consider to meet those criteria.

So can you provide any direct link at all to any coding standards
documents for C, from any source, that recommends reserving
identifiers beginning with those three letters for integer type
variables? IN C.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      01-09-2008
On 7 Jan, 01:02, "Bill Cunningham" <(E-Mail Removed)> wrote:
> * * Wait a minute I think I might've just caught my problem. It's in the
> function body where I'm making the error. Not assigning name[] to anything
> in the body. What about this.
>
> char passw(char name[])
> * * * * {char i=name;
> * * * * * *....
> * * * * * * if (strcmp(i,name2)==0) {printf"ok");}
> * * * * * * else if(strcmp(i,name2)!0) {puts("unequal rtry again"); return
> passw();}


please post compilete compilable (if you can) programs
rather than fragments. Many of your "dumb" errors
would have been caught if you'd shown the code to a compiler.

I fiddled with your layout, added includes and a main()

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

char passw (char name[])
{
char i = name;
char name2[] = "nick";

if (strcmp (i, name2) == 0)
{
printf"ok");
}
else if(strcmp (i, name2)!0)
{
puts("unequal rtry again"
);

return passw();
}

int main (void)
{
char c;
char name[] = "nick";

c = passw (name);

return 0;
}

I compiled and and got this little lot:-

G:\tmp\bill.c(6) : warning C4047: 'initializing' : 'char ' differs in
levels of indirection from 'char *'
G:\tmp\bill.c(9) : warning C4047: 'function' : 'const char *' differs
in levels of indirection from 'char '
G:\tmp\bill.c(9) : warning C4024: 'strcmp' : different types for
formal and actual parameter 1
G:\tmp\bill.c(11) : error C2143: syntax error : missing ';' before
'string'
G:\tmp\bill.c(11) : error C2059: syntax error : ')'
G:\tmp\bill.c(13) : warning C4047: 'function' : 'const char *' differs
in levels of indirection from 'char '
G:\tmp\bill.c(13) : warning C4024: 'strcmp' : different types for
formal and actual parameter 1
G:\tmp\bill.c(13) : error C2143: syntax error : missing ')' before '!'
G:\tmp\bill.c(13) : error C2059: syntax error : ')'
G:\tmp\bill.c(14) : error C2143: syntax error : missing ';' before '{'
G:\tmp\bill.c(1 : error C2198: 'passw' : too few actual parameters
G:\tmp\bill.c(21) : error C2143: syntax error : missing ';' before
'type'
G:\tmp\bill.c(24) : error C2143: syntax error : missing ';' before
'type'
G:\tmp\bill.c(26) : error C2065: 'c' : undeclared identifier
Error executing cl.exe.

bill.obj - 9 error(s), 5 warning(s)


correcting the typos gave this. This only has one
syntax error.

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

char passw (char name[])
{
char *i = name;
char name2[] = "nick";

if (strcmp (i, name2) == 0)
{
printf ("ok");
}
else if(strcmp (i, name2) != 0)
{
puts("unequal rtry again");
}

return passw();
}

int main (void)
{
char c;
char name[] = "nick";

c = passw (name);

return 0;
}


**** read your textbook ***



--
Nick Keighley




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Small cameras getting too small? GRL Digital Photography 50 02-03-2006 03:12 AM
Small Square with small red X Peter Coddington Computer Support 4 01-03-2006 06:58 AM
Small inconsistency between string.split and "".split Carlos Ribeiro Python 11 09-17-2004 05:57 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments