fizzy drink
fizzy drink

Reputation: 682

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)

Upvotes: 0

Views: 833

Answers (1)

hmjd
hmjd

Reputation: 122001

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.

Upvotes: 2

Related Questions