0

Is there a better, shorter, easier to read version of the following code:

char ar[100];

int main() {
    //ar = "hello"; doesn't compile
    ar[0] = 'h';
    ar[1] = 'e';
    ar[2] = 'l';
    ar[3] = 'l';
    ar[4] = 'o';
    ar[5] = '\x00';
    return 0;
}

Note: The type of ar has to be char[100]. The "real" program is:

#include <string>
#include <cstdarg>

#define TO_STRING_BUF_SIZE 100

char toStringBuf[TO_STRING_BUF_SIZE];

std::string toCptr_(const char * format, ...) {
  va_list argzeiger;
  va_start(argzeiger, format);
  int16_t ret = vsnprintf(toStringBuf, TO_STRING_BUF_SIZE, format, argzeiger);
  if(ret >= TO_STRING_BUF_SIZE - 1) {
    //toStringBuf = "buffer too small";
  } else if(ret < 0) {
    //toStringBuf = "encoding error";
  }
  va_end(argzeiger);
  std::string returning(toStringBuf);
  return returning;
}
4
  • 4
    So what is the problem? The dummy code or your actual code? If the actual code has a problem where and what is it? Aug 31, 2016 at 18:18
  • ar = "hello" is a syntax error. Try strcpy Aug 31, 2016 at 18:19
  • 3
    "The type of ar has to be char[100]" Only if your professor or TA says so, otherwise there's no reason to use a bare char array. Aug 31, 2016 at 18:25
  • Totally agree about the professor TA saying this to you as a "reason" (in quotes as you see). Or the other reason is that you just believe that you can only do whatever you want to do with a char array? Aug 31, 2016 at 18:26

3 Answers 3

6

Use the strcpy function to copy a C-style string.

strcpy(ar, "hello");
0

As others have stated, you can use strcpy()/strncpy() to copy a string into a char[] buffer.

However, that is not actually necessary in this example. Your function returns a std::string, so you don't actually need a char[] at all. It is a waste of memory for short messages, a limitation for long messages, and a thread-unsafe condition (unless you use a local buffer instead of a global buffer). Try this instead:

#include <string>
#include <cstdarg>

std::string toCptr_(const char * format, ...)
{
    std::string str;

    va_list argzeiger;
    va_start(argzeiger, format);

    int ret = vsnprintf(NULL, 0, format, argzeiger);
    if (ret < 0) {
        str = "encoding error";
    }
    else {
        str.resize(ret+1);
        vsnprintf(&str[0], str.size(), format, argzeiger);
        str.resize(ret);
    }

    va_end(argzeiger);

    return str;
}

Or, at the very least:

#include <string>
#include <cstdarg>

#define TO_STRING_BUF_SIZE 100

std::string toCptr_(const char * format, ...)
{
    char toStringBuf[TO_STRING_BUF_SIZE];
    std::string str;

    va_list argzeiger;
    va_start(argzeiger, format);

    int ret = vsnprintf(toStringBuf, TO_STRING_BUF_SIZE, format, argzeiger);
    if (ret < 0) {
        str = "encoding error";
    }
    else if (ret < TO_STRING_BUF_SIZE) {
        str.assign(toStringBuf, ret);
    }
    else {
        str.resize(ret+1);
        vsnprintf(&str[0], str.size(), format, argzeiger);
        str.resize(ret);
    }

    va_end(argzeiger);

    return str;
}
-1

Use strncpy(), eg:

strncpy(toStringBuf, str_buf, TO_STRING_BUF_SIZE);
toStringBuf[TO_STRING_BUF_SIZE-1] = 0;

Don't use strcpy(), as it is one of the most classically well-known unsafe functions in C/C++. It can write past the end of the buffer, causing buffer overflow errors. Not a problem in your particular example, but it should still be avoided in general.

8
  • 2
    This is bad advice. See here, among other places. The strncpy function was meant for fixed-length structures inside other structures, not for C-style strings. Aug 31, 2016 at 18:30
  • i agree that c strings are crappy as the article implies, but strncpy is still better than just strcpy. that's like saying you should not use a seatbelt because only airbags are safe. seat belts are still better than no seat belt. Aug 31, 2016 at 18:39
  • 1
    Thank you, but shouldn't it be strncpy(str2, str1, str2bufsize) instead of len(str1)? In my example: strncpy(toStringBuf, "buffer too small", TO_STRING_BUF_SIZE) ; Aug 31, 2016 at 19:19
  • @Julius Your answer is being read by someone who doesn't know how to use strcpy. They're not going to get that nuance unless you tell them. Aug 31, 2016 at 21:30
  • 1
    So when copying that error message with strncpy, if the buffer is too small you get an unterminated string. How, exactly, is that "safe"? Sep 1, 2016 at 1:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.