leave_letters(char *string) { int i=0,j=0; char *c[20]={'/0'};
for(i=0;i
Blake Baker
Of course, the point would be to print out only letters. For example, if I input "Sup Forums" it should output "chan".
Carter Gutierrez
Fuck, I forgot to put it the way it was! isalpha(string[i]) should be !=0, not ==0.
Easton Ward
>ide >gui
kill yourself
Zachary Reyes
>char *c[20] Are you sure you're not declaring an array of 20 pointers to characters?
James Flores
Why did Sup Forums turn to shit. :(
You're not 1337 for avoiding ide's you idiot. As you can see, I have issues with a simple fucking problem, imagine if I tried to learn emacs or vim or something else on top of the C. Fuck off mate.
Ethan Flores
it's because you're using gets(). gets() has been deprecated for YEARS. Get with the times!
Brandon Hall
Idk senpai.. Consider downloading atom as a text editor. It's really flexible and has tons of plugin support.
Jaxon Foster
Not sure in anything now, but the same declaration works normal with the "char *string[20]" part. When I try to debug it and turn on Watches tho, it sees what I input in "string", but for "c" gives me gibberish or "address out of bounds" or something like that.
Liam Wilson
But that part works fine. ;_;
Colton Lewis
Maybe because you're overwriting the null terminator on c..
Nice! Now I'm all confused about pointers again! I deleted the * and just went with string[20] and c[20] and it works fine now. God dammit. Why the fuck did it work with string[20] and not with c[20]. :(
Anyways, thank you mate, helped a ton!
Carson Brown
Just did this: char c[20]={'/0'}; But yours could work too I think. Also it's != in the condition, not ==. My bad with copying, forgot to put everything back the way it was!
Andrew Robinson
>atom Did you mean Visual Studio Code ?
Thomas James
I like how Sup Forums's official hobby today is arguing which OS/Browser/IDE/TE is better all day every day. Read a god damn book.
Jacob Williams
Leave_letters has no function type but that may not matter in C.
Chase Miller
It doesn't, it's int by default. At least in GCC compiler I think.
Lucas Watson
Post what code you have now?
Thomas Richardson
Everything's the same, I just deleted the *-s, and switched the '==' in the condition with the '!=' as I should've from the start.
leave_letters(char *string) { int i=0,j=0; char c[20]={'/0'};
for(i=0;i
Hudson Smith
#include #include
int main() { char *string[20];
gets(string); leave_letters(string); }
leave_letters(char *string) { int i=0,j=0; char *c[20]={'/0'};
for(i=0;i
Lincoln Nguyen
You have to think about what those variables are in memory and how they are treated when they are casted to different types.
A char *string[20] is an array of 20 pointers. On a 32-bit system, this will (most likely) be a chunk of 20*4 bytes. With each pointer being 32 bits. When you pass this into gets(), it gets casted into a char array, which would be a the same array but indexed by 1 byte at a time instead of 4 bytes at a time, i.e. char[20*4].
leave_letters takes a char * argument, so when you pass string into leave_letters, that chunk of memory gets casted into a char[] just like it did when you passed it into gets(). This is why the mistake on the first string wasn't affecting the program.
The mistake on c[20] though has an effect. To the function, c[] is an array of pointers, so it is indexed every 4 bytes. When you set c[j] = string[i], you are setting the first 4 bytes of c to represent the value represented by 1 byte in string. That means if string[0] = 'a' or 0x55, then c[0] = 0x00000055. If string[1] = 'b' or 0x56, then c[1] = 0x00000056. in a little endian system, the first 8 bytes of c[] would look like this: [0x55, 0x00, 0x00, 0x00, 0x56, 0x00, 0x00, 0x00].
Now, when you finally pass c[] into puts(), it gets casted into a char*, which indexes individual chars (or bytes). It starts by printing 0x55=>'a', then finds 0x00=>'\0' as the next byte and stops because that is the null char.
Landon Price
If your "IDE" doesn't even understand what you're writing with it, it's just a bloated text editor.
Get something that can tell you when you get something like this wrong.
Logan Carter
This is right. string and c should both be arrays of characters.
Also you should never use gets. If the input is 20 or more bytes long, it will overflow the buffer and undefined behaviour will happen, including inside leave_letters where strlen(string) is more than 20. Instead, use fgets(string, 20, stdin).
You probably meant a backslash rather than a slash. This is not a common way to initialize a string, but I think it would work; if the array initialization value in braces doesn't have enough elements, the rest are filled with zeros, so you have a null terminator wherever the string ends up stopping. More common would be to put c[j]=0; after the loop.
Dominic Cook
Ooooooh... I got it! I'm very grateful to you for taking the time to explain that to me mate, thanks a ton!
Never did learn how to do that actually! Like this?
Luke Reed
Okay VS does it in a nice hovering style, codeblocks does the same, just in a separate window. The problem there was, I didn't really understand why it's complaining.
Nicholas Bennett
Does it do that when you're typing or only when you compile the program? A separate window sounds like it's just the compiler output.