Conversation started Dec 9, 2014 at 0:37.
200_success
Dec 9, 2014 00:37
Hi jargonjunkie. Let's break this down a bit.
Suppose we want to always uppercase the text. How could you write display()?
void display(char *string)
{
 for (char *s = string; *s; s++)
 {
 *s = toupper(*s);
 }
 puts(string);
}
Would that work? Any questions about that?
Alternatively...
void display(char *string)
{
 for (int i = 0; string[i]; i++)
 {
 string[i] = toupper(string[i]);
 }
 puts(string);
}
Would you be more comfortable with that?
Phrancis
Dec 9, 2014 01:27
Just chimed in with a friend on Skype... Found out he's an SE junky, on the Unix site. Small world!
Captain Obvious
Dec 9, 2014 02:19
0

prestokeysConsider the following: #include <iostream> struct State { virtual ~State() = default; }; struct Drunk : State { void singWhileDrunk() {std::cout << "Singing while drunk.\n";} }; struct Person { State* state; void singWhileDrunk() {dynamic_cast<Drunk*>(state)->singWhileDrunk();} ...

0

ChandlerI found this site when I searched for a Java RPG to use as an outline since I'm a total beginner in my Computer Science class. I copied and pasted some code that was posted here around two years ago, but I couldn't compile it on BlueJ. Google helped me a lot since I found out how to import stuff...

0

GurmanJust for review and may be useful for somebody from os import walk from time import time from abc import ABCMeta, abstractmethod class Reporter(object): __metaclass__=ABCMeta @abstractmethod def report(self, catalogs_cntr, files_ctnr, speed): """ output catalogs_cntr, file...

Phrancis
Dec 9, 2014 02:41
replied to a message: View original message
@CaptainObvious Is it just me or that question is pretty mediocre?
jargonjunkie
replied to a message: View original message
its not so much the pointer notation over array notation thats the hiccup for me. its more the mystery behind the magic. i know that, for instance...

void display(char *string, int (*transform)(int))
{
for (char *s = string; *s; s++)
{
*s = transform(*s);
}
puts(string);
}
200_success
Do you follow the hard-coded toupper() samples above?
jargonjunkie
replied to a message: View original message
so i could easily translate that too

void display(char * string, int (* transform)(int value))

so i know that transform is a function that is a pointer-to-int and it takes an argument of int value., but what is transform doing? it hasn't been declared until that point and is private to the functions parameter scope. i'm not one for the "black box theory". i like to know what's in the box.
replied to a message: View original message
@200_success ya. lol i feel stupid right now. i can see that toupper, but it somehow fulfills what toupper and tolower is doing all at once.
200_success
Before I discuss function pointers, could you confirm whether you are completely comfortable with this use of toupper() first?
void display(char *string)
{
 for (int i = 0; string[i]; i++)
 {
 string[i] = toupper(string[i]);
 }
 puts(string);
}
jargonjunkie
replied to a message: View original message
yes, i am more comfortable with array notation, but i don't mind the pointer notation. that way i can get better at interpreting it. i know that
string[i] == *(string + i) == string == &string[i]
are all supposed to be synonymous. which is why you can say that
string[i] = toupper(string[i]); == *s = transform(*s) || *s = toupper(*s++)
assuming were using a while() loop for instance
while (*string)
200_success
Dec 9, 2014 02:52
So you are convinced that my last sample prints an uppercase version of whatever string you pass in?
Captain Obvious
0

ChandlerI may seem like an idiot for asking, but here's something I made on BlueJ that I need help with: /** * class Kirby will draw a Kirby character * I'm disclaiming now if that's an issue * * @version WhenChandlerThoughtItWouldBeMoreEfficientToUseMicrosoftPaintForThis */ //At the bottom of t...

jargonjunkie
replied to a message: View original message
@200_success i feel like a horse being led to water and i just don't realize that im staring at the water im supposed to drink already lol. but im going to answer i don't know because i really don't. im not sure what you're trying to ask. but if youre asking if im convinced, the answer would have to be no. but then, i haven't applied your theory to the application yet...hence the reason for my doubt. to be honest, this is dubious evidence IMO at best.
200_success
That's fine. I want to make sure we're on the same page before progressing.
How about string[0] = toupper(string[0]) — can you tell me what that does?
jargonjunkie
replied to a message: View original message
ok, ill play along.

string is the address of where the data is stored.
[0] is the dereferenced value, or where the first element is stored at the given address.

string[0] is assigned the value that toupper returns which is an assumed char, or int value that represents a char value. string[0] is the char that is processed by toupper and toupper returns an "uppercase" value, which is really...i see now lol.
200_success
So it just overwrites the first character of the string with the uppercase version of itself. Right?
jargonjunkie
Dec 9, 2014 03:05
replied to a message: View original message
@200_success yea, but how does transform know whether to do either or if we just declared it?
200_success
Let's defer that discussion a bit, if you don't mind.
jargonjunkie
replied to a message: View original message
@200_success ok. let's keep going. im curious.
200_success
string[1] = toupper(string[1]) does what?
jargonjunkie
replied to a message: View original message
string[0] = toupper(string[0]) //first char
string[1] = toupper(string[1]) //second char
string[2] = toupper(string[2]) //third char
and so on...
200_success
Good. How can we uppercase the whole string?
jargonjunkie
Dec 9, 2014 03:08
replied to a message: View original message
@200_success all replacing themselves with their respective "modified" chars
200_success
for (int i = 0; string[i]; i++)
{
 string[i] = toupper(string[i]);
}
OK with that?
Captain Obvious
0

hedfun2I am new to encryption and recently wanted to learn more about it. I researched how to do AES Encryption in java and assembled my own java AES encrpytion class. I don't know if there are any major security holes though. Can someone experienced in java encryption look at my code and tell me if the...

jargonjunkie
replied to a message: View original message
@200_success yea, for(;;) or while() would work.
200_success
As long as you somehow stop when you encounter the NUL terminator.
So, how would you write a loop that lowercases the entire string?
jargonjunkie
'0円' NULL and 0 all different representations all amounting to the "same value".
200_success
Dec 9, 2014 03:10
Yes, that's right.
jargonjunkie
replied to a message: View original message
@200_success just replace toupper with tolower
replied to a message: View original message
@200_success that's why you used the switch...
200_success
I should be a bit more careful in saying that they are all the same. '0円' is used to represent ASCII value 0, also known as the NUL character.
NULL is generally used to mean a pointer with a zero value.
... which points to "nowhere".
jargonjunkie
replied to a message: View original message
@200_success i know what you meant. it's okay. and zero is a literal value or representation.
200_success
0 is just a generic zero.
If you mix them up, the code will "work", but programmers reading your code will get confused.
jargonjunkie
or literal constant to be exact.
200_success
Dec 9, 2014 03:13
Anyway, strings are terminated by a '0円' marker.
jargonjunkie
ok. that makes sense
200_success
And what does this code do...?
for (int i = 0; string[i]; i++)
{
 string[i] = passthrough(string[i]);
}
jargonjunkie
each char representing one byte, the difference between an array and an string literal is that the string literal holds the null character.
so if we had the string "hello, world"
and broke it down, it would be an array of chars with a terminating null char '0円'
i is in the scope of the for loop and only exists until the end of the block.

for i is initialized to 0 while string char is not null increment i postfix
assign string[i] the value of passthrough(string[i]);

which basically checks to see if the passed value is an int and if it is it returns an int which is passed to string[i].
or am i wrong?
200_success
That's all true... but what actually happens, in plain English?
I'm referring to this passthrough() function, by the way...
int passthrough(int c) {
 return c;
}
jargonjunkie
the loop scans the string char by char until it reaches the null character. while it traverses the string character by character it passes the value back and forth from passthrough to string.

while each value is passed over to pass through, it needs to be of type int. the data object being passed to the function. im assuming, or inferring, that since only an int value can be passed (thats part of the test, or condition) and if it does so, it can return the integer based value to the calling function or object.
200_success
Dec 9, 2014 03:23
OK, except that there is no real testing going on. It just does it.
Basically, it goes through the string, overwriting each character with itself.
Or more simply, it does "nothing".
jargonjunkie
and thats all it does.
so what's the point?
200_success
So, let's consider the three loops...
First version:
for (int i = 0; string[i]; i++)
{
 string[i] = toupper(string[i]);
}
Second version:
for (int i = 0; string[i]; i++)
{
 string[i] = tolower(string[i]);
}
Third version:
for (int i = 0; string[i]; i++)
{
 string[i] = passthrough(string[i]);
}
They are all the same, except for the toupper vs. tolower vs. passthrough.
So the challenge is, can we write one loop that covers all three cases?
jargonjunkie
replied to a message: View original message
@200_success i would assume so.
200_success
The only purpose of the passthrough function is to make all three cases look similar, so that we don't need to write a special case for your -p flag.
So, what we want is
for (int i = 0; string[i]; i++)
{
 string[i] = do_the_right_thing(string[i]);
}
Or, with more elegant naming,
for (int i = 0; string[i]; i++)
{
 string[i] = transform(string[i]);
}
The next challenge is, how can the caller specify what the "right thing" is?
jargonjunkie
200_success
Dec 9, 2014 03:30
Sometimes, the "right thing" is toupper. Sometimes it's tolower. Sometimes it's passthrough.
If you read the documentation for toupper() and tolower(), you'll see that they both accept an int and return an int.
jargonjunkie
ya, int toupper(int c)
not a single pointer in sight
not even a char
200_success
Therefore, we have to design passthrough to also accept an int and return an int.
So, we have to tell the compiler that transform is some kind of function that accepts an int and that returns an int.
jargonjunkie
and hopefully it will complain if not so.
200_success
That's what int (*transform)(int) says. The variable transform represents a function that accepts an int and that returns an int when called.
With that knowledge, let's look again at
void display(char *string, int (*transform)(int))
{
 for (char *s = string; *s; s++)
 {
 *s = transform(*s);
 }
 puts(string);
}
The loop I wrote is exactly equivalent to the ones I have been showing you just now.
Captain Obvious
0

CodeYogiimport re def replace_token_regex(s, token=" "): return re.sub(token, '20%', s.strip()) def replace_token_inplace(s, token=" "): for index, char in enumerate(s): if ord(char) == ord(token): s[index] = '20%' return s print replace_spaces_regex("Foo Bar ") s = l...

200_success
Dec 9, 2014 03:36
It just happens to use *s instead of string[i]. Don't let that bother you. I assure you that they are the same.
So, display is a function that accepts a string and some transformation, where the transformation is one that maps an int to an int. It performs the transformation, then prints it.
Take a minute to convince yourself or raise any objections.
jargonjunkie
replied to a message: View original message
@200_success i believe you. it doesnt bother me at all. the part that gets me is that transform is dereferencing itself. why not declare it as int * transform(int)?
is there a major differnce between (*transform) and * transform?
200_success
The parentheses around (*transform) are necessary to get the precedence right.
jargonjunkie
because parentheses is higher than the asterick.
so the function really isn't a pointer to a int, its a dereferenced value because you're supplying a referenced value?
200_success
int *transform(int) would be interpreted as int *(transform(int)), which doesn't make sense.
transform is not a pointer to an int. It is a pointer to a function. The function accepts an int and returns an int.
jargonjunkie
replied to a message: View original message
@200_success ok. i am following. i just wanted to clarify before asking my next question to make sure i am following what you're saying.
switch (option(argv[i]))
 {
 case '-':
 help(stderr, argv[0]);
 return 1;
 case 'h':
 help(stdout, argv[0]);
 return 0;
 case 'p':
 transform = passthrough;
 break;
 case 'u':
 transform = toupper;
 break;
 case 'l':
 transform = tolower;
 break;
 default:
 string = argv[i];
replied to a message
here you state that transform = passthrough;
and transform = toupper;
i had expected some arguments such as var = transform(value);
but it seems like youre somehow assigning values to these functions...maybe this is somehow out of the scope of what i've covered, i am missing a few key chapters from my book.
200_success
Dec 9, 2014 03:45
That would be calling the function and actually performing the transformation. We don't want to call it in main. We want that loop we were talking about to call it.
In main, we just want to specify what kind of transformation we want.
So that we can tell display() to use the right transformation.
jargonjunkie
so what does transform = toupper; actually do?
it just tells transform what to do and how?
in this case, set char to uppercase.
200_success
Technically, it sets transform to point to the chunk of code that represents the toupper function.
jargonjunkie
because transform just takes these values and replaces its "old values" with its "new values" per se. so thats a how a pointer based function works, eh?
200_success
Logically, you can think of it as saying "I think we will want to use toupper as the transformation later."
jargonjunkie
ok
thats the part that i was having a hard time with.
i was like wait, wth is going on here?
im starting to get it now though.
rolfl
Dec 9, 2014 03:51
0

rolflI have coded up in SEDE what I believe to be a mostly accurate representation of the post edits that count toward Archaeologist. It identifies all your edits, and then discards those that were active within 6 months prior to the edit. It uses the following strategy to determine the last activity...

200_success
Maybe it would be easier to understand if we didn't assign the function pointer first. Try playing with a very simple main, like...
int main(int argc, char *argv[]) {
 char *string = argv[1];
 display(string, toupper);
}
jargonjunkie
well, let me ask a question real quick.

could

int * function(int value) be considered the same as
int function(char * string, int (*function)(int value))
200_success
In the latter part, you used function twice — do you mean two different functions?
jargonjunkie
yes,

to better state

int outside(char * string, int (*inside)(int))
is int(*inside)(int) == int * function(int)?
200_success
And in the former part, do you mean int (*function)(int value)?
jargonjunkie
Dec 9, 2014 03:55
yes
200_success
Remember, you need the parentheses to get the right precedence.
jargonjunkie
alright, lets go back for a sec then, so i can better understand
lets say these are both functions
function one and function two respectively
int * function_one(int)
int (*function_two)(int)
both are functions, but the important part here is the precedence
one says im returning a pointer to an int
and the other says what again?
200_success
I'm not sure that the function_one example even makes sense.
jargonjunkie
well let's say its an actual function
because thats how we can get a return null value or pass an address of a character in a string. it usually represents a return value, right? generally speaking of course.
int * function_one (int * string)
{
if (*string == '0円')
return null
else
return *string
}
200_success
I just tried compiling int *function_one(int) = NULL; and got an error.
On the other hand, int (*function_two)(int) = NULL; works.
Oh, I see.
function_one is a function that accepts a pointer to an integer, and that returns a pointer to an integer.
It is, however, not a pointer to a function.
jargonjunkie
Dec 9, 2014 04:04
exactly
#include <stdio.h>
#include <stdlib.h>
char * function_one(char * string)
{
if (*string == '0円')
return NULL;
else
return string;
}
int main(void)
{

return 0;
}
this does compile
200_success
Yes, I think that compiles.
jargonjunkie
i just checked lol
needed to make sure i wasnt crazy
sanity check and all
now back to my question
200_success
Go ahead.
jargonjunkie
as i just stated, function_one represents a return value a pointer-to-int or in this case a pointer-to-char for example.

function_two
this is what you said.
That's what int (*transform)(int) says. The variable transform represents a function that accepts an int and that returns an int when called.
With that knowledge, let's look again at
void display(char *string, int (*transform)(int))
{
for (char *s = string; *s; s++)
{
*s = transform(*s);
}
puts(string);
}
lets say that function_two's equivalent was this function pointer
int (*transform)(int)
what does that mean exactly?
200_success
So function_two and transform have the same type, and are "compatible" with each other.
Captain Obvious
Dec 9, 2014 04:09
0

ajkumar25my task is to generate random strings and store in a file till the size of file is less than 10MB. My approach towards this problem is as follows. import java.io.File; import java.io.FileWriter; public class Application { public static void main(String[] args) throws Exception { lon...

jargonjunkie
yes. and to reiterate, this is what my question really is, and i really am just copy and pasting what i said before.
--------------------------------
alright, lets go back for a sec then, so i can better understand
lets say these are both functions
function one and function two respectively
int * function_one(int)
int (*function_two)(int)
both are functions, but the important part here is the precedence
one says im returning a pointer to an int
and the other says what again?
what is function_two? what is it doing? and how is it differnt from function one aside from the obvious.
200_success
function_one is actually a function (a function that accepts an int and that returns a pointer to an int). function_two is not actually a function, but rather a pointer to a function — one that accepts an int and returns an int.
jargonjunkie
ok.
finally.
i understand lol
200_success
Yeah, sometimes it's tricky to read a C declaration. Anyway...
as I was saying...
int main(int argc, char *argv[]) {
 char *string = argv[1];
 display(string, passthrough);
 display(string, toupper);
 display(string, tolower);
}
Do you feel OK about that?
jargonjunkie
yes
200_success
Dec 9, 2014 04:15
int main(int argc, char *argv[]) {
 char *string = argv[1];
 int (*function_ptr)(int);
 function_ptr = toupper;
 display(string, function_ptr);
}
How about that?
Captain Obvious
0

gabrielfreibergI have an angular app using a restful api which works, but I know is structured poorly. I'm using a projectService (factory) to do all interfacing with the REST back-end, for both of the backend Project and Task models. The ProjectController and TaskController both get the projectService (fact...

jargonjunkie
what you're saying when you declare something as
transform = toupper
since transform is not a function and it is a pointer to a function, you can point to an already existing function such as toupper, or tolower, or to transform...

were really saying, transform is pointing to an existing function that can do this behavior for us already, so let toupper do all the work. toupper is passed the value by using transform which is also a pointer. so we can arbitrarily use one function or task over the other depending on the needs of the current action.
200_success
Exactly!
jargonjunkie
i told you i understood haha
all thanks to you man?
yes!!! all thanks to you!!!
i didnt realize how clever the code really was.
its cleaner, easier to maintain, less to read, and to write.
200_success
Yes. It is more elegant than passing special characters to display(), then having display() interpret them with some kind of switch or if-elseif-elseif.
The tricky part is understanding that you can pass a pointer to a function, and figuring out how to actually write the pointer declaration.
jargonjunkie
Dec 9, 2014 04:22
ya, i can see that.
i just want to say thank you for your time, help, and dedication. its refreshing to work with someone like you, esp since i do most of this on my own. most people just brush me off, mock me, or refuse to help. lately, i've been using stack exchange and i've had much better results.
200_success
I suggest that you experiment with some of the stripped-down versions of main() to get the hang of the concept.
jargonjunkie
i definitely will.
200_success
Well, I already took the time to write an answer. It would be a shame if it just flew over your head. =)
jargonjunkie
haha
gonna have to mark that one as an answer ;)
200_success
There are some languages, like Python or LISP, where passing code around is common and natural. In C, it's a bit unusual. But it's a tool in your toolkit that comes in handy for situations like this.
jargonjunkie
Dec 9, 2014 04:25
yes indeed. i was looking at both those languages, but im really edging over towards Python so i can focus more on my Maths again and move towards my Intro to Algorithms book.
i just wanna solidify my knowledge with C first which is why im taking my time with it. its my real first language.
200_success
Python is a nice language. I recommend it. C is also good to learn so that you get to understand what goes on at a low level.
Anyway, good night, and I look forward to your next question on CR.
jargonjunkie
that's why i chose C. my natural need to know led me to C. ive had experience with JavaScript and PHP in the past, but C is a totally different tool. and good night to you as well. look forward to some answers in the future.
later, it was a pleasure.
Conversation ended Dec 9, 2014 at 4:28.

AltStyle によって変換されたページ (->オリジナル) /