Gaojian922188
Gaojian922188

Reputation: 503

iphone ,how to print the char array

I have a char array,char contract[8];and assign the value to array, and I want to print the value,so I use NSLog(@"%@",contract);and build succeeded.but run incorrect.

Upvotes: 2

Views: 4654

Answers (4)

esmirnov
esmirnov

Reputation: 376

If you know the size

NSLog(@"%.*s", 8, contract);

If contract is a NULL-terminated string

NSLog(@"%s", contract);

or just convert to NSString

NSLog(@"%@", [[[NSString alloc] initWithBytesNoCopy:contract length:contractLen encoding:NSASCIIStringEncoding freeWhenDone:NO] autorelease]);

Upvotes: 3

Kathiresan Murugan
Kathiresan Murugan

Reputation: 2962

for (Char *string in myArray) {
    NSLog(@"%@", string);
}

Upvotes: 0

Tudor
Tudor

Reputation: 4174

Try

NSLog(@"%@",[NSString stringWithCString:contract encoding:NSUTF8StringEncoding]);

Basically, you need to make the C string an NSString object.

Upvotes: 7

visakh7
visakh7

Reputation: 26400

Please go through Format Specifiers. Try %c instead of %@. Hope this helps

Upvotes: -2

Related Questions