I'm learning C this year in school, and I'm having trouble with this program dealing with pointers. You guys probably get this alot, but could you please help me?
The program should do the following:
The user types a series of words and the program prints the second word the user typed.
1. There is no space at the beginning
2. There is no space at the end
3. There is only one space between words
I can't get this to work using pointers(which we have to use). Here is my code.
Although I completely understand your code. I'm not sure what this is (int argc, char** argv). Why is that needed?
Name:
Anonymous2011-11-08 20:16
I notice one mistake is that getSecondWordPointer returns a pointer to a character array allocated on that function's stack. The thing is, that array will be destroyed when the function returns. You should write e.g. char *s = malloc(50);
Thanks, your seriously the best. At least I kinda had the right idea. haha
Name:
Anonymous2011-11-08 20:26
>>10
Just typical C practice. It allows for command line programs to have arguments.
Name:
Anonymous2011-11-08 20:27
>>3 print("%s") expects a string of characters, that is a char*, you are giving it a char by writing *secondWordPointer.
That is incorrect you fucking nigger. Again, for the second time, C doesn't have a string of characters. Instead, what C has are an array of characters. If this array of characters is terminated with '\0', then it becomes a string.
>>16
Great. As a final note, you should probably free(secondWordPointer); at the end of main to avoid memory leaks.
Name:
Anonymous2011-11-08 20:29
>>3
Also, take your highlighted incorrect usage and shove it straight up your ass you fucking uneducated faggot. You never have and never will amount to anything more than some general labor monkey that works form some shit no name hick firm.
Name:
Anonymous2011-11-08 20:30
>>19
Did your nigger ass even bother to look at the code? Cripes, shut up and go scrub another toilet you fucking dumb nigger bitch.
>>26 OK, but please understand that there is little semantic difference between "array" and "string".
That is totally incorrect. An array in C is an immutable lvalue. In contrast, a string can either be a mutable or immutable array of characters that get terminated with '\0'.
>>27
And is why you have a bunch of libraries in C just to handle basic string manipulations.
Name:
Anonymous2011-11-08 20:44
>>27
As an indeed uneducated person, I find that though they have special meanings in the context of C programming, their general meanings are very close.
>>29
No they aren't. And the fact that you think they are makes you that much dumber. Either learn the fundamental differences or just don't bother taking the time to learn about programming.
Name:
Anonymous2011-11-08 20:46
Anyway amidst the flames and the trolls I guess as an uneducated teenager wasting his time on 4chan I have learnt the hard way that "strings" are always null-terminated "arrays".
Name:
Anonymous2011-11-08 20:49
Besides, to go back to the stupid post that created this whole debacle, it can be said that printf("%s"); actually expects a string, because a non-null-terminated array would make it crash or print garbage
>>19 at the end of main
No, that's useless since the entire process is gone once main exits. free() is for use... deeper inside, where memory that isn't needed can be reused for something else.