简体   繁体   中英

strlen EXC_BAD_ACCESS in Xcode - c

This is my code:

#include <stdio.h>
#include <string.h>

int main() {
    char x[256];
    strcpy(x, "Gonzo!");
    printf(strlen(x));
}

I'm not sure that I am coding this correctly. I want to know the string length of x .

I am using XCode 5+ and I get the error:

EXC_BAD_ACCESS(code=1, address=0x6)

The first argument of printf() is a const char* , which specifies the format. The posted code passes in 6 as the starting memory location for the char* which is incorrect and is causing the error. Use:

printf("%d\n", strlen(x));

See the linked printf() reference documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM