jephthah 1,888 Posting Maven

narue has already given you the answer.

i'm not telling you anything she hasn't already. but maybe i can break it down for you into easy-to-chew bites.


1 -- create a new array.
2 -- start indexing existing array at the first element
3 -- if the existing element does not exist within the new array,
-- 3a -- then copy the existing element to the new array
4 -- move to the next existing array element and repeat at (3)


there are other ways to do it, but it just does not get any more simple than this.


.

jephthah 1,888 Posting Maven

and you will, of course, need to change the prototype of your function to match....

jephthah 1,888 Posting Maven

i dont believe you can make pointers to structure elements like that. you should pass a pointer to the entire structure into the function:

pthread_cond_wait(&key);

then in the function, you can modify/access the elements like this

(*key).mutex = whatever;

or using the shorthand method (IMO, the preferred method)

key->mutex = whatever;

.

jephthah 1,888 Posting Maven

^ ah, yeah, i just found that, too. R.STILTSKIN is right, i believe.

you are missing the header file

#include <errno.h>

so... let me (re)learn the lesson: bugs are in user code, not compilers.
[/carlfacepalm]


.

jephthah 1,888 Posting Maven

this particular EBUSY error was the subject of some bug fixes in GCC a few years ago. are you using an older version of GCC that might still have this bug? seems unlikely, but....

anyhow, not trying to blow you off, but since this is not standard C and it may be compiler related, you might get a quicker and more detailed response if you also try asking at GCC's forums.

jephthah 1,888 Posting Maven

your problems are so widespread and fundamental, it's not reasonable for me to even try and debug it. this is not a simple fix. to be blunt, you are attempting to take on too complex of a project (for your skill level) at one time.

theres no way to help you at this point, short of rewriting your code and handing it back to you, which would be a lot of effort for us, and no learning opportunity for you.

redefine the scope of your project, and break it into little pieces. do one thing at one time. get that one thing to work right. then as you understand what you are doing, you can expand from there to incorporate more.

you cant just jump into a project with 100 moving parts, and have no clue how many are broken at any given time. narrow your focus down to working on one piece at at time. this forum will help you as you have specific questions.

jephthah 1,888 Posting Maven

well.... maybe it's a pedantic exercise assigned by his professor?

jephthah 1,888 Posting Maven

your function is very nice, but i have to ask, what's wrong with

memset(string,0,21);
jephthah 1,888 Posting Maven

In certain cases when dealing with text files or reading data from a "character device", the Microsoft MS-DOS shell (COMMAND.COM) or operating-system utility programs would historically append an ASCII control-Z character (aka the "SUB" character, 0x1A) to the end of a disk file

--source: wikipedia

jephthah 1,888 Posting Maven

BANERJEEEIN,

what DRAGON said... compile it and find out.

how hard is that?

jephthah 1,888 Posting Maven

i know you are asking about functions, but an important thing about static, is that when you scope a local variable as such, it will retain its value across repeated calls to the function where it is declared. this is very important when creating functions, espeically if they are intended to be imported into larger projects.

you can also make a static global variable that will retain its value and be visible to all functions with the *.c file, but no functions anywhere else, much like how DRAGON described the static-scoped function

jephthah 1,888 Posting Maven

TRIAD, sorry for the delay... for some reason, i dont get email notification of responses to threads lately, there's been a lag somewhere.

DWKS, there are no "backs" to go behind, mate. that's why we keep this all in the open. everyone is welcome to contribute. :)

Still, the first problem is his structure is NOT 20 bytes. it's 21. (regardless of what the comment says it is :P ) ...

That's why I'm saying he has alignment issues. "Version" and "HeaderLength" are only 4 bits each, and they need to be concatenated together into one 8-bit char.

if he gets the entire header correctly aligned into a string of 20 bytes, then a method like the one you described will work, and is what i've been trying to tell him do do all along :D

but i dont understand what do you mean by split the "DESTINATION and SOURCE IP ADDRESSES

the IP addresses are 4 bytes each.

in order to caluclate an IP checksum, you need to calculate all values by "performing 1's complement addition on the header broken into ten (10) 2-byte words"

so you split each of the addresses in an "upper half" and a "lower half", each sized 2-bytes.

you can do it like the way DWKS showed you. another way that might be easier to understand is convert the code you posted earlier, to convert your struct into an array of unsigned shorts, like so:

IP_HEADER …
jephthah 1,888 Posting Maven

"IP Header Version" can't be more than 4 bits. that's not allowed.

i think you need to study up on IP

read RFC 791. pay special attention to Section 3: Specification

http://www.faqs.org/rfcs/rfc791.html

here's another summary:

http://www.networksorcery.com/enp/protocol/ip.htm

.

jephthah 1,888 Posting Maven

damn. i typed out a huge reply, and just lost it. sorry, but i just can't retype all of that... :(


(1) you need to use shift operators and bitwise-ANDS

(2) no, you cant do that. you need to do something similar to the code you posted that uses "memcopy"

jephthah 1,888 Posting Maven

TRIAD

the code you show is how they perform 1's complement addition to calculate checksums.

its more complicated implementation of the basic process that I outlined to you above.

...

the last bit of code is a conversion of data from a structure into a string of unsigned chars.

so, yes, you could do something like that to convert your structure into a string of shorts.

be forewarned, though, your problems still exist with your arrangement of header elements, particularly VERSION and HEADER LENGTH should be concatenated into one 8 bit char, not strung across two.

seriously, i reccomend that you try to implement the basic IP header checksum process i described rather than blindly trying to apply some code from the internets that you don't understand.

jephthah 1,888 Posting Maven

I hate it when people use acronyms that I don't know the meaning of.

for real, man... me too!


by the way, have you met "TRIAD" ?

he's the original poster

:P

jephthah 1,888 Posting Maven

TRIAD,

your question should be "how do i calculate a checksum" .... NOT "how do i convert a struct to a short".

the quick answer, is that you take your entire header and divide it into 2-byte "words", along their natural 16-bit boundaries, and perform a 1's complement addition on each word for a total checksum that is itself a 2-byte "word"

but before you do that, you've got a few potential problems, in that your header values do not align on the 16-bit boundaries.

your first two CHARs should not be individual 8-bit values, but should be concatenated into one total 8-bit value where VERSION is the 4 MSB's and HEADER LENGTH is the 4 LSB's. -- then that 8-bit value is itself concatenated with the TOS, where VERSION/HEADER are the 8 MSBs and the TOS is the 8 LSBs.

now you have your first "word"

LENGTH, ID, and OFFSET are your next three "words"... but it is important to note here that the first three (3) MSB's of the OFFSET are actually flag bits. (you may already know this and be handling it properly, but its not obvious from your structure.)

TTL and PROTOCOL are concatenated into the fifth "word", where TTL is the 8 MSB's and PROTOCOL is the 8 LSB's.

the HEADER CHECKSUM field will eventaully be the sixth "word" --- but for purposes of calculating this checksum in the first place, you ignore this field at …

jephthah 1,888 Posting Maven

Doing so with characters is your best bet unless you're using Windows API and you're forming an application.

this doesnt make any sense. what's any of this got to do with Windows API?

you've got an 8x8 matrix of single characters... what meaningful information can you have in such a thing?

you should make an 8x8 matrix of a structure that describes every important aspect of any board position.

jephthah 1,888 Posting Maven

a char will hold a single character, which is just an 8-bit int.

what information are you trying to contain in any 'square'?

this doesnt seem very meaningful, to have one char value in any square...

jephthah 1,888 Posting Maven

I'd suggest to you that STRTOK might not be the best choice here. because the delimiters in STRTOK are treated equally. STRTOK takes a string and divides it up into any number of equivalent "tokens" based on the blind application of one or more "delimiters" that are used to mark the boundaries of the tokens.

For your problem, you want to differentiate between opening bracket < and closing bracket >, so you can treat the text found within the brackets differently than the text found without.

So I would instead use a couple of char * pointers to keep track of relative positions within the search string, in conjunction with STRSTR to find the next bracket location, along with a variable that keeps track of whether you are "inside" a tag (because the last bracket found was '<') or "outside" a tag (because the last bracket found was '>')


.

jephthah 1,888 Posting Maven

my mistake: "toupper" is in the <ctype.h> library, not <string.h>

jephthah 1,888 Posting Maven

toupper returns the value of the character that is passed in as an argument, after it is operated upon.

value1 = toupper('a');
value2 = toupper('Z');
value3 = toupper('\n');

value1 is the only one that is different than the input. its value is 'A'. value2 and value3 are the same as the input ('Z' and '\n') since they are not uppercase characters.

so whatever the value returned is, just assign it back into the place from which you pulled the original character (the first letter)

consider the code fragment example:

char capitalizeMe[10] = "lowercase";

capitalizeMe[0]=toupper(capitalizeMe[0]);
capitalizeMe[5]=toupper(capitalizeMe[5]);

the result is that the word 'lowercase' is transformed to 'LowerCase'


.

jephthah 1,888 Posting Maven

if i have a character array, say: char myCharString[16]; and i want to capitalize the first letter (ie, the first element of the array), then I use the function "toupper" found in the <string.h> library, and it will do exactly what i want it to do: myCharString[0] = toupper(myCharString[0]); however... if your exercise prevents you from using <string.h> libaray functions, then you will need to convert it by hand.

first recall the ASCII code values of characters, and that a lowercase letter is exactly 32 (0x20) greater than its uppercase version, as designated by ASCII code. So to convert the lowercase 'a' (0x61 in ASCII) to 'A' (0x41) you would subtract 0x20 from the former (or 32 decimal, if you prefer), like so: myCharString[0] -= 0x20; however, doing this by hand has its dangers. you will have to FIRST CONFIRM that the character is indeed both a lowercase letter (and not anything otherwise), else the result of subtracting 0x20 from some other character can easily crash your program.


EDIT: One thing to remember the type char is, for all intents and purposes, an 8-bit int. char and int can be operated upon in the same way with the only exception being that int is generally 32 bits.


.

jephthah 1,888 Posting Maven

VERNON: I think that giving beginners malloc is like giving a child a gun and saying "dont shoot anyone, mmkay?" ... ive become quite weary of fixing buggy programs written by someone who thinks they know how to use malloc() when the really don't.

ALBAN: what Vernon is getting at, is that you CAN NOT simply declare an array with a variably number of elements. the array size must be declared with a constant, hard-coded value.

whats the maximum that "sa kufiza" could possibly be? make your array at least that size, then. youre not going to run out of RAM.

once you get "sa kufiza", then only use that many elements of the array.


.

jephthah 1,888 Posting Maven

here's your problem:

if(y<=1)
return 1;

it should be:

if(y==1)	
		return x;
	if(y<=0)	
		return 1;

.

jephthah 1,888 Posting Maven

quintillion? thats not a large number.

now about seven years ago, i worked on a cryptographic accelerator with a 4,096-bit number... that was large


,

jephthah 1,888 Posting Maven

Our doctor want us learn how to make these shapes by using loops for exam question.

sweet.

my doctor just wants me to bend over and take a deep breath. :(

i should look into changing plans.


.

jephthah 1,888 Posting Maven

"URGENT" is so played out. But, hey, you're getting better... Your only other post here, from 5 months ago, was "HeLP I need it today"

next time, try titling your post "HALP HALP MY ASS IS ON FIRE!!!!!1" and lets see what happens.


.

Salem commented: I am dyslexia of borg - your ass will be laminated. +17
jephthah 1,888 Posting Maven

oh, well that changes things. you're talkign about the Windows API that handles windows events.

In that case you should not be processing any data in WndProc, itself. Ideally this function will only be used to report what actions the user has performed on the window. specific data processing should be handled by other (non-callback) functions.

in any event, this thread was a general discussion about *why* globals should not be used, not *how* globals should not be used. Perhaps your problem would be better served in its own specific thread.


.

jephthah 1,888 Posting Maven

no, i would declare a normal struct in the program that *calls* WndProc (most likely, your "main" routine) and, from there, pass a pointer to the struct as an argument to WndProc


.

jephthah 1,888 Posting Maven

why don't you just pass a pointer to a struct into the function?

jephthah 1,888 Posting Maven

um, okay... thanks?

jephthah 1,888 Posting Maven

lich, if youre going to help,

(1) learn to indent your code properly
(2) learn to use tags

and for the record, your code is horribly fragile and will break if someone looks crosseyed at it.

.[code=c] tags

and for the record, your code is horribly fragile and will break if someone looks crosseyed at it.


.

jephthah 1,888 Posting Maven

if (pow<0)
ans = 1.0/ans;


fractional powers would be more fun.

:P

jephthah 1,888 Posting Maven

i'll bet that, as part of the homework requirements, he's not allowed to use the <math.h> library

jephthah 1,888 Posting Maven

if you ever work on any big project you're going to be in trouble.

because if you have to work with any coworkers who develop code the right way, they're going to make you look bad ...

then people will curse your name after you've left (or been asked to leave) your job and your code is handed off to future developers/maintainers.

here's a hint: "struct"


.

jephthah 1,888 Posting Maven

are you just trying to delete the numeral characters from a string?

then do something like this

void deleteNumerals(char *mystring)
{
    char *str_ptr = mystring;
    while(*str_ptr)
    {
        if (*str_ptr<'0' || *str_ptr>'9')
            *mystring++ = *str_ptr;
        str_ptr++;
    }
    *mystring = '\0';
}

just note that the string being pointed to will be modifed in place, destroying the original. if you need to keep the original string, save a copy of it before calling the function.

.

jephthah 1,888 Posting Maven

^ i dont know how that fact informs the discussion here. It certainly doesnt change the fact that global variables/pointers are generally, if not always, evidence of sloppy programming.

perhaps sometimes, in some contexts, when sparingly used, they might be useful... but only when they are specified from the beginning and clearly limited to a specific and well-defined functionality.

but not as an afterthought, not as a quick-fix, and not because they are convenient.

jephthah 1,888 Posting Maven

global variables are evil.

most beginners don't realize this, because in short little school programming assignments , no one cares.

it becomes a major problem is "the real world" when people start relying on global variables as "duct tape" for poor program specification and implementation.

because in the real world you have tens- or hundreds of thousands of lines of code that are often written by several people, and have to be maintained by many more people and very soon, no one knows WTF all those global variables are for.

jephthah 1,888 Posting Maven

[edit] this doesn't belong on this board :) [/edit]

and neither do those $99.00 links :)

.

jephthah 1,888 Posting Maven

what, does everyone here have an ACM membership?

am i the only one who doesnt?

because i was interested in your links, so i went to check it out (the one about Hamilton Paths)

and well, guess what? I need to pay $99.00 in order to access your article.

so, instead of you getting all clenched up at the slightest bit of justified criticism, maybe you ought to consider your audience when you post links.

I mean, yeah, I could go and post all sorts of links to IEEE peer-reviewed journal articles... but i dont. ... for obvious reasons.

and by the way, great way to introduce yourself. welcome to the community.

.

jephthah 1,888 Posting Maven

too bad most people on here don't have membership to ACM.

hacker9801 commented: gtfo troll +0
jephthah 1,888 Posting Maven

this is a job for regular expressions.

try using the Regex++ library for C++

http://ourworld.compuserve.com/homepages/john_maddock/regexpp.htm

jephthah 1,888 Posting Maven

yeah ... i'm just not seeing where the "Urgency" is here

i thought someone was bleeding on the floor.

Salem commented: Indeed. +16
jephthah 1,888 Posting Maven

thats one of the most godawful implementations of a stack ive ever seen.

a stack is supposed to be simple:

PUSH and POP.

that's it.


.

jephthah 1,888 Posting Maven

lol. dont start with Salem about void main.

not only is it his raison d'être ... but he's right


.

jephthah 1,888 Posting Maven

i think this is an example of "obfuscated code"

perl programmers also love to do stuff like this.

jephthah 1,888 Posting Maven

this thread made me laugh.

jephthah 1,888 Posting Maven

theres no "learning" visual c. if you're using the free MSVC compiler, it's still just C...

MS compiler and libraries still has non-ANSI portability problems compared to GCC (linux) but given MS history, it could be a lot worse.

it at least doesnt introduce addtional problems such as what Borland does.

sorry about your virus problems. i doubt it has anything to do with college.

i still don't quite understand what you were trying to do... were you trying to solve "EVERY" possible solution from each of the 64 squares? the closed solutions alone number around 26,000,000,000,000 solutions.

i dont think you're going to get that accomplished anytime soon on your old Dell PC.

jephthah 1,888 Posting Maven

its hard for me to understand exaclty what you're doing.

are you trying to solve the knight's tour, once for each of the 64 starting positions?

does it matter whether the solution is open or closed? what exactly are you storing and recovering in the text files?

because there are literally trillions (million-millions) of closed solutions. i cant imagine you needing to provide more than one solution for each starting square.

if it helps any, each 64-round "tour" should be easily solved by the computer program in less than 5 seconds, and thats just overhead for various I/O. without printing to screen, should be much less than 1 second.

if it's hanging on you, you've got some major problem(s) somewhere, probably an infinite loop.