PDA

View Full Version : Convert NSString to static char




dipaliP
Nov 6, 2008, 09:14 AM
I have following string
NSString *stmt = [NSString stringWithFormat:@"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.



jaleel
May 7, 2009, 01:39 AM
I have following string
NSString *stmt = [NSString stringWithFormat:@"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.

Diear brother i also facing this problem.. if you get any idea to solve this problem.. pls tell me dear......

BlackWolf
May 7, 2009, 05:26 AM
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];

robbieduncan
May 7, 2009, 05:43 AM
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];


Ah yes char is a single character, but a char* is a pointer to a character. Or the start of an array of characters...

BlackWolf
May 7, 2009, 07:17 AM
Ah yes char is a single character, but a char* is a pointer to a character. Or the start of an array of characters...

good to know, k :D

PhoneyDeveloper
May 7, 2009, 09:28 AM
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.