I've been at it for an hour now, I think I'm going mad. What the fuck am I doing wrong here guys...

I've been at it for an hour now, I think I'm going mad. What the fuck am I doing wrong here guys, it prints out only one character!

*****************************************************

#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

Of course, the point would be to print out only letters. For example, if I input "Sup Forums" it should output "chan".

Fuck, I forgot to put it the way it was! isalpha(string[i]) should be !=0, not ==0.

>ide
>gui

kill yourself

>char *c[20]
Are you sure you're not declaring an array of 20 pointers to characters?

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.

it's because you're using gets().
gets() has been deprecated for YEARS. Get with the times!

Idk senpai..
Consider downloading atom as a text editor. It's really flexible and has tons of plugin support.

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.

But that part works fine. ;_;

Maybe because you're overwriting the null terminator on c..

Might try:

if(isalpha(string[i])==0)
{
c[j] = string[i];
c[j++] = '\0';
}

if(isalpha(string[i])==0)
{
c[j] = string[i];
c[++j] = '\0';
}

That should have been, sorry

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!

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!

>atom
Did you mean Visual Studio Code ?

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.

Leave_letters has no function type but that may not matter in C.

It doesn't, it's int by default. At least in GCC compiler I think.

Post what code you have now?

Everything's the same, I just deleted the *-s, and switched the '==' in the condition with the '!=' as I should've from the start.

****************************************************

#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

#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

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.

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.

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.

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?

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.

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.

wat u try do ?