473,996 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scope q

I have taken an extraordinary leap into the modern world by purchasing
webspace. In addition to my private concerns, I would like to make a
part to which others, e.g. my nieces and ex-wife, can ftp. To keep a
hold of the reigns, I shall write a program that changes the password,
and the brains of this prog will be in C:
#def MIN_WORD_LENGTH 9
#def MAX_WORD_LENGTH 15

/* subroutine for random permutation */
void permute(char *, int)

/* main

char p[] = "abcdefghijklmn opqrstuvwxyz";
char q[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ";
char r[] = "0123456789 ";
void swap(char *, char *);

end main */

void permute(char *m , int n)
{ ; }

void swap(char *a, char *b)
{
char c;
c = *a;
*a = *b;
*b = c;
}
/* end pseudosource */
I'm going to build a word from p, q and r. At this point, I have 2
questions:
1) Does the scope look right? I've got the subroutine at file scope and
the swap at block scope. If I make a call to swap from within permute(),
is every ISO compiler going to know what I'm talking about?
2) Is it foolhardy of me to think that I can permute a string given its
pointer and length, without needing any information back, hence the void
declaration? frank
-------
May 29 '06 #1
6 1767
On Mon, 29 May 2006 19:53:15 -0400, Frank Silvermann
<in*****@invali d.net> wrote:
I have taken an extraordinary leap into the modern world by purchasing
webspace. In addition to my private concerns, I would like to make a
part to which others, e.g. my nieces and ex-wife, can ftp. To keep a
hold of the reigns, I shall write a program that changes the password,
and the brains of this prog will be in C:
#def MIN_WORD_LENGTH 9
#def MAX_WORD_LENGTH 15

/* subroutine for random permutation */
void permute(char *, int)

/* main

char p[] = "abcdefghijklmn opqrstuvwxyz";
char q[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ";
char r[] = "0123456789 ";
void swap(char *, char *);

end main */

void permute(char *m , int n)
{ ; }

void swap(char *a, char *b)
{
char c;
c = *a;
*a = *b;
*b = c;
}
/* end pseudosource */
I'm going to build a word from p, q and r. At this point, I have 2
questions:
1) Does the scope look right? I've got the subroutine at file scope and
the swap at block scope. If I make a call to swap from within permute(),
is every ISO compiler going to know what I'm talking about?
All your subroutines are at file scope. You don't have a choice about
this since C does not allow nested functions.

If the prototype for swap() is really inside the body of main(), then
when you call it inside permute(), the compiler will not know what
types of arguments it takes or what kind of value it returns. In C99
a diagnostic is required. In C89, the compiler must assume it returns
an int, which is incorrect in this case. Many compilers will also
generate an discretionary diagnostic about the missing prototype. If
you move the code for swap() before the code for permute(), the
problem goes away. The oft recommended approach here is to put your
prototypes at file scope; it's just cleaner all around.
2) Is it foolhardy of me to think that I can permute a string given its
pointer and length, without needing any information back, hence the void
declaration? frank


Since permute() receives a pointer to the string (as opposed to the
string itself), it can update the string in place. You don't need to
pass the length (though it is more efficient in my opinion) since
permute could figure it out (e.g., strlen). Whether or not your
function can generate a permutation with no other knowledge is an
algorithm question. I expect the answer can be found in Knuth's
masterpiece.
Remove del for email
May 30 '06 #2
Frank Silvermann said:
At this point, I have 2 questions:
1) Does the scope look right? I've got the subroutine at file scope and
the swap at block scope.
If you want a function for swap() - which is by no means necessary - it must
be at file scope, since C does not support the nesting of functions.
If I make a call to swap from within permute(),
is every ISO compiler going to know what I'm talking about?
C supports function-call syntax, yes.
2) Is it foolhardy of me to think that I can permute a string given its
pointer and length, without needing any information back, hence the void
declaration?


No, if you just mean a random permutation:

for J = 0 to (N - 1)
R = Pseudo-random number in range J to (N - 1)
swap S[J], S[R]
next
all done

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 30 '06 #3
Barry Schwarz wrote:
On Mon, 29 May 2006 19:53:15 -0400, Frank Silvermann
<in*****@invali d.net> wrote:
I have taken an extraordinary leap into the modern world by purchasing
webspace. In addition to my private concerns, I would like to make a
part to which others, e.g. my nieces and ex-wife, can ftp. To keep a
hold of the reigns, I shall write a program that changes the password,
and the brains of this prog will be in C:
#def MIN_WORD_LENGTH 9
#def MAX_WORD_LENGTH 15

/* subroutine for random permutation */
void permute(char *, int)

/* main

char p[] = "abcdefghijklmn opqrstuvwxyz";
char q[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ";
char r[] = "0123456789 ";
void swap(char *, char *);

end main */

void permute(char *m , int n)
{ ; }

void swap(char *a, char *b)
{
char c;
c = *a;
*a = *b;
*b = c;
}
/* end pseudosource */
I'm going to build a word from p, q and r. At this point, I have 2
questions:
1) Does the scope look right? I've got the subroutine at file scope and
the swap at block scope. If I make a call to swap from within permute(),
is every ISO compiler going to know what I'm talking about?
All your subroutines are at file scope. You don't have a choice about
this since C does not allow nested functions.

I'm not understanding something here.
If the prototype for swap() is really inside the body of main(), then
when you call it inside permute(), the compiler will not know what
types of arguments it takes or what kind of value it returns. In C99
a diagnostic is required. In C89, the compiler must assume it returns
an int, which is incorrect in this case. Many compilers will also
generate an discretionary diagnostic about the missing prototype. If
you move the code for swap() before the code for permute(), the
problem goes away. The oft recommended approach here is to put your
prototypes at file scope; it's just cleaner all around.

I'll be fine with declaring at file scope. Why would one, given a
possible confrontation with something that sounds like 'agnostic', do
otherwise?
2) Is it foolhardy of me to think that I can permute a string given its
pointer and length, without needing any information back, hence the void
declaration? frank


Since permute() receives a pointer to the string (as opposed to the
string itself), it can update the string in place. You don't need to
pass the length (though it is more efficient in my opinion) since
permute could figure it out (e.g., strlen). Whether or not your
function can generate a permutation with no other knowledge is an
algorithm question. I expect the answer can be found in Knuth's
masterpiece.

I need LESS than I thought? (Rhetorical question). Since I'll want
cases to be equiprobable, the length will be known in main before it
calls the subroutine. Seems like cheating. frank
May 30 '06 #4
On Tue, 30 May 2006 03:31:42 -0400, Frank Silvermann
<in*****@invali d.net> wrote:
Barry Schwarz wrote:
On Mon, 29 May 2006 19:53:15 -0400, Frank Silvermann
<in*****@invali d.net> wrote:
I have taken an extraordinary leap into the modern world by purchasing
webspace. In addition to my private concerns, I would like to make a
part to which others, e.g. my nieces and ex-wife, can ftp. To keep a
hold of the reigns, I shall write a program that changes the password,
and the brains of this prog will be in C:
#def MIN_WORD_LENGTH 9
#def MAX_WORD_LENGTH 15

/* subroutine for random permutation */
void permute(char *, int)

/* main

char p[] = "abcdefghijklmn opqrstuvwxyz";
char q[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ";
char r[] = "0123456789 ";
void swap(char *, char *);

end main */

void permute(char *m , int n)
{ ; }

void swap(char *a, char *b)
{
char c;
c = *a;
*a = *b;
*b = c;
}
/* end pseudosource */
I'm going to build a word from p, q and r. At this point, I have 2
questions:
1) Does the scope look right? I've got the subroutine at file scope and
the swap at block scope. If I make a call to swap from within permute(),
is every ISO compiler going to know what I'm talking about?
All your subroutines are at file scope. You don't have a choice about
this since C does not allow nested functions.

I'm not understanding something here.


You said swap was at block scope (5 lines up). The function swap is
not at block scope. It is at file scope. All functions are at file
scope.
If the prototype for swap() is really inside the body of main(), then
when you call it inside permute(), the compiler will not know what
types of arguments it takes or what kind of value it returns. In C99
a diagnostic is required. In C89, the compiler must assume it returns
an int, which is incorrect in this case. Many compilers will also
generate an discretionary diagnostic about the missing prototype. If
you move the code for swap() before the code for permute(), the
problem goes away. The oft recommended approach here is to put your
prototypes at file scope; it's just cleaner all around.

I'll be fine with declaring at file scope. Why would one, given a
possible confrontation with something that sounds like 'agnostic', do
otherwise?
2) Is it foolhardy of me to think that I can permute a string given its
pointer and length, without needing any information back, hence the void
declaration? frank


Since permute() receives a pointer to the string (as opposed to the
string itself), it can update the string in place. You don't need to
pass the length (though it is more efficient in my opinion) since
permute could figure it out (e.g., strlen). Whether or not your
function can generate a permutation with no other knowledge is an
algorithm question. I expect the answer can be found in Knuth's
masterpiece.

I need LESS than I thought? (Rhetorical question). Since I'll want
cases to be equiprobable, the length will be known in main before it
calls the subroutine. Seems like cheating. frank


But will it be known in permute?
Remove del for email
May 31 '06 #5

"Richard Heathfield"
Frank Silvermann said:

At this point, I have 2 questions:
1) Does the scope look right? I've got the subroutine at file scope and
the swap at block scope.


If you want a function for swap() - which is by no means necessary - it
must
be at file scope, since C does not support the nesting of functions.

I'd be embarrassed to reveal how many times I've made this mistake. There
is something down there that is really puzzling me, but maybe I need to be
emphatic: hey, dummy, declare functions at file scope.
If I make a call to swap from within permute(),
is every ISO compiler going to know what I'm talking about?


C supports function-call syntax, yes

That is, when properly put at file scope.

2) Is it foolhardy of me to think that I can permute a string given its
pointer and length, without needing any information back, hence the void
declaration?


No, if you just mean a random permutation:

for J = 0 to (N - 1)
R = Pseudo-random number in range J to (N - 1)
swap S[J], S[R]
next
all done


I have to bug out as OP on this, as not only have my newsreaders gone
Indian, so have my Indians. C dreams. joe
Jun 1 '06 #6
"Joe Smith" <gr**********@n etzero.net> writes:
"Richard Heathfield"

[...]
If you want a function for swap() - which is by no means necessary - it
must
be at file scope, since C does not support the nesting of functions.

I'd be embarrassed to reveal how many times I've made this mistake. There
is something down there that is really puzzling me, but maybe I need to be
emphatic: hey, dummy, declare functions at file scope.


To be precise, function *declarations* at block scope are perfectly
legal (though many would argue that they're bad style). Function
*definitions* may only appear at file scope.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 1 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6485
by: Anonymous | last post by:
Is namespace the same thing as scope? While reading the book "Thinking in C++", I was under the impression that namespace is, well, a namespace--a feature to create a hiearchy for identifiers within the Global Namespace. And that, all identifiers within a given namespace, however deeply nested they are, each of them has the Global Scope. So, if namespace outer { int a = 1;
6
3174
by: pembed2003 | last post by:
Hi all, I am reading the book "C++ How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope function-prototype scope I think(might be wrong):
5
2106
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope function-prototype scope I think(might be wrong):
8
3405
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an actual program. To me "scope" of the name of a function or object are the general rules for the areas of a program that can through a declaration, have "visibility."
3
3444
by: marco_segurini | last post by:
Hi, I am using VS 2005. If I compile the following code only line 6 returns me an error while line 9 returns a warning. If I comment the line 6 and debug the program the assignments of lines {9, 11} work fine. Why the '.' "scope operator" is accepted in line 9 and not in line 6?
39
3238
by: utab | last post by:
Dear all, Is there a clear distinction how to decide which functions to be members of a class and which not How is your attitude (Your general way from your experiences ...) "If the function changes the state of the object, it ought to be a member of that object." Reference Accelerated C++, A. Koenig, page 159.
7
2214
by: Christian Christmann | last post by:
Hi, I've a a question on the specifier extern. Code example: void func( void ) { extern int e; //...
1
2593
by: Steven T. Hatton | last post by:
All of the following terms are used in some way to describe where and how a name is relevant to a particular location in a program: visible, declarative region, scope, potential scope, valid, introduced, used, potentially evaluated and accessible. They all seem to have subtle difference in meanings. Any positive contribution to the following will be appreciated. Here is my attempt to make sense of these:
1
25734
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that variable can only be used in that function. If you were to try to access that variable anywhere else in...
1
3772
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: - "function prototype scope" for structures and unions? - "extern" for internal linkage ?
0
10424
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
10239
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11941
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
11504
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
10172
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...
1
8563
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6513
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
5268
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
3
3853
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.