Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C *char pointer to char array

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.

like image 409
user2959923 Avatar asked Nov 18 '25 03:11

user2959923


1 Answers

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++)
like image 73
Yu Hao Avatar answered Nov 20 '25 16:11

Yu Hao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!