开发者

Type of parameter accepted by strcpy() ?

Why is that strcpy() accepting char array pointer even though the definition of strcpy is char * strcpy( char * , const char * ) ??

#include <stdio.h>
#include <string.h>

main()
{
    char str[] = "Have A Nice Day";
    char ptr[17];

    strcpy(开发者_Go百科ptr, str);
    printf("%s", ptr);

}


An array is not a pointer (although they are similar in behavior and usage), but it transparently decays to one in a context where a pointer is needed (like in the case where it's passed as a parameter to a function that expects a pointer).

A more in-depth description can be found in the C FAQ 6.3.


An char[n] gives an address which can be used in place of a const pointer with memory allocated at time of declaration.


In C/C++ arrays are pointers as well. http://www.cplusplus.com/forum/articles/9/ See here for more explanation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜