CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2002
    Location
    San Miguel
    Posts
    66

    HELP: What's the problem here?

    I'm missing very basic regarding the char *. Why the program crashes when strcpy( result, Digits);

    void Add(char * result)
    {
    char Digits[] = "1234567";

    strcpy( result, Digits);
    }

    I'll appreciate your help.

    Luis F Hernandez
    EBS

  2. #2
    Join Date
    Oct 2002
    Location
    OH
    Posts
    100
    how to you call the function Add()? Is "result" pointing to something? Is it pointing to something big enough to hold the string "1234567"

    Like: Is "result" also a character array of at least 7 in length.

  3. #3

    Actually....

    The buffer needs to hold atleast 8 - 7 chars plus a null terminator, the '\0' char or 0.

  4. #4
    Join Date
    Sep 2002
    Posts
    28

    Explanation-Explicación

    When you call the function "void Add(char *result)" you
    must pass a pointer that realy points to some memory
    allocated.

    Example 1:

    char memory[8];
    Add(memory);

    Example 2:

    char *memory = new char[8];
    Add(memory);

    The size is greater than 7 because the string contains 7 digits and a null character that you don't see (7 + 1 = 8).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured