quick question how can I work around this

char * dat = "08/11/2014";
    char test[10] = dat;

I need to be able to take a string and put it into an array of chars, how can i do this?

I need to be able to take a string and put it into an array of chars, how can i do this?

If the string is a literal, it's as easy as this:

char test[] = "08/11/2014";

If the string is obtained some other way, and all you have is a pointer to it, you need to create an "array" that can hold it, and copy the characters. In this case, I'd suggest dynamic memory:

// Don't forget to include space for the null character
char *test = malloc(strlen(dat) + 1);

// Always check for failure
if (test)
{
    strcpy(test, dat);

    ...

    free(test); // All done, clean up the mess
}

The problem with some random string you know little about is you don't know the size. So it's tricky to use an array proper due to requiring a compile-time constant[1].

[1] Yes, C technically allows variable length arrays starting with C99. However, they're not recommended for various good reasons. Let me know if you want more detail on why VLAs should be avoided in favor of more classic techniques.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.