Convert NSString to static char

Discussion in 'iOS Programming' started by dipaliP, Nov 6, 2008.

  1. dipaliP macrumors newbie

    Oct 6, 2008
    I have following string
    NSString *stmt = [NSString stringWithFormat:mad:"INSERT INTO table (.....) VALUES (....), fields, values];
    now i want to store this string into 'static char' variable as i want to insert some values into a table using sqlite3.
    I am using following to convert string to 'const char'
    const char *sql = [stmt UTF8String];
    but i haven't get any idea to convert string to static char.

    Any suggestion is acceptable.
    Thanks in advance.
  2. jaleel macrumors newbie

    May 6, 2009
    Diear brother i also facing this problem.. if you get any idea to solve this problem.. pls tell me dear......
  3. BlackWolf macrumors regular

    Apr 9, 2009
    call me stupid, but isn't a CHAR(acter) only ONE character. so you can only store an entire string in multiple char-variables, not in one, can you?

    what does work is
    unichar something = [yourString characterAtIndex:0];
  4. robbieduncan Moderator emeritus


    Jul 24, 2002
    Ah yes char is a single character, but a char* is a pointer to a character. Or the start of an array of characters...
  5. BlackWolf macrumors regular

    Apr 9, 2009
    good to know, k :D
  6. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    First, let me recommend that you don't do that. Store your string as an NSString and convert it to const char * with [yourString UTF8String] at the moment that you use it. Why do you want to 'convert string to static char'?

    Second, if you really want to store a char array then that's easy. Allocate your char array, with malloc or on the stack or as a global, and then strcpy the bytes from [yourString UTF8String].

    strcpy(mycharbuffer, [yourString UTF8String]);
    Third, use FMDB for sqlite access. It will make your life easier.

Share This Page