177 questions
0 votes
1 answer
76 views
char* vs std::string.data()
I'm using a Azure IoT Hub to send and receive messages to and from a device. To do this, I'm utilizing a library written in C and my application is written in C++. When I convert my std::string to a ...
0 votes
0 answers
35 views
why does putting a null char in string give error in c [duplicate]
im trying to run this code #include <stdio.h> #include <string.h> #if defined(_WIN32) #define SEPERATOR '\\' #elif defined(__unix__) || defined(__linux__) #define SEPERATOR '/' #endif ...
3 votes
4 answers
132 views
Receiving a char array to a void pointer shows different behavior
I am attempting to develop a method to customize strcpy. I considered two approaches for this implementation. One method involves using memcpy, while the other involves incrementing the pointer and ...
2 votes
3 answers
401 views
How to join compile-time string-like objects while keeping the API simple?
I am trying to concatenate string-like objects at compile-time. With the help of this post, I came up with something like this: #include <cstddef> #include <utility> #include <algorithm&...
0 votes
1 answer
112 views
Char pointer member being replaced with "\320" unintentionally when building a struct pointer
I am building a command line parser in C, where I take a command line input and organize it into a struct pipeline. The struct has a commands member that contains a struct that has the first command, ...
0 votes
0 answers
60 views
Two different char pointers end up with the same value [duplicate]
I'm assuming this is undefined behavior, but I'm not exactly sure why? When I compile the above code with g++ 4.4.7 and execute it: #include <iostream> #include <string> using namespace ...
1 vote
2 answers
103 views
how literals in c are stored when defining with both character array declaration and character pointer declaration?
I read that while using character pointer declaration first the string literal is getting stored in static storage and then pointer to it is returned. but is same happening while using literals in ...
1 vote
0 answers
226 views
C++20 changes reading into char arrays with `operator>>` - How to fix this?
EDIT: To summarize from the comments (before I close the topic): the issue has been discussed here previously: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0487r1.html The resolution ...
0 votes
5 answers
246 views
How do I modify a char** in another function
This is what I expect my string array s to be after the program is run: {"#0", "#1", "2"}. This is what I am getting: {"#2", "#2", "2"}. How ...
-1 votes
1 answer
2k views
How to use Arduino's Serial.print with char* and const char* types
I require to build a simple Arduino function that returns either "char*" or "const char*" type and then I need to print that value. However, I'm facing a problem: when I try to ...
0 votes
2 answers
57 views
Getting a list of strings from a function without dynamic memory
I am writing a C function GetDeviceList() which must return a list of device names found as strings somehow. The number of devices found and of course the device names themselves will vary each time ...
2 votes
4 answers
1k views
Is String Literal in C really not modifiable?
As far as I know, a string literal can't be modified for example: char* a = "abc"; a[0] = 'c'; That would not work since string literal is read-only. I can only modify it if: char a[] = &...
1 vote
1 answer
207 views
C: String Variable Loses Value After Returning From Void Function
hope you are well. I recently started learning ADT (Abstract Data Type) in college, and I have an assignment that states the following: Complete ADTDate adding the following primitive function: void ...
0 votes
1 answer
57 views
Frist few bytes of a char* are corrupted after creating it with malloc()
Whenever I create a char* with malloc(), the first few bytes get random data, which is different every time I compile the code. In this case, I wanted to concatenate 2 char* to create 1 char*. char *...
3 votes
2 answers
420 views
How does memory allocation work with char pointers(string literals, arrays)?
Currently reading K&R and just got stumbled across the char pointers. There's nothing about memory allocation when defining char pointers in the book rn, maybe it'll be explained later. But it ...