Thread: swaping using pointers

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    12

    Angry swaping using pointers

    I am trying to write a program that takes in a string, reverses the letters in it and displays the result. I have not got it finished but am stuck as I keep getting this error:

    Application Error
    The instruction at "0x004010a7" referenced memory at "0x00000065". The memory could not be "read".

    this is the code that I have

    <code>

    #include <stdio.h>

    char string, change;
    char *ptr;
    int len;
    main()
    {
    printf("Write your string: ");
    string = getchar();


    for(ptr = string; *ptr != NULL; ptr++)
    {
    printf(ptr, "%c", ptr);
    };

    return 0;
    }
    <code>

    Can you help?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    First off, code tags are with square brackets [ ]

    >string = getchar();
    this will only get you one char (hence the name of the function!).

    >printf(ptr, "%c", ptr);
    this is incorrect, have alook in your book for the format of the args for printf().

    >main()
    should be int main(void)

    >char string, change;
    >char *ptr;
    >int len;
    Don't make global variables unless you have to. Put these inside main().

    >0x00000065
    this is the letter A (ascii 0x65). It's probably what you typed in when the program asked for your input. I expect it was caused by the incorrect usage of printf().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    char string, change;
    char *ptr;
    int len;
    You don't use change or len, so there's no point in having them. string is wrong on many different levels, the foremost being that it can only hold one char instead of an entire string of them. The second problem is that all identifiers in C that start with "str" in lower case are reserved by the implementation for further updates. You can consider them reserved words like if and else. You also should use global variables when possible, they are considered bad programming style unless absolutely necessary.
    string = getchar();
    This only takes the first character in the string. The output will probably not be what you want.
    for(ptr = string; *ptr != NULL; ptr++)
    {
    printf(ptr, "%c", ptr);
    };
    First, since string is a char this assignment is broken, you're trying to assign the contents of string to a pointer. Definitely not the effect you want. I've also got bad news for you in the condition of the loop, I doubt that the user will enter a null pointer anywhere in the string, so this loop will continue for who knows how long. This can be fixed by testing for a nul character instead, something like

    *ptr != '\0'

    Your call to printf is also wrong, it should look like this:

    printf ( "%c", ptr );

    This would work much better, but according to your requirements it is still incorrect. The string is indeed printed out, but in the wrong order. You wanted it in reverse:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      char *ptr;
      char astring[BUFSIZ];
      printf("Write your string: ");
      fgets ( astring, sizeof astring, stdin );
      for(ptr = astring; *ptr != '\0'; ptr++)
        printf("%c", *ptr);
      return 0;
    }
    To fix this you can forget about using the pointer safely, so let's just stick with an index that starts at the end of the string and goes to 0.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ( void )
    {
      int ptr;
      char astring[BUFSIZ];
      printf("Write your string: ");
      fgets ( astring, sizeof astring, stdin );
      for(ptr = strlen ( astring ) - 1; ptr >= 0; ptr--)
        printf("%c", astring[ptr]);
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM