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

    Join Date
    Sep 2012
    Rep Power

    [noob question]How to convert from static to pointer

    Hy to everyone . I have a problem.

    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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Rep Power
    Your function already uses pointers: `out` is a pointer.
    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]
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Rep Power
    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:
    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.

IMN logo majestic logo threadwatch logo seochat tools logo