locked
copiar de char[] a char* RRS feed

  • Question

  • I've tried to copy the value passed to main's second argument, like this

    int main(int argc, char *argv[]) {
    	char* token;
    	std::strcpy(token, argv[1]);
    }

    but the compiler cannot deal with the 'constantness' of the second parameter in strcpy().

    Can any one help understand how to do this?

    Thanks!

    Monday, November 16, 2015 4:19 PM

Answers

  • On 16/11/2015 17:19, del noble wrote:
    > I've tried to copy the value passed to main's second argument, like this
    >
    > int main(int argc, char *argv[]) {
    >     char* token;
    >     std::strcpy(token, argv[1]);
    > }
    >
    >
     
    You haven't allocated any memory for the output "token" string.
    Your "token" just points to some garbage (you didn't even initialize
    that pointer).
    So the strcpy() call will stomp some random memory area.
     
    Since you are using C++ (and not pure C), do yourself a favor and use a
    convenient string class to store strings, like std::string (this will
    automatically manage memory allocation and cleanup for the string).
     
    You can create a std::string instance from the content of argv[1] just
    using a std::string constructor:
     
    [code]
       #include <string> // for std::string
    ...
       std::string token( argv[1] );
     
    [/code]
     
    Giovanni
     
    • Proposed as answer by chchlll Sunday, November 29, 2015 8:36 AM
    • Marked as answer by Shu 2017 Monday, November 30, 2015 9:59 AM
    Monday, November 16, 2015 7:36 PM

All replies

  • What compiler error are you getting? I don't see what this has to do with the second parameter of strcpy being const.
    Monday, November 16, 2015 4:58 PM
  • Among your other problems, token does not point to memory you own.  In fact, its value is indeterminate.  Evaluating an indeterminate value can cause undefined behavior.  Even if the value is not a trap representation, attempting to access memory you don't own also causes undefined behavior.

    Before you attempt to access argv[1], you should confirm that argc is at least 2.

    Monday, November 16, 2015 5:41 PM
  • On 16/11/2015 17:19, del noble wrote:
    > I've tried to copy the value passed to main's second argument, like this
    >
    > int main(int argc, char *argv[]) {
    >     char* token;
    >     std::strcpy(token, argv[1]);
    > }
    >
    >
     
    You haven't allocated any memory for the output "token" string.
    Your "token" just points to some garbage (you didn't even initialize
    that pointer).
    So the strcpy() call will stomp some random memory area.
     
    Since you are using C++ (and not pure C), do yourself a favor and use a
    convenient string class to store strings, like std::string (this will
    automatically manage memory allocation and cleanup for the string).
     
    You can create a std::string instance from the content of argv[1] just
    using a std::string constructor:
     
    [code]
       #include <string> // for std::string
    ...
       std::string token( argv[1] );
     
    [/code]
     
    Giovanni
     
    • Proposed as answer by chchlll Sunday, November 29, 2015 8:36 AM
    • Marked as answer by Shu 2017 Monday, November 30, 2015 9:59 AM
    Monday, November 16, 2015 7:36 PM
  • On 16/11/2015 17:19, del noble wrote:
    > I've tried to copy the value passed to main's second argument, like this
    >
    > int main(int argc, char *argv[]) {
    >     char* token;
    >     std::strcpy(token, argv[1]);
    > }
    >
    >
     
    You haven't allocated any memory for the output "token" string.
    Your "token" just points to some garbage (you didn't even initialize
    that pointer).
    So the strcpy() call will stomp some random memory area.
     
    Since you are using C++ (and not pure C), do yourself a favor and use a
    convenient string class to store strings, like std::string (this will
    automatically manage memory allocation and cleanup for the string).
     
    You can create a std::string instance from the content of argv[1] just
    using a std::string constructor:
     
    [code]
       #include <string> // for std::string
    ...
       std::string token( argv[1] );
     
    [/code]
     
    Giovanni
     
    Monday, November 16, 2015 7:38 PM
  • On 16/11/2015 17:19, del noble wrote:

    I've tried to copy the value passed to main's second argument, like this

    int main(int argc, char argv[]) {
        char
    token;
        std::strcpy(token, argv[1]);
    }

    You haven't allocated any memory for the output "token" string.
    Your "token" just points to some garbage (you didn't even initialize that pointer).
    So the strcpy() call will stomp some random memory area.

    Since you are using C++ (and not pure C), do yourself a favor and use a convenient string class to store strings, like std::string (this will automatically manage memory allocation and cleanup for the string).

    You can create a std::string instance from the content of argv[1] just using a std::string constructor:

         #include <string> // for std::string
    ...
         std::string token( argv[1] );
    

    Giovanni

    Monday, November 16, 2015 7:40 PM
  • On 16/11/2015 17:19, del noble wrote:

    I've tried to copy the value passed to main's second argument, like this

    int main(int argc, char argv[]) {
        char
    token;
        std::strcpy(token, argv[1]);
    }

    You haven't allocated any memory for the output "token" string.
    Your "token" just points to some garbage (you didn't even initialize that pointer).
    So the strcpy() call will stomp some random memory area.

    Since you are using C++ (and not pure C), do yourself a favor and use a convenient string class to store strings, like std::string (this will automatically manage memory allocation and cleanup for the string).

    You can create a std::string instance from the content of argv[1] just using a std::string constructor:

         #include <string> // for std::string
    ...
         std::string token( argv[1] );
    

    Giovanni

    Monday, November 16, 2015 7:42 PM
  • On 16/11/2015 17:19, del noble wrote:

    I've tried to copy the value passed to main's second argument, like this

    int main(int argc, char argv[]) {
        char
    token;
        std::strcpy(token, argv[1]);
    }

    You haven't allocated any memory for the output "token" string.
    Your "token" just points to some garbage (you didn't even initialize that pointer).
    So the strcpy() call will stomp some random memory area.

    Since you are using C++ (and not pure C), do yourself a favor and use a convenient string class to store strings, like std::string (this will automatically manage memory allocation and cleanup for the string).

    You can create a std::string instance from the content of argv[1] just using a std::string constructor:

         #include <string> // for std::string
    ...
         std::string token( argv[1] );
    

    Giovanni

    Monday, November 16, 2015 7:43 PM
  • yes, thanks Gio!
    Monday, November 30, 2015 4:44 PM