Hello World!
If now i want to take the 3 first chars in a string, to compare them to something else.
How do i easiest only get those 3 chars of the string when i want to compare?
Hello World!
If now i want to take the 3 first chars in a string, to compare them to something else.
How do i easiest only get those 3 chars of the string when i want to compare?
Code:const char* s1 = "Hello world"; char s2[4]; //3 - You want 3 characters //1 - The NULL char //4 = 3 + 1 strcpy(s2, s1); if(strcmp(s2, "Hel")) printf("OK");
Oh, it looks like audinue's example is incorrect. Using strcpy() in that way will result in a buffer overrun. You should use strncpy() instead, e.g.,
EDIT:Code:const char* s1 = "Hello world"; char s2[4]; //3 - You want 3 characters //1 - The NULL char //4 = 3 + 1 strncpy(s2, s1, 3); s2[3] = '\0'; if(strcmp(s2, "Hel")) printf("OK");
Then again, there is strncmp(), so life is easier than that:
Code:const char* s1 = "Hello world"; if (strncmp(s1, "Hel", 3) == 0) { printf("There's a match!\n"); }
Last edited by laserlight; 07-05-2008 at 03:50 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Sorry, revision:
You're right. We must add the NULL char in the end of the string.Code:char *s1 = "1234567"; char *s2 = malloc(4 * sizeof(char)); memcpy(s2, s1, 3); s2[3] = '\0'; if(strcmp(s2, "123") == 0) { printf("'%s' is identical to '123'.", s2); }
strcpy function
Purpose:
Copies a string.
Syntax:
char * strcpy(char * restrict targetstring, const char * restrict sourcestring);
Declared in:
<string.h> (strcpy)
The function copies sourcestring, including the terminating null character ('\0'), to targetstring. The behavior is undefined if the two strings overlap.
Returns:
The argument targetstring.
Conclusion:
This function copies the whole string.
Last edited by audinue; 07-05-2008 at 03:47 PM.
Except that now you changed s1 from const char* to char* when you should have left it aloneSorry, revision:
Also, note that sizeof(char) is always 1. Anyway, I think that strncmp() works best here.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Wondeful, thanks laserlight!
Btw is there function like strrncmp
Compare the right-part of the string?
Not in the standard library, but it is not too difficult to write one.Btw is there function like strrncmp
Compare the right-part of the string?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
What happens without the NULL.
Will it pickup data from memory until next NULL?
And when i do the:
Get: warning: assignment makes integer from pointer without a castCode:str2[3] = "\0";
But when i instead off "\0", is using ascii:
its working great.Code:str2[3] = 0;
Why?
Last edited by MKirstensen; 07-05-2008 at 04:55 PM.
Because double quotes is a way of writing short-hand arrays, ie
is the same asCode:char test[6] = "hello";
Notice it's nul terminated, thusCode:char test[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
is reallyCode:str[3] = "\0";
Which of course won't work as, str2[3] is a single character. And it's also a string literal (that is stored in read-only memory somewhere thus the pointer cast error). So you probably wantCode:str[3] = {'\0', '\0'};
Where single quotes are represent an ASCII constant (evaluated to a number anyway, in this case 0 -- look up an ASCII table).Code:str2[3] = '\0';
As per the other thread, you really need to get yourself a book.
I believe the behavior is undefined. On many or most systems, it depends upon memory access restrictions put in place by the OS, compiler, etc. etc..
And when you're talking about the nul terminator it's nul or NUL not NULL
No, it is called a "null character", according to C99, and consequently it can be called a "null terminator". Checking my English dictionary, I can tell you that "nul" is not a word. If anything, it is an abbreviation for "null". Of course, it is bad to call it a "NULL terminator" since that may be confused with the macro NULL, though '\0' and NULL do have the same integral value.And when you're talking about the nul terminator it's nul or NUL not NULL
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)