simple char array, please help

Started by
15 comments, last by rip-off 10 years, 9 months ago

Which compiler are you using? It may have snprintf(), but with a different spelling. Ex: MSVC calls it _snprintf().

Advertisement

But my c compiler doesn't seem to support snprintf. What alternative do i have?

If you're using GCC/Clang, you can turn on C99 mode (default is C89, I believe). snprintf is part of the C99 standard, not the C89 standard. To turn on C99 mode with GCC or Clang, just pass the -std=c99 flag to the compiler when compiling. Or change your IDE to use C99 mode if you're using an IDE.

If you're using MSVC... it's a bit tricker, see below.

Just use memcpy.


You should try to avoid the printf functions if you don't need the advanced formatting they provide.

a) Unless you have good reason to, don't worry about premature optimization. b) memcpy isn't necessarily a good replacement. I specifically used snprintf because it always null terminates the string. Always. memcpy does not, nor does strncpy. Yes, he can use memcpy, but he would have to 1) determine how long the source buffer is, 2) determine how long the dest buffer is, 3) pick the min of the two, 4) ensure the copied string is null terminated. It's certainly possible to use memcpy, but judging by his skill level, I think it's more worthwhile to suggest things that are less likely to lead him to screwing up (or at the very least, pointing out the caveats of alternative methods).

Which compiler are you using? It may have snprintf(), but with a different spelling. Ex: MSVC calls it _snprintf().

It should also be pointed out that there are a few gotchas if you use MSVC's _snprintf. Most notably, _snprintf does not necessarily null terminate the string (like snprintf always does). Additionally, the return values are different. Before anyone uses _snprintf, they should first read this.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

As SiCrane stated, your buffer will get invalid (out of scope) once the function return. It might work, and it might not... Use something like this instead:


void getNetworkAtPriority(char *firstArgument, char *pBuf)
{
    sprintf(pBuf, "mcc-mnc-umts");
}

int main()
{
    char entry1[14];
    ZeroMemory(&entry1[0], 14);

    getNetworkAtPriority("1", &entry1[0]);

    return 0;
}



Guard that against buffer overrun!

When you supply a buffer pointer to a function you should always provide the length of the buffer as well, and ensure that the function does not write beyond the end of the buffer.


int stuffToBuff(char* buffer, size_t bufferLength) {
  char thingToCopy[] = "Hello, world!";
  if(strlen(thingToCopy) >= bufferLength) {
    return -1; //indicate failure
  }
  strcpy(buffer, thingToCopy);
  return 0; //indicate success
}

int main() {
  char buf[20];
  int result = stuffToBuff(buf, 20);
  if(result == -1) {
    //there was an error!
  }
  return 0;
}
Remember that C-style strings require a zero at the end, so your buffer needs to be one longer than the number of characters being copied.

Yea, i though about that, but i didn't add it because i though it would obscure the important parts...

btw, shouldn't

if(strlen(thingToCopy) >= bufferLength) {

be

if(strlen(thingToCopy) <= bufferLength) {

???

I think you made a typo...

btw, shouldn't

if(strlen(thingToCopy) >= bufferLength) {

be

if(strlen(thingToCopy) <= bufferLength) {

???

I think you made a typo...

No, (strlen(thingToCopy) >= bufferLength) is correct. It's checking if the length of the string that needs to be copied is longer than the buffer, and if so, returns an error. You can read it as "If the string is longer than the buffer, return -1 to indicate failure."

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

Cornstalks is correct.

I'd return the number of bytes copied rather than 0 from the function though to indicate success. It will be useful if you are appending strings (don't need to call strlen again).

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Oh lol, my bad, i didn't read the code carefully, im used to do the opposite (check if the string lenght is smaller than buffer size, then return -1 if not, or whatever)

@musafir2007, do not remove or replace questions, it makes the thread very confusing to follow. You can follow up in a separate reply. If you're making more than a minor edit, it is courtesy to indicate the nature of the edit, or as Vortez did, to use strike through to indicate the old post. But this behaviour is generally more suitable for replies, where you might otherwise be leaving misleading information, than questions.

I have restored a hybrid post from the various questions you appear to have asked.

This topic is closed to new replies.

Advertisement