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.
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++)
'0', but '\0' or 0i < 10 is more clear than i <= 9 in C-based languages, so it's preferred.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.
strlen() in each iteration is bad idea
sprintf(a, "%p", b);to get the pointer into the buffera. However, it could be that the buffer is not wide enough, depending on the size of the pointer. Actually, check outsnprintf(), which should be safer.for(i = 0; a[i] = b[i]; i++);