Convert NSString to static char

Discussion in 'iPhone/iPad Programming' started by dipaliP, Nov 6, 2008.

  1. macrumors newbie

    Joined:
    Oct 6, 2008
    #1
    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. macrumors newbie

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

    Joined:
    Apr 9, 2009
    #3
    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
    Code:
    unichar something = [yourString characterAtIndex:0];
    
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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. macrumors regular

    Joined:
    Apr 9, 2009
    #5
    good to know, k :D
     
  6. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    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].

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

Share This Page