   C and pointers, Logic Error
  below is the code for a function I have to write to reverse a char array and print out the array.  I cannot simply print it reversed it must be reversed in memory.  Everything works fine except the printing of the reversed array.  The line it is suppsed to print on is simply blank.  I have gone over the code over and over and can''t figure out why. I figured I would get an outside view, seeing as how someone else could spot something I overlooked.Ifyou have anymore questions about the rest of the code let me know, I will post more.Thank you for any help you can give   /*Name: ReverseInput: pointer to a char arrayOutput: voidDescription: takes a character array and reverses the elements in memory*/void Reverse (char *pReverseBuffer){   int numchar = 0,LBound = 0, UBound, temp;   int count = 0;   //Printf the Buffer before the reverse and count number of characters in it   printf("Buffer3 (before Reverse): ");   while (*(pReverseBuffer + count) != ''\0'')   { printf("%c",*(pReverseBuffer + count)); count++; numchar++;   } //End while loop   printf("\n");    printf("There is %d characters in Buffer3.\n", numchar); //confirm number of characters  //Set upper bound equal to the number of characters and swap elements at position lower and upper bound   //until bounds are equal   UBound = numchar;  while (LBound != UBound)   { temp = *pReverseBuffer + LBound; *(pReverseBuffer + LBound) = *(pReverseBuffer + UBound); *(pReverseBuffer + UBound) = temp; UBound--; LBound++;   }//End While loop    //Print the Buffer after reversed   count = 0; printf("Buffer3 (after Reverse): %s "*pReverseBuffer);   while (*(pReverseBuffer + count) != ''\0'')   { printf("%c",*(pReverseBuffer + count)); count++;    } //End while loop   printf("\n");   return;}//End function Reverse
void reverse (char *data, int length){  char* first = data;  char* last = first + length - 1;  while(last>first)  {char tmp = *last;*last = *first;*first = tmp;++first;--last;  };}char buffer[] = "This is a string.";reverse(buffer, strlen(buffer));printf("%s", buffer);Your errors: * LBound + count points to the null character at the end. Which means it ends up being first in the reversed array, and printf stops immediately, printing nothing. Initialize at count -1 instead (oh, and use strlen)* If the array has an even size, UBound will never be equal to LBound. Use < instead. “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor);  I believe that your swapping while loop is reversing the reverse! You should not loop for a num of characters times, you should loop half of that....UBound = numchar/2;while (LBound != UBound){... ;  Thanks Fruny I will try that out, I can''t use strlen though, the next part of the lab is to create our own version of strlen. ;  For pointless'' sake, you can also change this code here:char temp = *last;*last = *first;*first = temp;to simply:*last ^= *first;*first ^= *last;*last ^= *first;and it will do it for ya! I like using bitwise XOR over creating the new variable because it''s just easier for me to understand.  The first part took me about 10 seconds to understand, the bottom I knew exactly what it was doing.Besides, if it''s for a Programming class it makes you look like you have a grasp of the language, thus increasing your grade :-) ;quote:Original post by ShadowWolf   *last ^= *first;*first ^= *last;*last ^= *first;Not as clever an idea as people generally believe as it cannot be generalized.quote:and it will do it for ya! I like using bitwise XOR over creating the new variable because it's just easier for me to understand. The first part took me about 10 seconds to understand, the bottom I knew exactly what it was doing. My dear sir, I think you have a problem.   quote:Besides, if it's for a Programming class it makes you look like you have a grasp of the language, thus increasing your grade :-)I have marked people down for such infantile behaviour.“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor) [edited by - Fruny on February 18, 2004 1:15:28 PM];  all they have done is mention bitwise operators. We have not discussed their use in the least.  The point of this lab is to work with pointers, so I am going to stick to the swap I have. ;  Ahh, I see my issue now.  I didn''t really think about how the pointers would interfere with that.  Very interesting.  Sometimes I sometimes forget some of the complications involved with C++ pointer operations.But to addresss your post, Fruny,you could hardly be less helpful.  I mean, to call me infantile for wanting a better grade and yet just say "My dear sir, I think you have a problem."?  C''mon!  It would''ve been better for me to have something like "You can''t do that because of blah blah blah..." than "You have a problem." don''t you think?  Then, when I tested it, I could have maybed saved some time researching why it doesn''t work - exactly what I''m going to do now!About the marking people down for that, contrary to popular professor belief, there is nothing beyond grades in College.  Learning something is great, but as long as the letter grade is there, the letter matters most and learning is second.  Don''t delude yourselves or students by saying it''s not true, it is.  I''ve never known anyone to be threatened to be expelled for having good grades but not knowing the content.  If it were the case, things would be much better.  Everything you can do to get a better grade is something you better do unless you''re sitting pretty on that "A".I guess by some sort, maybe you assumed I knew it wouldn''t work?  I didn''t - though I do now!  Sorry for bad information ;-) ;  ShadowWolf, I think you misunderstand what Fruny was calling infantile behaviour. There''s nothing wrong with wanting better grades, but you are sadly mistaken if you think that XOR trick will get you a better grade. If you wrote that kind of code in a class that I was grading, I''d also probably mark you down for showing off. Even if you escaped that, I certainly wouldn''t treat it as any sort of "extra credit" like you seem to be suggesting. ;  I''d mark you down not for "showing off", but for not understanding the general purpose dangers of the method and for not grasping that it is actually less efficient than using a temporary. I''d further mark you down for creating code that isn''t readable and easily comprehensible, and is thus harder to maintain. I''d finally mark you down on the basis of Dennis Ritchie''s comment:quote:It is generally twice as hard to debug a piece of code as it is to write it, so if you write the most clever piece of code you can you are, by definition, not smart enough to debug it. (paraphrased)As for there being nothing in college beyond the letter grade, what about after college? What about the damage it does to the reputation of the institution to graduate students with excellent credentials and imbecilic skills?That''ll be another point off... 