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
    Posts
    22

    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?

    Code:
    #include<stdio.h>
    
    void main()
    {
            char*hello = "hello";
            char*reverse(char*str1);
            printf("%c", reverse(hello));
    }
    
    char*reverse(char*str1)
    {
            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
    Location
    Singapore
    Posts
    23,773
    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:
    Code:
    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
    Location
    TX
    Posts
    2,059
    Should the printf() control string be "%c" or "%s"?

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    "%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