![]() you didn't check the return code of putchar(). Some folks will get onto you for not using with your if statements if(*pointer = '\n') putchar('\n') īut wait there's more. You neglect to check the return code of printf or indicate that you are deliberately ignoring it. Depending on your compiler and compiler arguments, it can result in a compilation error. Your use of int i=0 in the for loop is C99 specific. there are the following: char *pointer = first Lastly, in this amazingly bad piece of code, which really isn't worth mentioning, is relevant to neither strcpy() nor strncpy(), yet seems to have earned me the disfavor of who seems to write very verbose and thoughtful posts. Second, you're protecting the destination from buffer overflow, so specifying its size is appropriate. The indices are only 0-9 sentence2 = '\0' You could have learnt all this about strcpy and strncpy by reading their man pages, or their definitions in a C standard draft, etc. If you don't have a book, do look up online documentation for standard functions anyway. K&R 2 is a decent starting place if you don't have any other. Learning from a book is by far and away the best idea. For example it depends on what lies in memory after sentence1 as to whether your strcpy would step on any other variable's toes or not. ![]() It might appear to work, and then fail later on. The problem is that if you write bad code, you may never know. Learning C by trial and error is not good. If you want to put a null-terminated string into a buffer that might be too small, use snprintf. Generally speaking, strncpy is almost never a good idea. If you want to use strncpy you must also put a null terminator on (and there must be enough space for that terminator), unless you are sure that strlen(second) < sizeof sentence2. The strncpy function will always write exactly that many bytes. The third argument to strncpy is supposed to be the size of the destination. shouldn't I specify the sizeof(source) instead of sizeof(destination)? ![]() strncpy(sentence2, second, sizeof(sentence2)) So you would need char sentence1 or more. This doesn't work because first has 11 characters: the ten in the string, plus the null terminator. In this one, I'm trying to learn the string library in c. ![]() The next time I need to look up something, I just look at my own code in my files. I write code and comment all I know about it so that Strncpy(sentence2, second, 10) //shouldn't I specify the sizeof(source) instead of sizeof(destination).įor(int i =0 i < 500 i++) //Why does it crashes without this nonsensical loop?! Printf("strcmp(second, first) = %d\n", strcmp(second, first)) //returns a positive when the first different char is greater in first string.(M=77 S=83) Printf("strcmp(first, second) = %d\n", strcmp(first, second)) //returns a negative when the first differenet char is less in first string. Printf("strcmp(first, first) = %d\n", strcmp(first, first)) //returns 0 when both strings are identical. Puts("\n\n-main3 reporting for duty!-\n") ![]() Sentence2 = '\0' //Is this okay since strncpy does not provide the null character. Strncpy(sentence2, second, sizeof(sentence2)) //shouldn't I specify the sizeof(source) instead of sizeof(destination)? ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |