c-strings

Why do I get a compiler warning for converting a string literal to a char*, is it bad?

試著忘記壹切 提交于 2019-12-02 01:32:10
问题 So the compiler tells me this is a deprecated conversion from a string-literal to char*: char* myString = "i like declaring strings like this"; Should I be worried about this? Is this the wrong way to do this? I need to pass myString to a function that accepts a char* , who should I properly initialize the char* without this conversion? 回答1: It shouldn't even compile. If you need to pass it to function that you are sure won't change the string you need to use const cast, its one of its

Why do I get a compiler warning for converting a string literal to a char*, is it bad?

点点圈 提交于 2019-12-01 22:02:14
So the compiler tells me this is a deprecated conversion from a string-literal to char*: char* myString = "i like declaring strings like this"; Should I be worried about this? Is this the wrong way to do this? I need to pass myString to a function that accepts a char* , who should I properly initialize the char* without this conversion? It shouldn't even compile. If you need to pass it to function that you are sure won't change the string you need to use const cast, its one of its correct uses: functionName(const_cast<char *>("something")); Or if you don't want the const cast, you can copy the

Is there a neat way to do strdup() followed by strcat()?

醉酒当歌 提交于 2019-12-01 17:36:54
问题 Say I wanted to duplicate a string then concatenate a value to it. Using stl std::string, it's: string s = "hello" ; string s2 = s + " there" ; // effectively dup/cat in C: char* s = "hello" ; char* s2 = strdup( s ) ; strcat( s2, " there" ) ; // s2 is too short for this operation The only way I know to do this in C is: char* s = "hello" ; char* s2=(char*)malloc( strlen(s) + strlen( " there" ) + 1 ) ; // allocate enough space strcpy( s2, s ) ; strcat( s2, " there" ) ; Is there a more elegant

Implementing `strtok` whose delimiter has more than one character

你。 提交于 2019-12-01 12:22:36
Code snippet: char str[] = "String1::String2:String3:String4::String5"; char *deli = "::"; char *token = strtok(str,deli); while(token != NULL) { printf("Token= \"%s\"\n", token); token=strtok(NULL,deli); } The above code snippet produces the output: Token="String1" Token="String2" Token="String3" Token="String4" Token="String5" but I want the output to be: Token="String1" Token="String2:String3:String4" Token="String5" I know that I am not getting the expected output because each character in the second argument of strtok is considered as a delimiter. To get the expected output, I've written

How to use strcat() function?

纵然是瞬间 提交于 2019-12-01 10:50:10
I am very new in C language. I was trying to use strcat function. #include <stdio.h> #include <string.h> int main(int argc, const char *argv[]) { char s1[] = "12345"; char s2[] = "abcde"; strcat(s1, s2); puts(s1); puts(s2); return 0; } This one ran normally,but #include <stdio.h> #include <string.h> int main(int argc, const char *argv[]) { char* s1 = "12345"; char* s2 = "abcde"; strcat(s1, s2); puts(s1); puts(s2); return 0; } the last one failed to return a result. Why did the two different ways of declaration return different results in the strcat function. Thanks in advance. In C the

Conversion of ATL CString to character array

会有一股神秘感。 提交于 2019-12-01 06:34:54
I want to convert a CString into a char[] . Some body tell me how to do this? My code is like this : CString strCamIP1 = _T(""); char g_acCameraip[16][17]; strCamIP1 = theApp.GetProfileString(strSection, _T("IP1"), NULL); g_acCameraip[0] = strCamIP1; This seems to be along the right lines; http://msdn.microsoft.com/en-us/library/awkwbzyc.aspx CString aCString = "A string"; char myString[256]; strcpy(myString, (LPCTSTR)aString); which in your case would be along the lines of strcpy(g_acCameraip[0], (LPCTSTR)strCamIP1); From MSDN site: // Convert to a char* string from CStringA string // and

Conversion of ATL CString to character array

心不动则不痛 提交于 2019-12-01 06:15:03
问题 I want to convert a CString into a char[] . Some body tell me how to do this? My code is like this : CString strCamIP1 = _T(""); char g_acCameraip[16][17]; strCamIP1 = theApp.GetProfileString(strSection, _T("IP1"), NULL); g_acCameraip[0] = strCamIP1; 回答1: This seems to be along the right lines; http://msdn.microsoft.com/en-us/library/awkwbzyc.aspx CString aCString = "A string"; char myString[256]; strcpy(myString, (LPCTSTR)aString); which in your case would be along the lines of strcpy(g

Email validation in C++

丶灬走出姿态 提交于 2019-12-01 05:53:41
Okay so I'm trying to make a program which allows user to input their email. Their email will be considered valid if two stipulations are met: A. there must be an "@" sign somewhere in there and B. there must be a period after the "@". I got the code down for the most part, but I am having some difficulty when it comes to validating emails that have a period before the "@" sign. If they have the period before the "@" sign they are considered valid, but they shouldn't be. For example, entering text.example@randomcom is considered valid. Can anyone help me figure out what I did wrong? Thank you

Why can't I edit a char in a char*?

蓝咒 提交于 2019-12-01 04:41:53
Below is an exceedingly simple example. It compiles fine using gcc on Mac OS X (Snow Leopard). At runtime it outputs Bus error: 10. What's happening here? char* a = "abc"; a[0] = 'c'; Your code sets a to a pointer to "abc" , which is literal data that can't be modified. The Bus error occurs when your code violates this restriction, and tries to modify the value. try this instead: char a[] = "abc"; a[0] = 'c'; That creates a char array (in your program's normal data space), and copies the contents of the string literal into your array. Now you should have no trouble making changes to it. You

Email validation in C++

≯℡__Kan透↙ 提交于 2019-12-01 04:11:00
问题 Okay so I'm trying to make a program which allows user to input their email. Their email will be considered valid if two stipulations are met: A. there must be an "@" sign somewhere in there and B. there must be a period after the "@". I got the code down for the most part, but I am having some difficulty when it comes to validating emails that have a period before the "@" sign. If they have the period before the "@" sign they are considered valid, but they shouldn't be. For example, entering