954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Is casting a static array to (char*) in strcpy() dangerous?

What's the difference between

char   *Name;
Name = malloc (13*sizeof(char));
strcpy (Name, "George Brown");


and

char   Name[13];
strcpy (Name, "George Brown");


The compiler gives a warning in the second case: warning C4047: 'function' : 'char *' differs in levels of indirection from 'char *[13]' & warning C4024: 'strcat' : different types for formal and actual parameter 1 . The warning goes away if we cast the static array as (char*) . Is it dangerous though?

mohammadalipak
Newbie Poster
19 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

In which case I would say you're not posting the code you're compiling.

The only way to get those warnings with VC++ is to do this
char Name[13];
strcpy ( <strong>&</strong>Name, "George Brown");

As written, both snippets are fine.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

You're right. I was mistaking LPSTR datatype to be a char where it is defined as char* . Thanks.

mohammadalipak
Newbie Poster
19 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You