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

  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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I expect that you would malloc space to store pointers to char, then malloc space for each string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    Strcpy(s, *(argv + 1));
    I think you need to change that to
    Code:
     Strcpy(s, (*argv) + 1);
    Remember to dereference first.

  4. #4
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    Quote Originally Posted by laserlight View Post
    I expect that you would malloc space to store pointers to char, then malloc space for each string.
    how would I do that ?

    Now I'm getting /strcpy from the output.

    Code:
     
    ./strcpy thames
    Clone is /strcpy
    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;
       
       if(argc < 2) 
       { 
         printf("Please enter [Executable][string]\n");  
         exit(0);
       }
       
       s = malloc(strlen(argv[1]) * sizeof(char));
       
       Strcpy(s, (*argv) + 1);
       printf("Clone is %s\n", s);
       
       free(s);
       return 0;
    }
    Last edited by thames; 12-01-2012 at 11:02 AM.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by thames View Post
    how would I do that ?
    Read the comments.

    Code:
    #include <stdio.h>
    #include <string.h> /* strcpy */
    #include <stdlib.h> /* malloc */
    
    char **get(int n, int m) /* Malloc the 2D array (n x m) */
    {
        int i;
        char** table;
    
        /* Malloc n char pointers */
        table=malloc(n*sizeof(char *));
    
        /* For every char pointer, malloc space for the string
        * that they are going to point to    */
        for(i=0 ; i<n ; i++)
             /* *(table+i) is equivalent to table[i] */
            *(table+i)=malloc( m*sizeof(char) );
        return table;
    }
    
    int main(void)
    {
            int numberOfcharPtrs = 5;
    
            int spaceForEveryString = 20;
    
            int i;
    
            char** arrayOfStrings;
    
            arrayOfStrings = get( numberOfcharPtrs, spaceForEveryString);
    
            /* Fill the array with strings */
            for( i = 0 ; i < numberOfcharPtrs ; i++)
                    strcpy( arrayOfStrings[i] , "Example for thames from Brazil");
    
    
            /* Print all the strings */
            for( i = 0 ; i < numberOfcharPtrs ; i++)
                    printf("At index %d we have stored : %s\n",i,arrayOfStrings[i]);
    
            return 0;
    }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    I'm getting a segmentation fault when I try to input a name:

    Code:
     
      ./strcpy thames thames
      Segmentation fault

    Code:
     
    #include <stdio.h> 
    #include <stdlib.h> /* for exit and malloc*/
    #include <string.h> 
    
    #ifndef MAXPOINTERS
      #define MAXPOINTERS 20
    #endif 
    
    #ifndef MAXCHARS
      #define MAXCHARS 100
    #endif    
    
    void Strcpy(char*, char*);
    char** Malloc2D(int, int);
    
    void Strcpy(char* s, char* t) 
    { 
       while( (*s++ = *t++) )
       ;
    }
    
    char** Malloc2D(int nPointers, int nChars)
    {
       int i; 
       char** strings;
       
       strings = malloc(nPointers * sizeof(char*));
       
       for(i = 0; i < nPointers; i++) 
       { 
         *(strings + i) = malloc(nChars * sizeof(char));  
       }
       
       return strings;            
    }     
    
    int main(int argc, char** argv) 
    { 
       int i, j;
       char** s;
       
       
       if(argc < 2) 
       { 
         printf("Please enter [Executable][string]\n");  
         exit(0);
       }
       
       else if(argc > MAXPOINTERS) 
       { 
         printf("You can write up to %d words", MAXPOINTERS);
         exit(0);   
       }        
       
       s = Malloc2D(MAXPOINTERS, MAXCHARS);
       
       for(j = 1, i = 0; i < MAXCHARS; i++, j++) 
       { 
          Strcpy(s[i], argv[j]);  
       }        
       
       printf("The strings which were input are:\n");
       for(i = 0; i < MAXPOINTERS; i++) 
       { 
          printf("%d.\t%s", i, s[i]);   
       }         
       
       return 0;
    }

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    for(j = 1, i = 0; i < MAXCHARS; i++, j++)
    How many strings should you copy inside your array?
    - I should copy inside be array as many strings as the user gives in the cmd

    How many does he input?
    - argc
    No
    - why?
    Because argc contains the name of the executable too, so the number is
    Code:
    argc-1
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    Quote Originally Posted by std10093 View Post

    How many strings should you copy inside your array?
    - I should copy inside be array as many strings as the user gives in the cmd
    I'm using j for that purpose (to start argv at index 1) . I fixed the for:

    Code:
     
    for(j = 1, i = 0; i < MAXPOINTERS; i++, j++) 
       { 
          Strcpy(s[i], argv[j]);  
       }
    But I'm still getting the same error.

    edit:

    okey-dokey, I fixed this else as you told me:

    Code:
     
    else if(argc >= MAXPOINTERS) 
       { 
         printf("You can write up to %d words", MAXPOINTERS);
         exit(0);   
       }
    The current code:

    Code:
     
      #include <stdio.h> 
    #include <stdlib.h> /* for exit and malloc*/
    #include <string.h> 
    
    #ifndef MAXPOINTERS
      #define MAXPOINTERS 20
    #endif 
    
    #ifndef MAXCHARS
      #define MAXCHARS 100
    #endif    
    
    void Strcpy(char*, char*);
    char** Malloc2D(int, int);
    
    void Strcpy(char* s, char* t) 
    { 
       while( (*s++ = *t++) )
       ;
    }
    
    char** Malloc2D(int nPointers, int nChars)
    {
       int i; 
       char** strings;
       
       strings = malloc(nPointers * sizeof(char*));
       
       for(i = 0; i < nPointers; i++) 
       { 
         *(strings + i) = malloc(nChars * sizeof(char));  
       }
       
       return strings;            
    }     
    
    int main(int argc, char** argv) 
    { 
       int i, j;
       char** s;
       
       
       if(argc < 2) 
       { 
         printf("Please enter [Executable][string]\n");  
         exit(0);
       }
       
       else if(argc >= MAXPOINTERS) 
       { 
         printf("You can write up to %d words", MAXPOINTERS);
         exit(0);   
       }        
       
       s = Malloc2D(MAXPOINTERS, MAXCHARS);
       
       for(j = 1, i = 0; i < MAXPOINTERS; i++, j++) 
       { 
          Strcpy(s[i], argv[j]);  
       }        
       
       printf("The strings which were input are:\n");
       for(i = 0; i < MAXPOINTERS; i++) 
       { 
          printf("%d.\t%s", i, s[i]);   
       }         
       
       return 0;
    }
    Last edited by thames; 12-01-2012 at 02:44 PM.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    argv[0] containts the executable name, argv[1] will contain the first string and so on...

    So what I said before in code is this
    Code:
    for(j = 1, i = 0; i < argc-1; i++, j++)
    Of course do not forget to act likewise for the printing part of your code
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    I think I understood. I can't use MAXPOINTERS because I don't know how many pointers were entered. The remaining will cause a Segmentation Fault. A common problem.

    many thanks.

    Code:
     
    #include <stdio.h> 
    #include <stdlib.h> /* for exit and malloc*/
    #include <string.h> 
    
    #ifndef MAXPOINTERS
      #define MAXPOINTERS 20
    #endif 
    
    #ifndef MAXCHARS
      #define MAXCHARS 100
    #endif    
    
    void Strcpy(char*, char*);
    char** Malloc2D(int, int);
    
    void Strcpy(char* s, char* t) 
    { 
       while( (*s++ = *t++) )
       ;
    }
    
    char** Malloc2D(int nPointers, int nChars)
    {
       int i; 
       char** strings;
       
       strings = malloc(nPointers * sizeof(char*));
       
       for(i = 0; i < nPointers; i++) 
       { 
         *(strings + i) = malloc(nChars * sizeof(char));  
       }
       
       return strings;            
    }     
    
    int main(int argc, char** argv) 
    { 
       int i, j;
       char** s;
       
       
       if(argc < 2) 
       { 
         printf("Please enter [Executable][string]\n");  
         exit(0);
       }
       
       else if(argc >= MAXPOINTERS) 
       { 
         printf("You can write up to %d words", MAXPOINTERS);
         exit(0);   
       }        
       
       s = Malloc2D(MAXPOINTERS, MAXCHARS);
       
       for(j = 1, i = 0; i < argc - 1; i++, j++) 
       { 
          Strcpy(s[i], argv[j]);  
       }        
       
       printf("\nThe strings which were input are:\n");
       for(i = 0; i < MAXPOINTERS; i++) 
       { 
          printf("\n%d.\t%s", i + 1, s[i]);   
       }         
       
       return 0;
    }
    Now, I think I won't forget the segmentation fault problems are because of out of bounds access.

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Isn't it nice when you find the solution by yourself? ( With some ideas from other people, but still it seems that you did understand )

    A last tip : I wouldn't print all the pointers. I would print only how many input I have.
    In code
    Code:
    printf("\nThe strings which were input are:\n");
       for(i = 0; i < MAXPOINTERS; i++) 
       { 
          printf("\n%d.\t%s", i + 1, s[i]);   
       }
    This can achieved by modifying the red part

    You know you are welcome. Very glad that I helped
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    This can achieved by modifying the red part
    heh [trollface]

    You know you are welcome. Very glad that I helped
    sure thing! many thanks.

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