Thread: incrementing a pointer instead of incrementing an array and it's index

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    43

    incrementing a pointer instead of incrementing an array and it's index

    so I have the following problem
    Code:
        if(buffer[i]== ' '&&buffer[i+1]==' '&&buffer[i+2]==' ')
            { 
                
    
                buffer[i]= '*';
    
                putchar(buffer[i]);
    
                i = i + 2
    
                continue;  
            }
             
            putchar(buffer[i]); 
    
            i++;
        }
    this part of the code replaces 3 blanks with a *.

    so my challenge is to replace all the array access into pointer-arithmetic

    char *tmp0_pointer; // initialised in main function

    Code:
         if(*tmp0_pointer+1== ' '&&*tmp0_pointer+1==' ' &&*tmp0_pointer+2==' ' )
     
        { 
           tmp0_pointer = buffer+i; 
              
              *tmp0_pointer= '*';
            i = i + 2
    
            continue;  
        }
        tmp0_pointer = buffer+i;
        putchar(*tmp0_pointer); 
    
        i++;
        }
    However the code below doesn't provide the same result.
    I have a feeling I am not incrementing the pointers correctly.
    Could someone help me out ?
    Thank you in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Well you have +1 in two places for starters.

    And the equivalent of a[i] is *(a+i)
    Yes, you need ( ) to effect the correct meaning.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    you might like the C-library function strstr()

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The idea is to replace buffer[i] with say, *current, and get rid of i. So you would have something like this:
    Code:
    current = buffer;
    /* ... */
    if (*current == ' ' && *(current + 1) == ' ' && *(current + 2) == ' ')
    {
        *current = '*';
        putchar(*current);
        current += 2;
        continue;
    }
    If you're still using i, then you might as well use array index notation, because you're trying to index an array, not do pointer arithmetic, even though they amount to the same thing.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. incrementing a pointer
    By lmanukyan in forum C Programming
    Replies: 1
    Last Post: 01-20-2016, 08:35 AM
  2. Pointer Incrementing
    By zoowho in forum C Programming
    Replies: 4
    Last Post: 02-25-2010, 12:42 PM
  3. Incrementing array index in a node.
    By JFonseka in forum C Programming
    Replies: 8
    Last Post: 04-15-2008, 03:39 AM
  4. incrementing a char pointer
    By Bleech in forum C Programming
    Replies: 13
    Last Post: 11-20-2006, 11:25 PM
  5. incrementing an array as formal arg
    By vaibhav in forum C++ Programming
    Replies: 5
    Last Post: 12-27-2005, 08:53 AM

Tags for this Thread