Search code examples
cpointersc-stringsstrcpyfunction-declaration

How do I return and print out the starting address of the first string (string1)?


I first copy the second string into the first string. Then I would return string1 pointer from the function. Then I use %p and (void*) to print out the starting address of the first string. Is this the right approach?

    printf("address: %p  \n", (void*)functionstrcpy(stringnumber1,stringnumber 2));
    return 0;
}

char *functionstrcpy(char *stringnumber1,char *stringnumber2)
{
    //calculation
    return stringnumber1;
}

Solution

  • How do I return and print out the .... address of the ... string?
    I use "%p" and (void*) to print out the starting address of the first string. Is this the right approach?

    Yes. The return is correct and "%p" expects to match a void *. Yet since char * and void * are the same encoding and size, either will work.

    printf("address: %p  \n", (void*)functionstrcpy(str1,str2));
    // or 
    printf("address: %p  \n",        functionstrcpy(str1,str2));
    

    Thus the choice is best according to your group's style guide.