Thread: problem with pointer notation in for-loop

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    Angry problem with pointer notation in for-loop

    Having a bit of problem understanding why I am getting a
    Segmentation fault here
    I know it has something to do with my loop printing the
    reverse of the message array input in first for-loop code

    What is it I am not seeing???

    Code:
    #include <stdio.h>
    
    #define LENGTH 100    /*  maximum length of message array   */
    
    main()
    
    {                            /******************************/
       char message[LENGTH];     /* declare all variables     */
       char *ptr;
    
    
       /*************************************************/
       /* this loop stores message in array with pointer*/
       /*     pointing to it's elements                 */ 
       /*************************************************/
    
       printf("Enter your message :  \n\n");
       for (ptr = (message -1) ; (*ptr = getchar()) != '\n' ; ptr++)  {
         ;
         ptr = "\0";        /*  sets pointer to NULL   */
       }
    
       printf("\n");
    
       /***********************************************/
       /*  this loop take message array and prints    */  
       /*   in reverse using decrementation of pointer*/
       /***********************************************/
    
        printf("Here's your message in reverse:  \n\n");
        for ( ptr ; ptr >= message; ptr--){
          putchar(*ptr);
        }
    
        printf("\n\n");
    
        return 0;
    
    }
    Sue B.

    dazed and confused


  2. #2
    Unregistered
    Guest
    Code:
    for (ptr = (message -1) ; (*ptr = getchar()) != '\n' ; ptr++)  {
    You're assigning a value to a pointer, for this in and of itself isn't illegal, though you will almost never get the results you intended, but you also haven't initialized the pointer (ie. told it to point to a valid area in memory). Try this
    for ( ptr = &(message - 1); ( *ptr = getchar() ) != '\n'; ptr++ ) {

    Though judging from your comments, this would actually be safer
    ptr = &message;
    for ( ; (*ptr = getchar() ) != '\n'; ) {
    Code:
    ptr = "\0";        /*  sets pointer to NULL   */
    Two things. One: a pointer is null if the value is 0, not '\0' so it should be
    ptr = NULL;
    or
    ptr = 0;
    Two: "\0" isn't a null character, it's a string constant as dictated by the double quotes. A single character should be inside single quotes. If you wanted to assign a null character to the element of the array then you want to dereference the pointer first.
    *ptr = '\0';

  3. #3
    Unregistered
    Guest
    Code:
    for ( ptr ; ptr >= message; ptr--){
          putchar(*ptr);
    }
    I'm guessing this would give you an undefined result. In the initialization part of the for loop, what are you assigning to ptr? An array always starts with 0, so the conditional expression
    ptr >= message
    is just asking for trouble.
    Be more explicit with what you want the computer to do or it will give you problems when it tries to interpret your instructions. In this case
    Code:
    for ( ptr = &message[99]; ptr >= &message[0]; ptr-- ) {
    /* 
    **ptr is assigned to the address of the last element of the array
    **so there's no doubt as to where the loop starts. You loop until 
    **the pointer gets to the address of the 0th element of the array.
    **Simple and easy to understand, for both the reader and the
    **compiler.
    */

  4. #4
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    Angry getting garbage as output

    I am almost done here, but still getting
    weird characters before the array prints in reverse
    (last for loop controls this)

    What is still wrong???

    Code:
    #include <stdio.h>
    
    #define LENGTH 100    /*  maximum length of message array   */
    
    main()
    
    {
                                 /*****************************/
       char message[LENGTH];     /* declare all variables     */
       char *ptr;
    
    
       /************************************************************/
       /*  this loop stores message in array with pointer pointing */
       /*     to it's elements                                     */
       /************************************************************/
    
       printf("Enter your message :  \n\n");
       for (ptr = message ; (*ptr = getchar()) != '\n' ; ptr++)
         ;
    
         ptr = "\0";        /*  sets pointer to end of message   */
    
       printf("\n");
    
       /******************************************************/
       /*  this loop take message array and prints           */
       /*   in reverse using decrementation of pointer       */
       /******************************************************/
    
        printf("Here's your message in reverse:  \n\n");
        for ( ptr=&message[LENGTH]-1 ; ptr>=&message[0]; ptr--){
          putchar(*ptr);
        }
    
        printf("\n\n");
    
        return 0;
    
    }


    here's INPUT AND OUTPUT run

    *******************************************

    Enter your message :

    how are you doing?


    Here's your message in reverse:

    ,ü¾ÿ(Èû¾ÿ¼¨<ÿ`3ÿ¼¨<ÿ>ÿ>ÿ /* why is this printing ??? */
    ?gniod uoy era woh /* this is correct output */
    Sue B.

    dazed and confused


  5. #5
    Unregistered
    Guest
    The problem was that while you declared your array, you didn't put anything in it and the chances of the random values put in it being '\0' are very slim, so when you printed out the entire array, you got the garbage that was there initially. To fix this problem, when you declare your array, also initialize it with one '\0' character and that will fill the array with nulls.

    Here's the working code.
    Code:
    #include <stdio.h>
    
    #define LENGTH 100    /* Maximum length of message array */
    
    main()
    {
       char message[LENGTH] = {'\0'};     /* Declare all variables */
       char *ptr;
     
       /*  
       **   This loop stores message in array with pointer pointing
       **   to it's elements.
       */
    
       printf("Enter your message: ");
       for ( ptr = message ; (*ptr = getchar()) != '\n' ; ptr++ ){
           ;
       }
    
       /*  
       **   This loop take message array and prints   
       **   in reverse using decrementation of pointer.
       */
    
        printf("Here's your message in reverse:");
        for ( ptr = &message[LENGTH]-1 ; ptr>=&message[0]; ptr-- ){
    	  putchar(*ptr);
        }
        printf("\n");
    
        return 0;
    
    }

  6. #6
    Unregistered
    Guest

    :) Works perfectly

    Thanks.

    I should say, that is the second type problem where I forget
    about initializing an array.



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM