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 13:14

>>12
I was just saying what his code did, not if it makes sense. His code was casting a pointer to a character and writing it to an array, don't believe me? Here comes some proof:

- C file

void main(void)
{
    char a;
    char b[20];
    a = "test";
    b[20] = "loltext";
}

- Generated assembly for x86 target:

$SG34    DB    'test', 00H
    ORG $+3
$SG35    DB    'loltext', 00H
_DATA    ENDS
_TEXT    SEGMENT
_a$ = -4
_b$ = -24
_main    PROC NEAR
; File test.c
; Line 2
    push    ebp
    mov    ebp, esp
    sub    esp, 24                    ; 00000018H
; Line 5
    mov    eax, OFFSET FLAT:$SG34 ; eax contains pointer to 'test' string
    mov    BYTE PTR _a$[ebp], al ; al is the lower byte of eax, or pointer to 'test' & 0xFF as I said before.
; Line 6
    mov    ecx, OFFSET FLAT:$SG35 ; ecx contains pointer to 'loltext'
    mov    BYTE PTR _b$[ebp+20], cl ; cl is the lower byte of the pointer to 'loltext' (or just p & 0xFF ), and its written to b[20]
; Line 7
    mov    esp, ebp
    pop    ebp
    ret    0
_main    ENDP
_TEXT    ENDS
END


This is actually quite simple to understand, C will allow casting almost anything to anything you want, and the compiler will throw warnings when you do stupid stuff like that, but it won't forbid you to do it.

Example warnings:

test.c
test.c(5) : warning C4047: '=' : 'char ' differs in levels of indirection from 'char [5]'
test.c(6) : warning C4047: '=' : 'char ' differs in levels of indirection from 'char [8]'


I never said what OP was doing was correct in any way, i merely stated what his could would do when compiled/ran!

Now go back to reading that K&R book.

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