Simple Pointer Program

This is a discussion on Simple Pointer Program within the C Programming forums, part of the General Programming Boards category; I know that there are things on the internet but nothing seems to be helping right now. I am simply ...

  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";
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04: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, 02: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

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