0

I have a function that keeps on incrementing the char, so it will display the next char in the ascci code, but my problem is it never breaks the loop

     char * myFunction (char* &text)
       {
               char *temp = (char *)malloc(strlen(text));
                char *tmp = temp;
                while(*tmp != '\0')
                 {
                          *tmp = *text++;  //im incrementing the text
                          tmp +=1;

                 }
          return temp;

        }

       char *text = "hello";
        cout<<myFunction(text)<<endl;
7
  • *tmp += 1; -> tmp += 1; Commented Feb 26, 2014 at 10:04
  • Better to use string functions like strlen (instead of sizeof(text), which won't work, unless you want the size of the pointer itself) and strcpy (instead of the manual loop). Also, don't assume that malloc is successful, always check its return value (here temp)! Commented Feb 26, 2014 at 10:04
  • if *text++ returns '\0' then *tmp +=1; will increament it Commented Feb 26, 2014 at 10:08
  • What are you actually trying to do? It might be easier to throw all your code away and write something simpler. Commented Feb 26, 2014 at 10:10
  • increment to the next asci code of the char Commented Feb 26, 2014 at 10:11

2 Answers 2

2
while(tmp != '\0')

to be

while(*tmp != '\0')

tmp is the address of the start of the string which will never be '\0'

Sign up to request clarification or add additional context in comments.

1 Comment

Still, I cannot see how they expect to find a '\0' in there. That and all the other bugs.
1

there are lots of problems in your code, I summarized them in comments below:

 // Making your argument char*& allows you to modify text variable from 
 // calling function, I dont think this was your intention. If you just 
 // want to pass string to this function change its type to `const char*`.
 // I dont think this return value you really want here, now you have char, but I 
 // suppose you want to return string, so it should be `char*` or `const 
 // char*`
 char myFunction (char* &text)
   { 
           // sizeof(text) will return 4bytes, which is size of pointer, 
           // you want strlen(text)+1 here to get length of string + 1 
           // char for zero (end of string)
           char *temp = (char *)malloc(sizeof(text));
            char *tmp = temp;

            // You should dereference *tmp, but tmp is uninitialized here, 
            // I think you want to use *text here.
            while(tmp != '\0')
             {
                      *tmp = *text++;  //im incrementing the text

                      // You are incrementing value pointed by tmp, 
                      // if you want to increment tmp to next string 
                      // element use tmp++;
                      *tmp +=1;

             }

             // Here assign zero to last element of text            

      // temp is char* and your return value is char, this is wrong, 
      // you should change return value of myFunction to char*. Also 
      // remember to call free() on returned string once its no
      // longer needed - otherwise you will introduce memory leaks
      return temp;

    }




   // This should be `const char *text = "hello";` In c++ string
   // literals are of type `const char*`
   char *text = "hello";
    cout<<myFunction(text)<<endl;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.