[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

I've been at it for an hour now, I think I'm going

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 28
Thread images: 2

File: RhPaAmt.png (22KB, 362x589px) Image search: [Google]
RhPaAmt.png
22KB, 362x589px
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 <stdio.h>
#include <stdlib.h>

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<strlen(string);i++){
if(isalpha(string[i])==0)
{
c[j]=string[i];
j++;
}
}
puts(string);
puts(c);
}

****************************************************
>>
Of course, the point would be to print out only letters. For example, if I input "4chan" it should output "chan".
>>
Fuck, I forgot to put it the way it was! isalpha(string[i]) should be !=0, not ==0.
>>
>>56373204
>ide
>gui

kill yourself
>>
>>56373204
>char *c[20]
Are you sure you're not declaring an array of 20 pointers to characters?
>>
>>56373265
Why did /g/ 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!
>>
>>56373293
Idk senpai..
Consider downloading atom as a text editor. It's really flexible and has tons of plugin support.
>>
>>56373283
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.
>>
>>56373304
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
>>
>>56373283
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!
>>
>>56373441
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!
>>
>>56373322
>atom
Did you mean Visual Studio Code ?
>>
>>56373489
I like how /g/'s official hobby today is arguing which OS/Browser/IDE/TE is better all day every day. Read a god damn book.
>>
>>56373204
Leave_letters has no function type but that may not matter in C.
>>
>>56373525
It doesn't, it's int by default. At least in GCC compiler I think.
>>
Post what code you have now?
>>
>>56373684
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 <stdio.h>
#include <stdlib.h>

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<strlen(string);i++){
if(isalpha(string[i])!=0)
{
c[j]=string[i];
j++;
}
}
puts(string);
puts(c);
}
#include <stdio.h>
#include <stdlib.h>

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<strlen(string);i++){
if(isalpha(string[i])!=0)
{
c[j]=string[i];
j++;
}
}
puts(string);
puts(c);
}
>>
#include <stdio.h>
#include <stdlib.h>

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<strlen(string);i++){
if(isalpha(string[i])==0)
{
c[j]=string[i];
j++;
}
}
puts(string);
puts(c);
}



God damn retard.
>>
>>56373455

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.
>>
File: vscode.png (43KB, 1010x635px)
vscode.png
43KB, 1010x635px
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.
>>
>>56373455
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).

>>56373470
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.
>>
>>56373781
Ooooooh... I got it! I'm very grateful to you for taking the time to explain that to me mate, thanks a ton!

>>56373747
Never did learn how to do that actually!
 Like this? 
>>
>>56373866
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.
>>
>>56373904
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.
>>
>>56373204
wat u try do ?
Thread posts: 28
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.