473,396 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1776
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
10
by: AlexS | last post by:
Hi, I wonder if anybody can comment if what I see is normal in FW 1.1 and how to avoid this. I have .Net assembly, which creates literally thousands of temporary strings and other objects...
7
by: Sharon | last post by:
Hiya I have a small question, I saw this piece of code somewhere (it's for creating a customized context menu) and I was wondering: Why is it that the STYLE and SCRIPT-tags are broken up into...
12
by: jl_post | last post by:
Dear C++ community, I have a question regarding the size of C++ std::strings. Basically, I compiled the following code under two different compilers: std::string someString = "Hello, world!";...
2
by: BKMiller | last post by:
Hello everyone, I'm just getting started playing around with C++, so please don't laugh too loudly at my crude source code, okay? (o^^o) I'm combining together several introductory exercises...
4
by: =?Utf-8?B?VzFsZDBuZTc0?= | last post by:
When one architects a new project one of the first steps in the decision is to decide on the layers. (In my opinion anyway) One architecture that I have used before is to go solid OO and create...
0
by: NM | last post by:
Hello, I've got a problem inserting binary objects into the postgres database. I have binary objects (e.g. images or smth else) of any size which I want to insert into the database. Funny is it...
2
by: shror | last post by:
I need help in a small problem, I have created a registration form and I want my users not leave the username empty or even enter any number of spaces because sometimes I found that the users...
16
by: scholz.lothar | last post by:
I want to add some extension features to my program and this would require that i bundle a small c compiler with my program. On Unix it seems that tiny-c can do this, but i don't know about windows.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.