473,803 Members | 3,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1798
"Bill Cunningham" <no****@nspam.c omwrote 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 "initialise r" 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.co mwrote 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,nam e2)!0) {puts("unequal rtry again"); return
passw();}

Bill
Jan 7 '08 #4

"Bill Cunningham" <no****@nspam.c omwrote in message
news:eSegj.5898 $Xo1.2521@trndd c06...
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,nam e2)!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,nam e2)!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.c omwrote:
* * 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,nam e2)!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(1 1) : error C2143: syntax error : missing ';' before
'string'
G:\tmp\bill.c(1 1) : error C2059: syntax error : ')'
G:\tmp\bill.c(1 3) : warning C4047: 'function' : 'const char *' differs
in levels of indirection from 'char '
G:\tmp\bill.c(1 3) : warning C4024: 'strcmp' : different types for
formal and actual parameter 1
G:\tmp\bill.c(1 3) : error C2143: syntax error : missing ')' before '!'
G:\tmp\bill.c(1 3) : error C2059: syntax error : ')'
G:\tmp\bill.c(1 4) : error C2143: syntax error : missing ';' before '{'
G:\tmp\bill.c(1 8) : error C2198: 'passw' : too few actual parameters
G:\tmp\bill.c(2 1) : error C2143: syntax error : missing ';' before
'type'
G:\tmp\bill.c(2 4) : error C2143: syntax error : missing ';' before
'type'
G:\tmp\bill.c(2 6) : 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
2512
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 strings. I wonder if someone else had ever stumbled on it before, and if it has a good reason to work like it is. Both implementations take two parameters: the separator character and the max number of splits (maxsplit). However, string.split() accept
10
1786
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 when running. Usually it is something like { string s=some value; some local processing here
7
1267
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 parts? I hope someone can answer my question, thanks! Sharon html+='<TABLE STYLE="border:1pt solid #808080" BGCOLOR="#CCCCCC" WIDTH="140" HEIGHT="220" CELLPADDING="0" CELLSPACING="1">'; html+='<ST'+'YLE TYPE="text/css">\n'; html+='a:link...
12
13592
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!"; int size1 = sizeof(std::string); int size2 = sizeof(someString); and printed out the values of size1 and size2. size1 and size2 always
2
2061
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 into one small program. My compiler and IDE is Visual C++ Express Edition, and the book I'm using is Teach Yourself C++ in 24 hours by Jesse Liberty and David Horvath. While playing around with the code a little bit I found something I couldn't...
4
2049
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 objects, which normally are very small and only deals with the stuff pertaining to that object, then break it down into Business Process, Process Controllers and Data Access Objects for each "Object", each of which is created in it's very own .Net...
0
2707
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 works for files larger than 8000 Bytes. If a file is less than 1000 Bytes I get the following message: Error message: --invalid input syntax for type oid: "\074\077......";
2
1283
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 enter some space hits in the username and the form is submitted, so I created an array and tried to enter the spaces but I don't know how to make my code reject any spaces whatever their number and I failed doing this, and what I did was that I...
16
3369
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
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10316
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.