When I use std::string and when char* to manage arrays of chars in C++?
It seems you should use char* if performance(speed) is crucial and you're willing to accept some of a risky business because of the memory management.
Are there other scenarios to consider?
...
I'm starting to learn C by reading K&R and going through some of the exercises. After some struggling, I was finally able to complete exercise 1-19 with the code below:
/* reverse: reverse the character string s */
void reverse(char s[], int slen)
{
char tmp[slen];
int i, j;
i = 0;
j = slen - 2; /* skip '\0' and \n */
tm...
I'm looking through some code for learning purposes. I'm working through this portion of code.
// e.g. const unsigned char data={0x1,0x7C ... }
unsigned char buf[40];
memset(buf,0,40);
buf[0] = 0x52;
memcpy(buf+1, data, length); // What does buf+1 do in this situation?
On the last line where memcpy is called what does buf+1 do? buf is...
char *funcNames[]= {"VString","VChar","VArray","VData"};
for(int i=0;i<4;i++)
{
char* temp = funcNames[i];
int len = strlen(funcNames[i]);
for(int j = 0;j<len ;j++)
{
if(j!=0)
{
char arr = temp[j];
}
}
}
here i want to separate "V" from all string in char array ...and create a...
How do I write the MAC address ff:ff:ff:ff:ff:ff as a char[] in C?
Do I just do char macaddress[6] = "%0xFF%0xFF%0xFF%0xFF%0xFF%0xFF";
I'm not sure. Thanks!
...
Hi,
I've written a small program for my programming class, that uses pointers to reverse a char array. I was wondering if there is anything that I should do differently? Am I doing this correctly? Is there a more efficient way to accomplish this? Thanks!
My small program:
int main ( )
{
char buffer[80];
PrintHeader();
cout << "\nStr...
Hi there, I'm writing a string tokenization program for a homework assignment in C++, that uses pointers. However, when I run & debug it, it says that my pointer pStart, is invalid. I have a feeling that my problem resides in my param'ed constructor, I've included both the constructor and the object creation below.
I would appreciate it...
I have a a char array:
char* name = "hello";
I want to add an extension to that name to make it
hello.txt
How can I do this?
name += ".txt" won't work
...
Hi,
I just wrote a program that tokenizes a char array using pointers. The program only needed to work with a space as a delimiter character. I just turned it in and got full credit, but after turning it in I realized that this program only worked if the delimiter character was a space.
My question is, how could I make this program wor...
I have a char array which contains null characters at random locations. I tried to create an iStringStream using this array (encodedData_arr) as below,
I use this iStringStream to insert binary data(imagedata of Iplimage) to a MySQL database blob field(using MySQL Connector/C++'s setBlob(istream *is) ) it only stores the characters upt...
I'm trying to check whether or not the second argument in my program is a substring of the first argument. The problem is that it only work if the substring starts with the same letter of the string.
EDIT: It must be done in C, not C++. Sorry
int main(int argc, char **argv){
if (argc != 3) {
printf ("Usage: check <string o...
Simple question, how does one create a function which takes an unsigned char std::vector and spits out an unsigned char[] with a length. Thanks!
Ah, well it seems my problem was my knowledge of std::vector. I always believed that std::vector did not hold its values in linear fashion. That solves a lot of my problems. Thanks!
...
Hi, I wanna to write something to a .txt file in .c file, but required to name that file with the current timestamp as the postfix, just like filename_2010_08_19_20_30. So I have to define the filename char array first and process the filename by myself?Assign the character one by one?
Is there any easy way to do that?
...
I have this structure:
class Base
{
public:
void doACopy(char* strToCopy) {
strcpy(str, strToCopy);
}
private:
char str[4];
};
class Derived : public Base
{
public:
void doSomething() {
char toCopy[4];
toCopy[0] = 'a'; toCopy[1] = 'b'; toCopy[2] = 'c';
Base::doACopy(toCopy); // is there a...
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";
...