Thread: Help with syntax please.

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    Unhappy Help with syntax please.

    Basically I am having problems swapping two strings (char arrays).

    prototypes for the functions in question:
    Code:
    int main(int argc, char *argv[]);
    void ssort(char *argv[], int len);
    void swap(char ** s, char ** p);
    How I call ssort from main:
    Code:
    ssort(argv,argc);

    This is the function ssort:
    Code:
    void ssort(char *argv[], int len) {
    while(len > 2){
    if(strcmp(argv[len-2],argv[len-1]))
    swap( &argv[len-2], &argv[len-1]); /*IS THIS WRONG!?*/
    len--;
    }
    }
    And here is swap:
    Code:
    void swap(char ** s, char ** p) {
    char temp[strlen(*s)-1];
    strcpy(temp,*s);
    strcpy(*s,*p);
    strcpy(*p,temp);
    }

    I'm pretty sure my swap function is the thing that's wrong. The program compiles fine but it is not swapping the strings like I want.

    I have a function that prints out the strings, and it prints out exactly what I type in the first time, but then after it has gone through the sorting process, it gives them back in a different order but some have been combined with others and only half of the word is there.

    This is an assignment and I have to change the actual string itself, not just print out the string in the correct order

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    We have several issues.

    First, you need to understand that argv is an array of strings. So when you access an element, you've already got the string. You would pass a string to a function just like you would pass an array to a function.

    The second thing is that you're not really supposed to write to argv. If you've passed actual data that you need to use for your program through the command line, it might be best to use another variable and build your own array of strings.
    Code:
    #include <string.h>
    #include <stdlib.h>
    
    /** ... **/ 
    
    char ** myargv = malloc( argc * sizeof *myargv );
    if( myargv ) {
       int i;
       for( i = 0; i < argc; i++ ) {
          myargv[i] = malloc( strlen( argv[i] ) + 1 );
          if( myargv[i] ) {
             strcpy( myargv[i], argv[i] );
          }
       }
       myargv[argc] = NULL; /** like argv **/
    }
    Then you can swap your own set of strings around safely and mess with them as much as you like. Don't forget to free the space you've acquired from malloc.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    Thanks for your reply. I'll take your advice and not write to argv.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by citizen View Post
    Code:
    char ** myargv = malloc( argc * sizeof *myargv );
    [...]
       myargv[argc] = NULL; /** like argv **/
    This allocation should be (argc + 1) * sizeof *myargv or else writing to myargv[argc] will be out of bounds.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    That's right, I apologize.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM