Thread: Help with pointers!

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    Help with pointers!

    I have this program that is suppose to give me the reversal of a message that i write, using pointers.

    This is what i have, however, it seems there is a little problem with it because it gives me unknown symbols instead of the inverse words.
    Can anyone tell me what´s wrong on it?

    i added begin = c
    but stills gives me an error..


    Code:
    #include "stdio.h"
    int main()
    {
    int i=0,j;
    char c,str[50],*begin;
    begin=str;
    printf("Enter a message : ");
    
    while( (( c = getchar() ) != '\n') && (i < 49) )
    {
    begin = c;
    begin++;
    i++;
    }
    
    begin = '\0';
    
    printf("Reversal is : \t");
    for(j=i-1;j>=0;j--)
    printf("%c",str[j]);
    printf("\n");
    }
    Last edited by royaro; 03-09-2011 at 07:06 PM. Reason: changes

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    for (j = strlen(str) - 1; j > -1; j--)
      printf("%c";str[j]);
    You only want the occupied part of the buffer, not the whole thing.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( (( c = getchar() ) != '\n') && (i < 49) )
    {
    begin = str;
    begin++;
    i++;
    }
    Every time through your loop, it resets 'begin' to the start of the string. I don't think you want that line inside of your loop.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    for(j=i-1;j>=0;j--)
    printf("%c",str[j]);
    Did you say you were supposed to use pointers?
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays with pointers
    By Niels_M in forum C Programming
    Replies: 18
    Last Post: 07-31-2010, 03:31 AM
  2. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  3. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  4. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 07:28 AM
  5. Pointers pointers pointers....
    By G'n'R in forum C Programming
    Replies: 11
    Last Post: 11-02-2001, 02:02 AM