Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Shit in C

Name: Anonymous 2009-02-25 10:09

I'm trying to set the contents of a string based on what random number is generated by my srand.

dice = rand() % 10 + 1;

if ( dice = 1 )
word[20] = "Success";

^-- this shit doesn't work, though.

tl;dr. I want my "dice rolls" to generate a random number, random number corresponds to a pre-set word, and the preset word to be printed.

I'm probably going about it the wrong way. Suggestions from experts?

Name: Anonymous 2009-02-25 10:58

word[20] = "Success";

What is this ``word'' ? According to >>4 it's a character array/string then you're basically putting a pointer which got cast to a character ( so pointer & 0xFF ) into the 20th element/character(?) of the word array(or string?). This doesn't really make any sense. Maybe you want to just make ``word'' point to that string then simply write word = "that string"; It will make word point to the new string, also you would probably want to change that char word[20]; to char *word; since you want to be passing a pointer to the string, otherwise word will be preallocated on stack or locally or wherever you wrote that definition, alternatively you can use strcpy/memcpy/... to copy the string to your 20 character buffer... or maybe you should stop posting here and instead read K&R until you understand types( and pointers ) in C.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List