jephthah 1,888 Posting Maven

&q has a different type to q, so any good compiler would complain about incompatible pointer assignments

yes, you're right.

even though it works for this trivial example, not casting it properly is a bad habit to get into.

the correct way to initialize it is:

char q[16];
char *p = [b](char *)[/b]&q;
strcpy(q,"abc");
Salem commented: Nope, you're still wrong -7
jephthah 1,888 Posting Maven

try this:

char q[16];
char *p = &q;
strcpy(q,"abc");

.

jephthah 1,888 Posting Maven

I feel that Dani is your home and you're the one who says who should do what

this is no more "my" home than yours. do you see a "mod" bubble by my name? no. i don't even have a "featured" bubble, like you and serkan.

i just am blunt, and have little tolerance for bullshit. but that's just my opinion, and I've already been warned once for being an unpleasant ass. I'm trying to moderate my tone, but I'll still probably get banned before the year is out.


.

jephthah 1,888 Posting Maven

f you become a super mod, the first thing you would do would be to ban me from DaniWeb

hardly. because then who would i have to beat up on in geek's lounge?

:P

jephthah 1,888 Posting Maven

I don't see how my post was harsh, and i'm not saying we don't deserve credit for good posts, but requesting for your friends to be featured shows you're more interested in a badge rather than just helping.

your post wasnt harsh. it was honest and insightful, and far more gentle than I'll ever be. Mine was harsh. and here's one that's harsher:

The problem is, Serkan is always coming around here whining and complaining about his rep points, now he's complaining about his friends not getting a "featured" bubble

the reason his rep suffers, is because too many people are revolted by his continual whining, along with his overly creepy behavior towards certain female members.

if he quits acting like this place is some popularity contest, and sticks to assisting people with code problems, he might actually begin to be respected again.

i swear there's less drama at a middle school girl's slumber party than there is in a typical Serkan thread.

jephthah 1,888 Posting Maven

you miss my point ... even you dont have it with your significant reputation points.

there are plenty of people with more rep points than me who haven't been featured. and rep points are just as subjective and arbitrary as being selected to get a "featured poster" bubble. i mean, look at you: you were "featured". that proves how meaningless it is.

But i question the reasoning behind "essentials" getting featured.

it is you who miss the point. this is not a democracy. and featured poster is not some "American Idol"-like popularity contest that you get to vote on. the owners of this site can run this place any way they choose to do so, without soliciting your opinion or asking your approval.

Why'd they pick essentials? The real question is: why should you even care? What is it about essentials that makes you lose sleep at night? Personally, i dont care who gets featured or not. All i care about is becoming Super Mod.

i am going to organize ... a better c# forum where their skills are appreciated.

yeah okay, don't let the door hit you in the ass on the way out :)


.

William Hemsworth commented: Well said. +13
scru commented: omg man. harsh. +6
jephthah 1,888 Posting Maven

jesus shut up already. no one gives a flying shit that your best friends didnt get "featured."

serkan sendur commented: -4 -1
nav33n commented: I am very unhappy with Daniweb's bad-word-filter. It let you type shit (emm.. literally) and didn't even replace it with * ! -2
jephthah 1,888 Posting Maven

although I don't understand why did you use IF condition

this is not your thread to ask basic questions. go open your own thread on how to use IF.

and if you don't know how to use sscanf() -- much less how to look it up -- then you damn sure need to quit "helping" people.

.

jephthah 1,888 Posting Maven

how to extract some character/number from a char string?

Example:
char = TAGNAME_C123_V45_S67_M89

what you have written here is an identifier from a #define 'd constant -- not a string.

if it were a string, then you could use sscanf() as Tom Gunn suggested. but he's assumed that you really meant to put something like char *p = "TAGNAME_C123_V45_S67_M89"; ... and that is not what you wrote, since it was not in quotes.

if TAGNAME_C123_V45_S67_M89 is a constant identifier -- as it appears to be -- then you can not merely use sscanf() to extract substrings out of the identifier name. it becomes more complicated, and you will have to rewrite a significant amount of code.

if you truly meant it to be a string, then it must be in quotes and either (1) assigned when declared as a const char * , as Tom suggested, or (2) assigned later with a function like "strcpy()"

the difference is important, and will significantly affect how you accomplish what you want to do.

.

jephthah 1,888 Posting Maven

kernel32.lib can't be found. I've found the library in some Microsoft SDK folder and I add that to the library search path but ... something is wrong.

your .NET installation is broken. Unless you're some sort of windows/.NET guru, the chance of you fixing it correctly is small.

uninstall all visual studio and .NET components, reboot, then reinstall.

jephthah 1,888 Posting Maven

$350 a lid?

holy shit, you kids these days.

:icon_rolleyes:

jephthah 1,888 Posting Maven

i made a mistake. i said the OP who suggested that programmers were stoners was from the u.s. midwest... that's not correct. Menster is apparently from Australia.

so let me make my own overly gross generalization:

the reason you think all programmers are "stoners", Menster, is because you're from Australia. and as the rest of the world knows, there's no significant tech development coming out of Australia.

Now i can't say if that's the cause or the effect of you guys being a bunch of potheads, but there must be some connection.

jephthah 1,888 Posting Maven

Yeah, and Hempfest is coming up soon. I wonder if Seattle's former Chief of Police, Norm Stamper, will be there speaking on behalf of marijuana legalization/reform.... again.

:D

jephthah 1,888 Posting Maven

this is a bizarre correlation that you've made, that most programmers are "stoners".

i don't really know many programmers who are "stoners". most of them are intellectual types (or even downright "nerds") who aren't especially prone to partying.

granted *some* of them can be grouped in what you would classify as the "stoner culture", but those tend to be more of the unprofessional, undereducated, overgrown skript kiddies --- you know, the self-taught, PHP webpage jockeys who spend more time playing MMORPGs than producing code.

now, I'll admit: maybe my opinion is colored by my area of the country. I'm in Seattle, where two things may be quite different from your area of the US Midwest.

(1) we have a huge professional programming community (think: The Software Giant in Redmond), most of whom are highly educated and motivated.

(2) we also have a culture that doesn't much give a damn about who smokes a little pot in their private time. Marijuana use has been all but decriminalized here, so most people don't get their drawers all in a bunch about it.

Disclaimer: I don't smoke pot, and I don't much care whether anyone else does or doesn't.


.

jephthah 1,888 Posting Maven

For those with a below-par grin ...smile-boosting messages will pop up on the computer screen [such as] "lift up your mouth corners",

umm... yeah.

You know what? Japan is kind of weird.

scru commented: ^.^ +6
jephthah 1,888 Posting Maven

After adding malloc ,this code compile and execute perfectly ... can u explain it?

'k' is a pointer. meaning it describes, or "points to," an address of memory.

when you first declare this pointer, there is no memory allocated for it and the address that it points to is undefined.

malloc() then causes a location of memory to be allocated (in your example for a single integer) and the pointer 'k' is assigned to the address of this allocated memory space.

the *value* contained at this memory space is still undefined, until the line of code *k = 10 , which assigns the value 10 to the memory pointed to by 'k'

as wildgoose suggested, to use malloc for a single integer is not very practical. malloc is for dynamically sizing arrays, not single variables. you should not get in the habit of using it like this. You should instead do it the way he showed you in Post #2

in any event, it will make more sense to print the address as a hex value. use the format specifier '%p' to print a pointer as a hex address: printf("address %p = %d\n", k, *k); see http://www.faqs.org/docs/learnc/c620.html

.

jephthah 1,888 Posting Maven

but fgets reads strings can it read ints 2 ?maybe if i read a string and convert to int by atoi ?

that's one way to do it. I prefer to use "strtol()", because you can do more robust error checking to see if the user entered a valid number.

but sure... try "atoi()" to get started.

.

jephthah 1,888 Posting Maven

fflush(stdin) is totally, utterly, and completely wrong.

never, ever, use it.

learn to parse your input correctly in the first place, and you wont have this problem.

so get rid of scanf() while you're at it. that's a bad function to use, and is a source of the problem.

use fgets() instead, and parse the input to find the data you're looking for.

jephthah 1,888 Posting Maven

Hi,
I am interested in doing Phd in computer science. Can anyone help me in finding a suitable topic

waiting for early reply.

here's an early reply: get your PhD in post-hole digging.

i mean, jesus.

if you need an internet chat forum to tell you what your career research focus will be, you probably should go on and get out of academia now.

jephthah 1,888 Posting Maven

most of these trifling problems will go away when you throw away the P.O.S. Borland/Turbo compiler and get a real C/C++ compiler like CodeBlocks or Bloodshed Dev C++.

these GCC-based, standard C compilers wont let you use worthless functions like flushall(), obsolete libraries like conio.h, and will complain heartily if you try and hang yourself by using gets()

.

jephthah 1,888 Posting Maven

the answer to your basic problem:

if isdigit(cResponse);

is incorrect. it should be

if (isdigit(cResponse))
{   
    // do stuff here
}

(1) an if-statement must be entirely enclosed in parentheses.

(2) an if-statement -- if the condition is evaluated as true -- will execute either one line of code terminated with a semi-colon or a block of code contained between the curly braces, '{' and '}'

so even if you framed the conditional statement correctly, since you put the semicolon directly after the if statement, it still would never execute anything when the condition evaluated true.

.

jephthah 1,888 Posting Maven

If the code is already there, you might as well help make it better and hope the guy learns something instead of sitting on your **** waiting for him to fail at life.

or, conversely:

you post your code there, and wait for someone to make it better and hope some guy doesn't expect you to actually learn something, and you can just sit on your ass waiting for handouts all your life

And he can ask questions. That's how it works in most places. See something you don't understand, ask about it, get an answer, and learn.

O RLY?

and where are these "most places" of which you speak? The ones where people ask questions to get answers? Because it sure isn't here.

DoEds
Newbie Poster
Posts: 1


.

jephthah 1,888 Posting Maven

i dont know what you did, but your code is not visible.

so, please use code tags.

jephthah 1,888 Posting Maven

^ i like that link.

i don't think there's an equation without using summation. i tried to think about it for a minute, but gave up. I'm not really a math guy. :D

jephthah 1,888 Posting Maven

Is there a tutorial around for how to post math terms?

it's just LaTeX markup tags. any reference will describe the basics

http://en.wikipedia.org/wiki/Math_markup

jephthah 1,888 Posting Maven

and you don't need a "Big Number Library". you dont even need a long int . 10,000 is easily represented by a regular integer

the way to approach this problem is by summation:

\sum_{j=1}^{\left \lfloor sqrt(n) \right \rfloor} \left \lfloor \frac {n}{j} - j + 1 \right \rfloor

note, the bars represent integer floor. in code it would look like:

int answer = 0;
for (j = 1; j <= ((int)sqrt(n)); j++)
   answer += (n/j - j + 1);

.

VernonDozier commented: Good formula and it looks nice with the TEX tags. +17
jephthah 1,888 Posting Maven

the formula posted by siddhant is fundamentally flawed. it only appeared to work, because it just happened to be valid for n=2 through n=8. as 'n' increases past 8, the result becomes more and more inaccurate.

n=9 to n=11, the result is off by 1
n=12 to n=14, the result is off by 2
n=15, the result is off by 3
n=16 to n=17, the result is off by 4
n=18 to ... the result is off by 5

etc

jephthah 1,888 Posting Maven

EDIT: never mind. find it yourself.

jephthah 1,888 Posting Maven

The problem is the way we are taught in school. In school, we still use
#include<iostream.h>,#include<conio.h>,getch(),clrscsr()...etc.etc....

I say that you should avoid the use of old, non-standard libraries like "conio.h" unless you are explicitly instructed -- required -- to do so.

anyone can quickly learn to use any library function that one may given in the course of their work. But you will only be hurting yourself if you learn to rely on those non-portable functions, and then one day find all your code is broken when you try to port it to another environment.

also, when you come to ask questions on a standard-C forum like this one or any of the other major forums, you will lose the assistance of people who do not, can not, or will not use non-portable libraries such as conio.

ultimately it's your choice. personally, my choice is that i just don't even bother trying to compile anyone's code who uses non-standard libraries. I simply don't care to include those libraries on my work or home machines, and therefore i don't spend any time trying to debug examples those people might post.

jephthah 1,888 Posting Maven

what salem said.

and, anyhow, if you really want to do this correctly, you will use the standard C library <time.h> ... you will make use of the time_t type, and the structure tm.


here is a tutorial

jephthah 1,888 Posting Maven

Hey jephthah, since when can we use single quotes for c-strings?

case 1: printf("\n%s\n", 'one'); break;
case 2: printf("\n%s\n", 'two'); break;
case 3: printf("\n%s\n", 'three'); break;
case 5: printf("\n%s\n", 'five'); break;
case 11: printf("\n%s\n", 'eleven'); break;

:P

oh, hell, i just cut and pasted his code. i didn't even notice that

:)

jephthah 1,888 Posting Maven

um... you pretty much just described the two steps.

$  gcc file1.c main.c 
$ ./a.out

what more do you want to do?

Salem commented: Teach me to walk. "I put my right foot forward, then my left. Now what?" +35
jephthah 1,888 Posting Maven

as already stated, never try to use strings as cases for a switch. Some languages (like Perl) will allow this. C will not.

you could, for instance, try something like this:

void read_snum(char *sno)
{
   int courseNum;
   courseNum = atol(sno);
   
   switch(courseNum)
   { 
      case 1: printf("\n%s\n", 'one'); break;
      case 2: printf("\n%s\n", 'two'); break;
      case 3: printf("\n%s\n", 'three'); break;
      case 5: printf("\n%s\n", 'five'); break;
      case 11: printf("\n%s\n", 'eleven'); break;

      default: printf("\nInvalid Course Number\n");
   }
}

note, that my use of atol employs no error checking. I'm just using this as an example for switch/case statement. the preferred method would be strtol() and verify the string contains a valid number before moving to the switch.

.

jephthah 1,888 Posting Maven

Hello World!
i want to write a program

awesome!

Can any one help me!?

sure can!

Thanks in advance!

you're welcome!

.

WaltP commented: Was that necessary with the previous 3 responses? Useless posts like this do get old. -4
jephthah 1,888 Posting Maven

well, K&R is not really written for the beginner. It was written for people who were professional programmers in another language looking to learn the "new" language C.

obviously it's also older, and there have been a number of really good instructional books written since.

but admit it. you love this bitwise stuff now, don't you? :) it's great when you can solve a problem, without being handed the code.

jephthah 1,888 Posting Maven

you did not implement it right.

before declaring it 'done', you need to test the program with known values and known solutions. like the one i described x=85, p=6, n=5, y=238 ... the result should be 57. i'm quite certain the program you've posted will not give that result.

you're supposed to delete the bit field from 'x', but what you're doing is deleting everything around it. also, you should not be shifting the x value, but merely ANDing it with the proper bitmask.

you're also supposed to be keeping only the rightmost 'n' bits from 'y' ... contrariwise, you are deleting the rightmost 'n' bits from 'y' and keeping the rest.

.... i'm gonna let you figure out why, and how to fix it. re-read my post #6, above. the answer is there.

you also might want to make your variable types unsigned integers.


.

jephthah 1,888 Posting Maven

why do a convoluted loop? the point, anyhow, is to learn bitwise operators. otherwise throw the chapter away and go learn about loops.

but i do see that i misunderstood part of the question. you're not being asked to extract 'n' bits of the 'y' value from the bit pointer 'p'. you're only need to extract the rightmost 'n' bits from 'y'

it's still essentially the same problem/solution as what i demonstrated. here is the same example:

given x=0x55, y=0xEE, p=6, n=5 .... remove the 'n=5' bits from 'x' starting at bit number 'p=6', and replace them with the rightmost 'n=5' bits from 'y' ... you've already got the components that you need from the previous example ~(~0 << n) creates a bitmask of size 'n', and x << (p+1-n) will left-shift (notice i changed direction) the number of bits that you need based on 'p' and 'n'

in the example p=6, n=5, you want a mask starting at bit #6 and to be 5 bits wide, so the last bit must be bit #2 (bit #'s 6,5,4,3,2 = 5 bits total) and therefore you will left-shift the bitmask 2 bits. hence, p+1-n = 2.

so create a bitmask 5 bits wide 0000000000011111 , and AND it with 'y' to extract the rightmost 5 bits of 'y' into a new variable, say "y1".

shift "y1" two places to the left using the code you should now understand, shown above.

shift teh bitmask 2 …

jephthah 1,888 Posting Maven

andy is damned and determined to pound those square pegs into some round holes.

bummer. i was looking forward to some cool Narue code.

.

jephthah 1,888 Posting Maven

take the example: given x=0x55 (dec 85, binary 01010101), p=6, n=5, and y=0xEE (dec 238, binary 11101110)

what you want to happen is this:

x=  01010101  (0x55, dec 85)
p=6  ^        (bit #6)
n=5  .....    (get 5 bits)
    -10101--  (extracted from 'x'.  '-' are zeros)

y=  11101110  (0xEE, dec 238)
p=6  ^        (bit #6)
n=5  .....    (get 5 bits)
    -11011--  (extracted from 'y'.  '-' are zeros)

('r' return value is the value of 'x' with the
extracted bits replaced with the bits from 'y')

x=  0-----01  ('x', after extraction.  '-' are zeros)
    -11011--  (extracted from 'y'.  '-' are zeros)
+   ________
r=  01101101  (return value, 0x=6D, dec 109)

remember the function from your previous question

unsigned getbits(unsigned x, int p, int n)
{
    return (x >> (p+1-n)) & ~(~0 << n);
}

remember that the portion ~(~0 << n) created a bitmask of the form where number of 1's equaled the value of 'n' such that if n=5, a (16-bit) bit mask would be 0000000000011111 .

now this bitmask could be shifted two bits to the left, so that it looked like 0000000001111100 , and this could be used to extract the correct bits from 'y'

this same bitmask can then be inverted, so that it looked like 1111111110000011 , and this could be used to remove the no-longer-needed bits from x.

once youve removed the bits from 'x', its a simple matter to insert the corresponding bits that were extracted from 'y', by …

jephthah 1,888 Posting Maven

You must submit two valid Bot programs (one for your Offense Bot & one for your Defense Bot) to me at _my_email_@gmail.com

yeah, i'm thinking this must be some sort of trap.


.

jephthah 1,888 Posting Maven

~(~0 << n) makes a bitmask of ones equal to the number 'n'. does this by first filling the integer full of ones, then shifting them to the left 'n' bits (filling lsb's with zeros), then inverts all the bits.

for instance, n=5 makes a bitmask = '...00000011111' x >> (p+1-n) shifts the original number to the right 'p+1-n' bits.

the whole point is to extract the bits from the original value, starting with the zero-indexed bit # at 'p' (pointer) and extracting 'n' number of bits moving to the right.

the two are ANDed together to get the new, extracted, value

example value = 0x55 (01010101) with p=6 and n=5 will extract bits 6 through bit 2 (total of 5 bits), to find the new value 0x15, (dec 21, or '10101')

01010101
p=6  ^
n=5  .....
     10101   val = 0x15 (d 21)
William Hemsworth commented: Good post. +11
jephthah 1,888 Posting Maven

uh, yeah, i meant to say that the memory is allocated when the "program" is called.

but look if you want to be a pedant, then it's true there's no static array in the OP's snippet. what there is is an illegal attempt to use an undefined variable in what would otherwise have been a static array.

jephthah 1,888 Posting Maven

short answer: static arrays are allocated when the function is called, at which point your variable is undefined. so the construct is illegal.

if you want to dynamically allocate arrays, you need to use malloc() along with a corresponding free()

jephthah 1,888 Posting Maven

it's not "jephthah approved". it's "industry acceptable".

i felt my post was quite constructive: I gave several links to a variety of modern and free standard-C compilers without prejudice.

i mean, 3.0? we're talking vintage 1990. there's been a few changes to compilers and the C++ language in the past 20 years :)

but okay. you're right, i'm over the line again with my sarcasm and tone. apologies to those involved.

.

jephthah 1,888 Posting Maven

whether it's currently practical or popular isnt the question. the question was whether C++ defines a byte as being exactly 8 bits. as several of us have learned, it does not. it can be 8 or more.

historically (1960's - 1980's), mainframe and scientific computers had a wide variety of byte sizes, from 6 to 36 bits and all sizes in between.

see Clines' C++ FAQ Lite for more info

jephthah 1,888 Posting Maven

i, too, believed the same thing Tux did.... thanks Narue for the detailed lesson. i learn a lot here.

.

jephthah 1,888 Posting Maven

^ ahaha.. your rep just went from -49 to 0 with one pity post by Dragon. LOL.

now look, here's your chance, adatapost: don't screw it up. think before you post. and get rid of your Turbo C compiler.

jephthah 1,888 Posting Maven

I cannot ____ because I use Turbo C++ 3.0 IDE

are you stuck in a time machine? or is it still 1990, where you live?

throw that shit out, and join the 21st Century

If possible could you please give an example.

Code::Blocks (free)
Visual C++ express (free)
Bloodshed Dev C++ (free)

I am always ready to shift to Visual C++ Express but not until I have the answer to this question.

I am always ready to answer your question, but not until you get a modern compiler that isnt gimped

of course, then you will start having real questions for real problems, and we can just close this thread.


.

jephthah 1,888 Posting Maven

how about actually doing work when you're at work getting paid to work instead of goofing off?

jephthah 1,888 Posting Maven

Long story!!

yeah, it usually is, when you try and quit the internets.