c-strings

Why use c strings in c++?

Is there any good reason to use C-strings in C++ nowadays? My textbook uses them in examples at some points, and I really feel like it would be easier just to use a std::string. ...

Why does simple C code receive segmentation fault?

The following code receives seg fault on line 2: char *str = "string"; str[0] = 'z'; printf("%s", str); While this works perfectly well: char str[] = "string"; str[0] = 'z'; printf("%s", str); Tested with MSVC and GCC. ...

Why does c_str() print the string twice?

So... when I go: cout<<stringName<<endl; I get: NT But when I go: cout<<stringName.c_str()<<endl; I get: NTNT Why? ...

converting char** to char* or char

I have a old program in which some library function is used and i dont have that library. So I am writing that program using libraries of c++. In that old code some function is there which is called like this *string = newstrdup("Some string goes here"); the string variable is declared as char **string; What he may be doing in that f...

Are strtol, strtod unsafe?

It seems that strtol() and strtod() effectively allow (and force) you to cast away constness in a string: #include <stdlib.h> #include <stdio.h> int main() { const char *foo = "Hello, world!"; char *bar; strtol(foo, &bar, 10); // or strtod(foo, &bar); printf("%d\n", foo == bar); // prints "1"! they're equal *bar = 'X'; // seg...

char array vs. char pointer

Hey, When receiving data through a socket using recv, I've noticed that, with: char buffer[4]; memset(buffer, 0, 4); recv(socket, buffer, 4, 0); I receive mesgx�� "mesg" being what I sent, with some random characters appended. If I use char * method = (char *) malloc(4); memset(buffer, 0, 4); recv(socket, buffer, 4, 0); ...

Is sprintf(buffer, "%s […]", buffer, […]) safe?

I saw use of this pattern to concatenate onto a string in some code I was working on: sprintf(buffer, "%s <input type='file' name='%s' />\r\n", buffer, id); sprintf(buffer, "%s</td>", buffer); and I'm fairly certain it's not safe C. You'll notice that buffer is both the output and the first input. Apart from the obvious possibility o...

Writing into c-string

Hi, my code segfaults and I don't know why. 1 #include <stdio.h> 2 3 void overwrite(char str[], char x) { 4 int i; 5 for (i = 0; str[i] != '\0'; i++) 6 str[i] = x; 7 } 8 9 int main(void) { 10 char *s = "abcde"; 11 char x = 'X'; 12 overwrite(s, x); 13 printf("%s\n", s); 14 return 0; 15 } The gdb ...

Are c styled strings safe?

In c/c++ some people use c-styled strings like: char *str = "This is a c-styled string"; My question is is this safe? The way I see it is they created a char pointer that points to the first letter of a const array of chars, but can't some other thing eg another variable overwrite a portion of the char array in the memory? Thus causin...

C: Missing some logic with the pointers stuff...

I am writing my own string copy function. The following works: char *src, *dest; src = (char *) malloc(BUFFSIZE); //Do something to fill the src dest = (char *) malloc(strlen(src) + 1); mystringcpy(src, dest); void mystringcopy(char *src, char *dest) { for(; (*dest = *src) != '\0'; ++src, +dest); } But this doesn't work: char *sr...

converting c style string to c++ style string

Hi, Can anyone please tell me how to convert a C style string (i.e a char* ) to a c++ style string (i.e. std::string) in a C++ program? Thanks a lot. ...

Problems with this stack implementation

where is the mistake? My code here: typedef struct _box { char *dados; struct _box * proximo; } Box; typedef struct _pilha { Box * topo; }Stack; void Push(Stack *p, char * algo) { Box *caixa; if (!p) { exit(1); } caixa = (Box *) calloc(1, sizeof(Box)); caix...

Can a std::string contain embedded nulls?

For regular C strings, a NULL signifies the end of data. What about std::string, can I have a string with embedded NULLS? ...

Accessing/modifying an array of strings in a structure

Suppose I have the following code: typedef struct { char **p; } STRUCT; int main() { STRUCT s; *(s.p) = "hello"; printf("%s\n", *(s.p)); return 0; } which obviously doesn't work, but it should show what I want to do. How would I go about initialising, accessing, printing, etc the array of strings in the structure...

In C, can I initialize a string in a pointer declaration the same way I can initialize a string in a char array declaration?

Do these two lines of code achieve the same result? If I had these lines in a function, is the string stored on the stack in both cases? Is there a strong reason why I should use one over the other, aside from not needing to declare the null terminator in the first line of code? char s[] = "string"; char* s = "string\0"; ...