CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Little question about char convertion

  1. #1
    Join Date
    Apr 2007
    Posts
    2

    Little question about char convertion

    Hello!
    I've been using sprintf to convert a group of integers to a char sequence.
    like this:
    char st[];
    int a=2,b=3;
    sprintf(st,"%d and %d",a,b);
    cout<<st;

    it works good, but when trying to use char *st; instead of the array an error is thrown.
    "Error while dumping state (probably corrupted stack)"
    if sprintf receives a char * as first argument, shouldn't it work too?

  2. #2
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303

    Re: Little question about char convertion

    Are you saying you replaced
    Code:
    char st[];
    by
    Code:
    char *st;
    You haven't allocated any memory for the variable - you need to do this in both cases. I think you are lucky in the first case.

    Try
    Code:
    char st[25] = {0}; // allow plenty of room
    or
    Code:
    char *st = new char[25];
    or better yet use an STL string.

    Regards
    Alan

  3. #3
    Join Date
    Apr 2007
    Posts
    2

    Re: Little question about char convertion

    thanks for the info.
    the error is gone when the initialization is used. in both cases

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)