small string question

Discussion in 'C Programming' started by Bill Cunningham, Jan 7, 2008.

  1. 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
    Bill Cunningham, Jan 7, 2008
    #1
    1. Advertising

  2. "Bill Cunningham" <> 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
    Tomás Ó hÉilidhe, Jan 7, 2008
    #2
    1. Advertising

  3. "Tomás Ó hÉilidhe" <> 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
    Bill Cunningham, Jan 7, 2008
    #3
  4. 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
    Bill Cunningham, Jan 7, 2008
    #4
  5. Bill Cunningham

    cr88192 Guest

    "Bill Cunningham" <> 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
    >
    >
    cr88192, Jan 7, 2008
    #5
  6. Bill Cunningham

    Jack Klein Guest

    On Mon, 7 Jan 2008 11:38:31 +1000, "cr88192" <>
    wrote in comp.lang.c:

    >
    > "Bill Cunningham" <> 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
    Jack Klein, Jan 7, 2008
    #6
  7. Bill Cunningham

    cr88192 Guest

    "Jack Klein" <> wrote in message
    news:...
    > On Mon, 7 Jan 2008 11:38:31 +1000, "cr88192" <>
    > wrote in comp.lang.c:
    >
    >>
    >> "Bill Cunningham" <> 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/cstyle/indhill-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...
    cr88192, Jan 7, 2008
    #7
  8. Bill Cunningham

    Thad Smith Guest

    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
    Thad Smith, Jan 7, 2008
    #8
  9. Bill Cunningham

    Jack Klein Guest

    On Mon, 7 Jan 2008 13:36:18 +1000, "cr88192" <>
    wrote in comp.lang.c:

    >
    > "Jack Klein" <> wrote in message
    > news:...
    > > On Mon, 7 Jan 2008 11:38:31 +1000, "cr88192" <>
    > > wrote in comp.lang.c:
    > >
    > >>
    > >> "Bill Cunningham" <> 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_conventions_(programming)

    ....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/cstyle/indhill-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
    Jack Klein, Jan 8, 2008
    #9
  10. On 7 Jan, 01:02, "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;
    >            ....
    >             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(18) : 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
    Nick Keighley, Jan 9, 2008
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carlos Ribeiro
    Replies:
    11
    Views:
    695
    Alex Martelli
    Sep 17, 2004
  2. MackS
    Replies:
    6
    Views:
    312
    Steven D'Aprano
    Nov 15, 2005
  3. Replies:
    12
    Views:
    521
    Andre Kostur
    Sep 28, 2005
  4. Mike
    Replies:
    1
    Views:
    100
    Thomas 'PointedEars' Lahn
    Aug 21, 2004
  5. PeterT

    Small string problem

    PeterT, Sep 4, 2003, in forum: Perl Misc
    Replies:
    10
    Views:
    167
    PeterT
    Sep 5, 2003
Loading...

Share This Page