469,568 Members | 1,518 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

small string question

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
Jan 7 '08 #1
6 1573
"Bill Cunningham" <no****@nspam.comwrote 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
Jan 7 '08 #2

"Tomás Ó hÉilidhe" <to*@lavabit.comwrote in message
news:Xn**************************@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
Jan 7 '08 #3
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
Jan 7 '08 #4

"Bill Cunningham" <no****@nspam.comwrote 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


Jan 7 '08 #5
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
Jan 7 '08 #6
On 7 Jan, 01:02, "Bill Cunningham" <nos...@nspam.comwrote:
* * 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


Jan 9 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Carlos Ribeiro | last post: by
10 posts views Thread by AlexS | last post: by
7 posts views Thread by Sharon | last post: by
12 posts views Thread by jl_post | last post: by
2 posts views Thread by BKMiller | last post: by
4 posts views Thread by =?Utf-8?B?VzFsZDBuZTc0?= | last post: by
2 posts views Thread by shror | last post: by
16 posts views Thread by scholz.lothar | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.