I'm trying to learn C right now but it is frustrating the hell out of me

I'm trying to learn C right now but it is frustrating the hell out of me.

Can anyone tell me why the hell if I pass this function the string "90y1n30d10h32m0s" I get this output?

why isnt it 1 1 1 1 1 1? Does it have something to do with memory?

Attached: plsg.jpg (1310x900, 373K)

Other urls found in this thread:

wiki.c2.com/?ThreeStarProgrammer
c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=aryptr
lkml.org/lkml/2015/9/3/428
twitter.com/NSFWRedditImage

change your function so you pass the size of the array as a param. You can't do sizeof(x)/sizeof(x[0]) if all you have is a pointer to a string, it only works in scope. You could also import string.h and use strlen().
also why is your function returning a bool? Have it be void if it always returns true.

Thank you so much man.

I was just testing its functionality here with the print statement, I want it to return true if the all of the counts are equal to 1.

cool. Once you get it working, swap out all those ifs for a switch statement.

Sizeof(char*)=sizeof(void*)=4or8 depending by arch (32/64bits)
Your loop ends after 8 characters
If your array is less than 8 it may crash

> sizeof(valarray)/ sizeof(valarray[0])

What are you trying to do ?
You know that valarray is a pointer and a sizeof of it will return the size of the address space ?

if you want to scan a list just use

while(valarray[i] != NULL) {
...
}

Good idea dawg

can't believe coming here resulted in help from people who didn't call me a faggot. What a delightful surprise

ops I meant '\0' instead of NULL

What if your array doesn't have a terminator?

I actually tried this before the for loop, but had some difficulties, for some reason if I tried to print each character of the array it would always print 1
as the last character (if i used "90y1n30d10h32m0s" as the input ) which screwed up the results of my function.

Since im here, anyone have any idea why that would be?

is it because of ?

sizeof(pointer) returns a size of actual pointer, not the underlying data.
In contrast to sizeof(array) (array is declared as char arr[] or explicitly sized char arr[16]) where sizeof returns number of bytes of data. You can only use this in the same scope where the array is declared.
When passing to function as argument, it decoys to pointer. This is kinda ugly and unintuitive part of C, but once you wrap your head around it, it's consistent and expected behavior.

The way you used it is: sizeof(pointer) is 8 on your architecture, sizeof(char) is 1. So you only iterate over "90y1n30d" (with potential overflow on smaller inputs).

You can call strlen(val_array) (from string.h header). But careful when using it in for loop, the comparison happens on every call and when you mutate the data in some way, compiler can't assume you haven't changed the length and will really call strlen on every iteration. So better call it in advance:
size_t len = strlen(val_array);
int *_cout = 0, ...;

for (int i = 0; i < len; i++) {
switch (val_array[i]) {
case 'y':
y_count++;
break;
case 'n':
n_count++;
break;
...
}
}
....

char* x = "abc" is actually {'a', 'b', 'c', '\0'} but readonly, so the terminator is there. Not sure why your problem is happening

C is such a beautiful language.

Then it doesn't comply with the string API.

i meant, what if your array is not char but some other type. How should you know when you are looking at the last element?

everyone is so nice too.

last time I came here for Python advice and I got absolute roasted by everyone.

that's what happens when you use a stupid language

passing the size with it everywhere

You're alright, fag.

what font do you use its nice

you don't need i, you don't need strlen
just increase val_array till it is 0

4K users take it easy, here I have to use a font that looks good at low dpi

Menlo Regular, 12pt

should be learning C++ or C#

consider learning an easier/simpler/higher level language first before learning c

C is as simple as it gets.
What we see here is user having very bad point of entry for learning and having almost no basic foundations.

I started learning C yesterday

Sorry I am struggling with pointers,

I have a decent foundation with Python and Ruby

i am brainlet and took me many months to understand pointers. C was my first language but i still look back and sigh

assembly is even simpler, doesnt mean op should start learning assembly language

in a few days you will be using 5 star pointers

faggot

Don't lie to the boy.

Pointers are silly but you'll get the hang of it.

wiki.c2.com/?ThreeStarProgrammer

He's not going five stars at any point though

everything in memory has an address where it's located. a pointer is simply a variable that contains a memory address. pointers are useful because you can "dereference" them and access the memory location stored in them. it took me a little time to wrap my head around it a long time ago, but it makes sense once you start using them in the appropriate contexts.

that's not C faggot.

Yes it is.

>Yes it is.

Attached: nobrain2.jpg (645x729, 135K)

And why don't you explain to the class why that is not C?
If you bring up C89, you're a retard.

Its the same shit, NULL='\0'=0

#define NULL ((void*)0)
it is slightly different in C

>three star programmer
Unnecessary and vulnerable to exploits and even crashes.
It may look cool on obfuscation contests but not on production software

>c
>bool
>true

was introduced in C99.

>everyone posting brainlet pics is himself a brainlet confirmed

in the for loop after each if statement you want to do continue; for better performance

>not else if
Found the brainlet.

Use a histogram.

char hist[255] = { 0 };

for (i = 0; i < array_size; i++)
hist[array[i]]++;

c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=aryptr
lkml.org/lkml/2015/9/3/428

ANSI C is the One True C. C99 is a false C and you are a heretic if you use it.

dunno, c99 is so comfy

>mfw i gnu C

Attached: pdmsi.jpg (960x960, 180K)