user14399919
user14399919

Reputation:

How can I copy an element of an array of chars to a char?

Let's say I've got this piece of code:

void copyElement(int n, char source[100])
{
    char s;
    strcpy(s, source[n]);
}

I will get some errors and it won't work.

What would be the proper way to do it?

Upvotes: 0

Views: 301

Answers (2)

Amine Dakhli
Amine Dakhli

Reputation: 172

strcpy takes pointers to characters and not characters, what you could do is

void copyElement(int n, char source[100])
{
    char * s = (char *) malloc (1) ;
    strcpy(s, &source[n]);
}

Upvotes: -1

iamdhavalparmar
iamdhavalparmar

Reputation: 1218

You can do it directly. If you want to copy a char of a specific position you can use

char c = source[index_of_array];

Upvotes: 3

Related Questions