This code gives me the error message "warning: passing argument 1 of 'stringWithCString:encoding:' makes pointer from integer without a cast"

#import <Cocoa/Cocoa.h>
#import <Foundation/NSObject.h>
#import <stdio.h>
//#import <string.h>

@interface Numbers : NSObject
    NSString *name;
    int age;
    NSMutableArray *list;
//-(void) setName;
-(void) setAge :(int)a;


@implementation Numbers
-(void) setName{
//  NSLog(@"What is your name?");
    char n;
    printf("What is your name? ");
    scanf("%s", &n );
//  name = [NSString stringWithUTF8String:n];
    name = [NSString stringWithCString: n encoding: NSASCIIStringEncoding];

What could it be? Perhaps a header. I wish there was a better way to take input of strings. Using NSString is recommended over a regular cstring. I want to learn that because I plan on making more complex programs in the future.

4 Years
Discussion Span
Last Post by Moschops

This is broken in two different ways.

Firstly, you are using a single char (i.e. one byte only) to try to store all the user input. This is bad. You will trash memory. You need to read up on what a c-string is (i.e. an array of char) and how to use them.

Secondly, the NSString stringWithCString method wants a char pointer, and you're trying to feed it n, which is not a char pointer. It is a char. A char is not the same thing as a char pointer.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.