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.
>>10
u raelly sounds like an EXPERT C PROGRAMMER...pointer cast to a character..are you crazy?
Name:
Anonymous2009-02-25 11:28
>>12
6. Compilers often give more memory to arrays than you asked for. Here's how to check how big an array actually is (memset returns a null pointer if the size you passed to it is bigger than the size of the array you passed to it): int arr[256];
size_t realsize;
for (realsize = 0; realsize <= SIZE_MAX; ++realsize)
if (!memset(arr, 0, realsize)) break;
/* now you know that arr actually has realsize / sizeof (int) elements */
If you combine this with #5, your program will be faster in the long run (but this usually doesn't work for short programs).
>>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!
To continue off that post,
a string will usually be statically allocated and passed as a pointer (which is usually no different when stored than an int for x86 platforms), now the OP tries to put an pointer into a character? The compiler will issue a warning(or error depending on options(how strict) used), and implicitly cast it to a char, which just does as was shown above.
>>21
You do realize that random function really isn't random don't you? Wait, never mind. Don't answer that.
Name:
Anonymous2011-12-30 14:10
>>22
Yes and what is your point?
It doesn't interfere with the concept I was trying to illustrate.
Name:
Anonymous2011-12-30 16:23
>>13 memset returns a null pointer if the size you passed to it is bigger than the size of the array you passed to it
In PROPERLY BOUNDS-CHECKED C, memset() would signal a SIGSEGV if it exceeds the bounds of the array, which could then be caught.
array = "string";
You can do char array[] = "string";
But you can't assign a string to an array that already exists:
[quote]$ cat >tmp.c int main()
{
char array[20];
array = "string";
return 0;
}
$ cc tmp.c
tmp.c: In function ‘main’:
tmp.c:4:8: error: incompatible types when assigning to type ‘char[20]’ from type ‘char *’[/quote]
Name:
Anonymous2011-12-30 18:09
>>26
That's because a string literal is a character array, which behaves like a constant pointer except for sizeof and unary &.
[ Fri Dec 30 07:10:24 ]
[ @ ~/host/prog/sl ] $ cat sl.c
#include <stdio.h>
void main()
{
const char space = ' ';
const char nl = '\n';
const char *psl[] = {
"Fuck Kodak",
"Fuck him in the anus",
"Fuck him good"
};
char sl[] = "bodak is a fag";
int i,j,len;
sl[0] = 'k';
len=sizeof(sl)/sizeof(char)-1;
puts(sl);
for(i=1;i<len-1;++i){
putchar(sl[i]);
for(j=0;j<len-2;++j){
if(j != i-1 && j != len-i-2)
putchar(space);
else if(j == i-1)
putchar(sl[i]);
else if(j == len-i-2)
putchar(sl[len-i-1]);
}
putchar(sl[len-1-i]);
putchar(nl);
}
for(i=0;i<=len;++i)
putchar(sl[len-i]);
putchar(nl);
for(i=0;i<len;++i){
for(j=0;j<=i;++j)
putchar(sl[j]);
for(j=i+1;j<len;++j)
putchar(sl[len-j]);
putchar(nl);
}
puts(psl[0]);
puts(psl[1]);
puts(psl[2]);
}
[ Fri Dec 30 07:10:50 ]
[ @ ~/host/prog/sl ] $ gcc -std=c89 -pedantic sl.c
sl.c:2: warning: return type of ‘main’ is not ‘int’
[ Fri Dec 30 07:10:53 ]
[ @ ~/host/prog/sl ] $ ./a.out
kodak is a fag
oo aa
d d f f
a a
k k a a
i is s
s is i
a k a k
a a
f d f d
ao ao
gaf a si kadok
kgaf a si kado
koaf a si kado
kodf a si kado
koda a si kado
kodaka si kado
kodak si kado
kodak isi kado
kodak isi kado
kodak is kado
kodak is akado
kodak is a ado
kodak is a fdo
kodak is a fao
kodak is a fag
Fuck Kodak
Fuck him in the anus
Fuck him good
[ @ ~/host/prog/sl ] $ rm a.out
[ Fri Dec 30 07:14:44 ]
[ @ ~/host/prog/sl ] $ tcc sl.c
[ Fri Dec 30 07:14:46 ]
[ @ ~/host/prog/sl ] $ ls
a.out sl.c
btw This is something i made a while back... obviously doing a bit more than you need, and naming convention used is pretty terrible, but it's a start...
//-------------
>>33
you're a moron, enjoy your shitty compiler that's not even smart enough to convert a void main to int main and return 0 at the end.
Name:
Anonymous2011-12-30 19:29
>>35
IT'S CALL STANDARD C YOU FUCKING MORON. I DON'T KNOW IF YOU KNOW THIS OR NOT, BUT C IS MEANT TO BE PORTABLE. THAT FUCKING MEANS A COMPILER MUST FOLLOW CERTAIN STANDARDS TO ENSURE PORTABILITY.
NO GO SCRUB ANOTHER TOILET YOU FUCKING DUMB JEW.
Name:
Anonymous2011-12-30 19:31
>>36
you forgot your trip kodak fag.
gcc is portable.
Name:
Anonymous2011-12-30 19:32
>>37
OH MY BAD. I FORGOT TO USE THE "YOUR A FUCKING MORON" FLAG.
>>38
I can tell you're angry by the forced capitalization of all words. It's ok we know you're retarded. The faster you understand this the faster we can help you.
Probably a bad example for someone who is new to coding though..
In turbo pascal (or most any language =D) you could just use a readline / writeline to store & load words from a text file... then just pick words (/lines from the file) at random, and print to screen...
might only need 20-30 lines of turbo to get it to work