Thread: Simple Pointer Program

  1. #1
    Registered User
    Join Date
    Apr 2009

    Simple Pointer Program

    I know that there are things on the internet but nothing seems to be helping right now. I am simply trying to reverse this string uses pointers and no strlen or reverse functions from the string library. Can somebody tell me what I am missing?

    void main()
            char*hello = "hello";
            printf("%c", reverse(hello));
            int length;
            for(length = 0; str1[length]; ++length);
            char* left  = str1;
            char* right = left + length - 1;
            char tmp;
            while (left < right) {
                    tmp = *left;
                    *left++  = *right;
                    *right-- = tmp;
            return str1;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    One problem is that hello is a pointer to the first character of a string literal. A string literal must not be modified, but that is what reverse() attempts to do. Rather, you should write:
    char text[] = "hello";
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2008
    Should the printf() control string be "%c" or "%s"?

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    "%c" stands for character. "%s" stands for _string_. So the latter one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Program crashing on free() with an invalid pointer message
    By skreaminskull in forum C Programming
    Replies: 6
    Last Post: 01-23-2009, 05:10 AM
  3. Builder C++ large files support for simple C program
    By Murfury in forum C Programming
    Replies: 3
    Last Post: 11-23-2007, 03:47 PM
  4. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  5. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM