Like Tree2Likes
  • 2 Post By whiteflags

Array of pointers and pointer for arrays with argv

This is a discussion on Array of pointers and pointer for arrays with argv within the C Programming forums, part of the General Programming Boards category; Good afternoon. I was wondering why this program with a pointer for arrays with char (*argv)[100] doesn't work at all. ...

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

    Array of pointers and pointer for arrays with argv

    Good afternoon. I was wondering why this program with a pointer for arrays with char (*argv)[100] doesn't work at all.

    I understand char (*argv)[100] is declaring a pointer for arrays with n lines and a maximum of 13 chars. And char *argv[] is declaring an array for n char pointers (strings). Therefore, I thought the program below would work as expected, printing thames thames thames when I input:

    Code:
     
      ./strcpy thames thames thames
    My line of thought was: "well, if I input less than 100 characters, everything will work just fine. "

    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)[100]) 
    { 
       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\n");
       for(i = 0; i < argc - 1; i++) 
       { 
          printf("%d.\t%s\n", i + 1, s[i]);   
       }         
       
       return 0;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    1,923
    When i compile it
    Code:
    Macintosh-c8bcc88e5669-9:~ usi$ gcc -Wall px.c -o px
    px.c:38: warning: second argument of ‘main’ should be ‘char **’
    I think here is something you should read.
    Last edited by std10093; 12-02-2012 at 09:10 AM. Reason: damaged url
    Code - functions and small libraries I use
    __________________________________________________ __________________________________________________ ______________
    It’s 2013 and I still use printf() for debugging.

  3. #3
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    Quote Originally Posted by std10093 View Post
    When i compile it
    Code:
    Macintosh-c8bcc88e5669-9:~ usi$ gcc -Wall px.c -o px
    px.c:38: warning: second argument of ‘main’ should be ‘char **’
    I think [URL="http://www.cprogramming.com/tutorial/c/lesson14.html"]here/URL] is something you should read.
    I knew about this warning, but I'd like to understand why, considering what I said, char (*argv)[50] wouldn't work. will that notation work in another function for what I wanted to print ?

    edit:

    for example, instead of argv I have char (*str)[50] and I want to print thames

    do I have to write char *str[50] to print the output as expected ? why? if I have n letters and I tried to print less than 50 letters?
    Last edited by thames; 12-02-2012 at 09:11 AM.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    6,813
    I think looking up the type would be illuminating.

    char (*str)[50]
    declare str as pointer to array 50 of char

    char *str[50]
    declare str as array 50 of pointer to char

    So there is a difference, and argv is just a list of pointers at least argc long. And argv[argc] is guaranteed to be NULL.
    AndiPersti and thames like this.
    Quote Originally Posted by phantomotap
    Can you write code while blindfolded only with the blind covering your brain? Can you code while brainfolded?

  5. #5
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    I think looking up the type would be illuminating
    I got the idea.

    And argv[argc] is guaranteed to be NULL
    this is interesting, I didn't know that. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of 8 arrays of 7 pointers to int
    By juice in forum C Programming
    Replies: 2
    Last Post: 04-01-2012, 12:02 PM
  2. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  3. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  4. pointer to array of arrays
    By linuxdude in forum C Programming
    Replies: 3
    Last Post: 10-03-2006, 12:25 PM
  5. pointer to pointers to arrays of strings??
    By Binkstone in forum C Programming
    Replies: 8
    Last Post: 09-14-2001, 02:56 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21