Objective c EXC_错误_访问错误,目标C问题
嘿,我对Objective-C和Cocoa编程都是新手。。无论如何,我的代码看起来像:Objective c EXC_错误_访问错误,目标C问题,objective-c,cocoa,exc-bad-access,Objective C,Cocoa,Exc Bad Access,嘿,我对Objective-C和Cocoa编程都是新手。。无论如何,我的代码看起来像: int main(int argc, char *argv[]) { printf("how many address would you like to input?\n"); int numAddresses; scanf("%i", &numAddresses); char *inputString; NSMutableArray * arrayOfBooks = [NSMutableArra
int main(int argc, char *argv[])
{
printf("how many address would you like to input?\n");
int numAddresses;
scanf("%i", &numAddresses);
char *inputString;
NSMutableArray * arrayOfBooks = [NSMutableArray array];
for (int i = 0; i < numAddresses; ++i) {
book * f = [[book alloc] init];
printf("Please input the name of contact %i\n", i+1);
scanf("%s",inputString);
[f setName: inputString];
printf("Please input the address of contact %i\n", i+1);
scanf("%s", inputString);
[f setAddress: inputString];
printf("Please input the birthday of contact %i\n", i+1);
scanf("%s", inputString);
[f setBirthday: inputString];
printf("Please input the phone number of contact %i\n", i+1);
scanf("%s", inputString);
[f setPhoneNumber: inputString];
[f print];
[arrayOfBooks addObject:f];
[f release];
}
for(int i = 0; i < numAddresses; i++){
[arrayOfBooks[i] print];
我基本上只是在做一个通讯录。当我输入名字时,它抛出一个“EXC_BAD_ACCESS”错误。为什么?
#import "book.h"
@implementation book
-(void) setName: (char*) nameInput{
name = nameInput;
}
-(void) setAddress: (char*) addressInput{
address = addressInput;
}
编辑:我不再得到错误。。但现在我有了一个新问题。它会提示我输入姓名,然后在我有机会做任何事情之前立即提示我输入地址。为什么会发生这种情况?“inputstring”只是一个字符*-它是未分配的,并且没有与之关联的存储
您必须向它传递一个指向足够大的数组的指针,或者使用“a”修饰符,并传递一个指向char的指针,以便scanf为您分配
请参见scanf上的手册页“inputstring”只是一个字符*-未分配,没有与之关联的存储
您必须向它传递一个指向足够大的数组的指针,或者使用“a”修饰符,并传递一个指向char的指针,以便scanf为您分配
查看scanf上的手册页一个好主意是使用而不是scanf()
。任何空格(空格、制表符、换行符等)都将导致scanf()
停止解析,而基于您试图获取的输入的解析可能不是您想要的
像这样使用fgets()
:
char lineBuffer[100]; // or some number large enough
puts("Enter something:");
if (fgets(lineBuffer, sizeof lineBuffer, stdin))
{
// lineBuffer contains the input, but it also contains the '\n' at the end.
doSomething(lineBuffer);
// etc. etc.
}
一个好主意是使用而不是scanf()
。任何空格(空格、制表符、换行符等)都将导致scanf()
停止解析,而基于您试图获取的输入的解析可能不是您想要的
像这样使用fgets()
:
char lineBuffer[100]; // or some number large enough
puts("Enter something:");
if (fgets(lineBuffer, sizeof lineBuffer, stdin))
{
// lineBuffer contains the input, but it also contains the '\n' at the end.
doSomething(lineBuffer);
// etc. etc.
}
与EXC\u BAD\u访问错误无关,但如果您的实际目标是创建通讯簿应用程序,则可能应该使用通讯簿框架。此外,您应该始终检查
scanf
的返回值scanf
返回成功解析的项目数,因此在代码中,每次调用scanf
都应返回1。如果不返回1,则表示发生了错误。与EXC\u BAD\u访问错误无关,但如果您的实际目标是创建通讯簿应用程序,则可能应该使用通讯簿框架。此外,您应该始终检查scanf
的返回值scanf
返回成功解析的项目数,因此在代码中,每次调用scanf
都应返回1。如果它不返回1,则表示发生了错误。好的,这很有意义。。但这给我带来了一个新问题。一旦我输入了名称/地址/等等,它会设置它们,但它们都会以某种方式设置为我最后输入的任何内容。您正在读取相同的缓冲区-然后我们的“设置”例程只是将它们都指向相同的缓冲区。您需要将数据复制到唯一的NSString。好的,这很有意义。。但这给我带来了一个新问题。一旦我输入了名称/地址/等等,它会设置它们,但它们都会以某种方式设置为我最后输入的任何内容。您正在读取相同的缓冲区-然后我们的“设置”例程只是将它们都指向相同的缓冲区。您需要将数据复制到唯一的NSString。
char lineBuffer[100]; // or some number large enough
puts("Enter something:");
if (fgets(lineBuffer, sizeof lineBuffer, stdin))
{
// lineBuffer contains the input, but it also contains the '\n' at the end.
doSomething(lineBuffer);
// etc. etc.
}