c-strings

cannot copy command line argument to character pointer in C [duplicate]

做~自己de王妃 提交于 2020-03-05 00:22:40
问题 This question already has answers here : 'strcpy' with 'malloc'? (6 answers) Closed 18 days ago . I am taking command line argument which I am copying to my character pointer. but its giving me an error. int main(int argc, char *argv[]) { char *cmdarg; if(argc>1) strcpy(cmdarg, argv[1]); else cmdarg = NULL; return 0; } This gives me Segmentation fault (core dumped) 回答1: You did not allocate memory where you are going to copy the argument pointed to by the expression argv[1] . Try the

How can I check if a string has special characters in C++ effectively?

杀马特。学长 韩版系。学妹 提交于 2020-02-18 14:01:12
问题 I am trying to find if there is better way to check if the string has special characters. In my case, anything other than alphanumeric and a '_' is considered a special character. Currently, I have a string that contains special characters such as std::string = "!@#$%^&". I then use the std::find_first_of () algorithm to check if any of the special characters are present in the string. I was wondering how to do it based on whitelisting. I want to specify the lowercase/uppercase characters,

What happens when a char array gets initialized from a string literal?

梦想的初衷 提交于 2020-02-03 08:15:49
问题 As I understand it, the following code works like so: char* cptr = "Hello World"; "Hello World" lives in the .rodata section of the program's memory. The string literal "Hello World" returns a pointer to the base address of the string, or the address of the first element in the so-called "array", since the chars are laid out sequentially in memory it would be the 'H'. This is my little diagram as I visualize the string literal getting stored in the memory: 0x4 : 'H' 0x5 : 'e' 0x6 : 'l' 0x6 :

Why/when to include terminating '\0' character for C Strings?

拈花ヽ惹草 提交于 2020-01-31 20:12:27
问题 I'm very new to C and am a bit confused as to when we need to manually add the terminating '\0' character to strings. Given this function to calculate string length (for clarity's sake): int stringLength(char string[]) { int i = 0; while (string[i] != '\0') { i++; } return i; } which calculates the string's length based on the null terminating character. So, using the following cases, what is the role of the '\0' character, if any? Case 1: char * stack1 = "stack"; printf("WORD %s\n", stack1);

Why/when to include terminating '\0' character for C Strings?

耗尽温柔 提交于 2020-01-31 20:11:59
问题 I'm very new to C and am a bit confused as to when we need to manually add the terminating '\0' character to strings. Given this function to calculate string length (for clarity's sake): int stringLength(char string[]) { int i = 0; while (string[i] != '\0') { i++; } return i; } which calculates the string's length based on the null terminating character. So, using the following cases, what is the role of the '\0' character, if any? Case 1: char * stack1 = "stack"; printf("WORD %s\n", stack1);

Concatenating char buffer into a string

自作多情 提交于 2020-01-30 10:56:54
问题 In the following snippet, I receive the data until I have completely received all the data from the socket client. I keep on storing the data in a char buffer of size 300. ssize_t b; char buffer[300] while((b = recv(socket_fd,buffer,sizeof(buffer))) > 0) { // keep on receiving the data } But the problem is on each iteration, buffer gets refilled and old values are lost. How do I concatenate the buffer values into one, so that at the end of the loop I get the message as one complete string?

C string through dll boundaries

不想你离开。 提交于 2020-01-25 07:50:10
问题 We need to return a C string through dll boundaries in a safe way! How can we do this? My idea: extern "C" __declspec(dllexport) const char *const Api { ... static const char *output = result.c_str(); return output; } 回答1: A pointer is being returned in your example code, but the memory region it is pointing to is probably not staying resident. If the result variable (std::string?) is on the stack, it's going to be destroyed when the function returns and the returned pointer will be dangling