Thread: Function not behaving properly

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    9

    Function not behaving properly

    I am making a simple function to convert a word to all lower case letters. While this function works perfectly for its first instance on line 52, but getting messed up when returning from line 53 on its second instance. Can someone explain me what's going on?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    char* eraseNewLine(char* word)
    {
        int i=0;
        while(word[i]!='\0')
        {
            if(word[i]=='\n')
            {
                word[i]='\0';
            }
            i++;
        }
        return word;
    }
    
    
    char* toLower(char* word)
    {
        char* wrd_cpy = word;
        int i = 0;
        while(word[i]!= '\0')
        {
            if(word[i]>='A'&&word[i]<='Z')
            {
                wrd_cpy[i]=(char)((int)word[i]+32);
            }
            i++;
        }
        return wrd_cpy;
    }
    
    
    int main()
    {
        char word_1[20], word_2[20];
        char* word_1_cpy, word_2_cpy;
        printf("Enter the word_1: ");
        fgets(word_1, 20, stdin);
    
    
        printf("Enter the word_2: ");
        fgets(word_2, 20, stdin);
    
    
        eraseNewLine(word_1);
        eraseNewLine(word_2);
    
    
        word_1_cpy = toLower(word_1);
        word_2_cpy = toLower(word_2);
    
    
        printf("%s and %s", word_1_cpy, word_2_cpy);
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2017
    Posts
    9
    Okay, I found a silly mistake on the line 39 while declaring word_2_cpy.
    Code:
    char* word_1_cpy, word_2_cpy;
    I should have put an asterisk before word_2_cpy. Well that solved the problem, but now I am facing a new one. My function is changing the original word to lower case even though I have assigned it to a copy. The function is now changing the original word as well as the copy. I want original to be in its original case.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Making a copy of the pointer isn't the same as making a copy of the string.

    A pointer is like a postcard.
    You don't make a new house simply by copying the address to another postcard.
    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.

  4. #4
    Registered User
    Join Date
    Jan 2017
    Posts
    9
    Okay, thanks. That clarifies a lot.

    Quote Originally Posted by Salem View Post
    Making a copy of the pointer isn't the same as making a copy of the string.
    So, at the line 22 "wrd_cpy" is behaving as a pointer to "word" instead of behaving as a new string. Therefore, I should just declare it without assigning anything to it and then copy the string variable "word" to it. Did I understand this correctly?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you want the pointer "wrd_cpy" to contain a new string, you need to allocate memory for it first (and free the memory when you're done). Alternatively, you could create an another char array in main() (perhaps called "word_1_lower"), copy the original string to this new array, and operate on the new array. This would keep the original intact.

    I would suggest using full words for your variable names; e.g. "word_copy". Just dropping the vowels does not buy you much, and makes the code less readable.

    Code:
    if(word[i]>='A'&&word[i]<='Z')
    {
        wrd_cpy[i]=(char)((int)word[i]+32);
    }
    Look into the isupper()/islower() and toupper()/tolower() functions in ctype.h. These will make your code portable and more readable.

  6. #6
    Registered User
    Join Date
    Jan 2017
    Posts
    9
    Quote Originally Posted by Matticus View Post
    If you want the pointer "wrd_cpy" to contain a new string, you need to allocate memory for it first (and free the memory when you're done).
    Can I measure the length of original string and use that measurement as a size of new one instead of memory allocation?

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you're compiling with respect to c99 or later, you can use a variable-length array (VLA). I personally avoid using them, preferring to malloc() in those situations.

    Be aware that strlen() does not count the null character '\0'.

  8. #8
    Registered User
    Join Date
    Jan 2017
    Posts
    9
    To be honest, I am not aware of which standard my compiler adhere to. I am using code::blocks.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code::Blocks is an IDE. You should check to see which compiler is being used (typically gcc/mingw by default).

    A quick search for "code blocks compile as c99" yields plenty of information.

  10. #10
    Registered User
    Join Date
    Jan 2017
    Posts
    9
    Its GNU GCC. I'll check later if its compiling to c99 standards. The above program is working now, by the way, by creating another character string in main and copying the old string to it and then passing it to the function to lower all case.. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to properly use a function?
    By Who in forum C Programming
    Replies: 2
    Last Post: 02-01-2013, 03:25 AM
  2. advise on how to properly use pow function
    By libchk in forum C Programming
    Replies: 12
    Last Post: 08-31-2011, 12:50 AM
  3. How to properly use a C function?
    By John_L in forum C Programming
    Replies: 4
    Last Post: 05-30-2008, 02:01 AM
  4. Loop doesn't seem to function properly
    By TeQno in forum C++ Programming
    Replies: 1
    Last Post: 01-31-2005, 05:25 PM
  5. while loop not behaving properly.
    By Dreamerv3 in forum C++ Programming
    Replies: 20
    Last Post: 01-08-2002, 05:51 PM

Tags for this Thread