Thread: [noob question]How to convert from static to pointer

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
1
Rep Power
0

[noob question]How to convert from static to pointer

Hy to everyone . I have a problem.

Code:
```int intToChar(unsigned int in, unsigned char* out){

int tmp;
int count = 0;

while(in != 0){
tmp = in % 10;
in = in / 10;
*(out + count) = (char)(tmp + '0');
count ++;
}

*(out + count) = '\0';

return count;
}```
I need to rewrite this function using pointers, but i don't understand what to modify. I don't need to have a pointer to function. Can you help me?

I understand the principle of a pointer, but i have no clue what to modify to this function. ...
2. No Profile Picture
bdb
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
156
Rep Power
33
You can rewrite the function so that `in` is also a pointer, or the return value is "returned" in a pointer ... but the original looks very much nicer!

[code=c]void intToChar_with_pointers(unsigned int *in, unsigned char *out, int *result);[/code]
3. Perhaps what they want is for you to replace the pointer arithmetic with a pointer that gets incremented.

For example (in order to not do your work for you), a simple implementation of string copying might be:
Code:
```char *my_strcpy(char *dest, char *src)
{
char *sp, *cp;

sp = src;
dp = dest;
while(*sp)  // fails when it reaches the null-terminator
*dp++ = *sp++;
*dp = '\0';

return dest;
}```
Since post-increment has higher precedence than dereferencing does, it's the pointer that's being incremented, so both working pointers are initialized at the beginning of their respective arrays and then iterate through each sequential location in their respective arrays until the source's null-terminator -- and hence the end of the string -- is reached.