C *char pointer to char array

14,237

Solution 1

The size of a is 10, so is the length of b(including the null terminator). Change

for(i = 0; i < 9; i++)

to

for(i = 0; i < 10; i++)

Solution 2

What you are doing is right.

Juts change i<10 in your for loop.

There are 10 elements in your char array and the array index is from 0-9.

Share:
14,237
user2959923
Author by

user2959923

Updated on June 04, 2022

Comments

  • user2959923
    user2959923 about 2 years

    I have a *char pointer and a char array. How can I put the value of the pointer in the char array? I've tried

    uint8_t i;
    char a[10];
    char *b = "abcdefghi";
    for(i = 0; i < 9; i++)
    {
        a[i] = b[i];
    }
    printf("%s", a);
    

    But this doesn't work.