Thread: Mallocin' strlen(argv) into a char*

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193

    Question Mallocin' strlen(argv) into a char*

    edit:

    gotcha!

    Code:
     
    s = malloc(strlen(argv[1]) * sizeof(char));
    Good afternoon friends. How can I malloc argv in main?

    Code:
     
    #include <stdio.h> 
    #include <stdlib.h> /* for exit() */
    #include <string.h> 
    
    void Strcpy(char*, char*);
    
    void Strcpy(char* s, char* t) 
    { 
       while ( (*s++ = *t++) )
       ;     
    }
    
    int main(int argc, char** argv) 
    { 
       char* s = malloc(strlen(argv) * sizeof(char));
       
       if(argc < 2) 
       { 
         printf("Please enter [Executable][string]\n");  
         exit(0);
       }
       Strcpy(s, *(argv + 1));
       printf("Clone is %s\n", s);
       
       free(s);
       return 0;
    }
    Last edited by thames; 12-01-2012 at 09:39 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Questions regarding char *argv[ ]...
    By MikeyIckey in forum C Programming
    Replies: 7
    Last Post: 02-12-2009, 11:37 PM
  2. question regarding char *argv[]
    By cnb in forum C Programming
    Replies: 6
    Last Post: 10-08-2008, 04:50 AM
  3. when i would use char *argv[] and when char** argv[] ?
    By blue_gene in forum C Programming
    Replies: 17
    Last Post: 04-14-2004, 07:00 PM
  4. int argc char *argv[]
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 12-20-2001, 03:14 PM
  5. How to use int argc, char* argv[] ?
    By gogo in forum C++ Programming
    Replies: 3
    Last Post: 11-21-2001, 01:21 PM

Tags for this Thread