Length of string changes when passed with pointer

This is a discussion on Length of string changes when passed with pointer within the C Programming forums, part of the General Programming Boards category; The program below takes 2 words or phrases from a user, puts them together(concatenate), and then sorts the letters in ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    26

    Length of string changes when passed with pointer

    The program below takes 2 words or phrases from a user, puts them together(concatenate), and then sorts the letters in ascending and then descending order.
    For some odd reason, when I pass a concatenated string by way of a pointer to a function, it changes the length of my new string. If you check the length for both the new string and it's pointer in the main where it is concatenated, it reads properly, but in the new function, it only recognizes the first word and the space. Any ideas?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<string.h>
    #include<ctype.h>
    #include<time.h>
    
    void sort(char *conptr);
    void sortRev(char *conptr);
    
    int main()
    {
    char userStr1[500];
    char userStr2[500];
    char conStr[500];
    char *inputptr1;
    char *inputptr2;
    char *conptr = conStr;
    
       printf("     ***Part 1 - Creating two user strings***\n\n");
       printf("Please enter in a phrase\nuserStr1>>  ");
           inputptr1 = gets(userStr1);
    
       printf("\n");
       printf("Please enter another phrase\nuserStr2>>  ");
           inputptr2 = gets(userStr2);
    
       printf("\n\n");
    
            puts (inputptr1);
                printf("The length of userStr1 is %d.\n\n", strlen(inputptr1));
            puts (inputptr2);
                printf("The length of userStr2 is %d.\n\n", strlen(inputptr2));
       printf("\n\n");
    
       printf("     ***Part 2 - concatenating the two user strings***\n\n");
       strcpy( conStr, inputptr1);
       strcat( conStr, " ");
       strcat( conStr, inputptr2);
       printf("The concatenated string is ~~> %s\n\n", conStr);
    
       int len = strlen(conptr);
       printf("len %d\n\n", len);
    
       sort(conptr);
       sortRev(conptr);
    
          system("PAUSE");
          return 0;
    }
    
    void sort(char *conptr)
    {
        char forwardSort[50] = "conptr";
        int len = strlen(forwardSort);
            printf("\n\n");
                    printf("len %d\n\n", len); 
       
       printf("Unsorted - %s \n", conptr);
    
        for(int x=0; x<len-1; x++)
        {
            for(int y=0; y<len-x-1; y++)
            {
                    if (conptr[y] > conptr[y+1])
                    {
                                char temp;
                                temp = conptr[y];
                                conptr[y] =  conptr[y+1];
                                conptr[y+1] = temp;
                    }//if()
            }//for(y)
         }//for(x)
         printf("Sorted - %s \n", conptr);
         printf("\n");
    }  //end sort
    
    
    void sortRev(char *conptr)
    {
        char revStr[70] = "conptr";
        int len = strlen(revStr);
            printf("\n\n");
    
        for(int x=0; x<len-1; x++)
        {
            for(int y=0; y<len-x-1; y++)
            {
                    if (conptr[y] < conptr[y+1])
                    {
                                char temp;
                                temp = conptr[y];
                                conptr[y] =  conptr[y+1];
                                conptr[y+1] = temp;
                    }//if()
            }//for(y)
         }//for(x)
         printf("Sorted in reverse order - %s \n", conptr);
         printf("\n");
    } //end sortrev

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,853
    > char forwardSort[50] = "conptr";
    > int len = strlen(forwardSort);
    Delete the first line, and do
    len = strlen( conptr );


    > inputptr1 = gets(userStr1);
    Read the FAQ - gets() is bad for you


    > char *conptr = conStr;
    There is no need to do this, in order to do this
    sort(conptr);
    Since you can just as easily do this
    sort(conStr);

    > int len = strlen(conptr);
    Declaration in the middle of the code, you've slipped into C++ mode.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    26
    Not quite sure why the way I had it didn't work or the how and why as to what makes yours act properly. I didn't learn anything, but at least I'll pass this class. Thanks for your help.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,853
    > char forwardSort[50] = "conptr";
    Well what were you hoping to achieve with this line?

    Would
    char forwardSort[50] = "hello world";
    Have made it any clearer?

    On the face of it, it looked like you were trying to copy the input parameter into a local variable. But since you used conptr correctly later on, it seemed entirely redundant.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 10:37 PM

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