pointer arithmetic

This is a discussion on pointer arithmetic within the C Programming forums, part of the General Programming Boards category; Ok. so I'm writing up the below assignment and have a little snag in the code takes command line ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    46

    pointer arithmetic

    Ok. so I'm writing up the below assignment and have a little snag in the code
    takes command line arguments of one string and two single characters
    locates the first character in the string: if the character was located at position i, then the character at position (i+2) of the string is changed to be the second input character. This operation is repeated until all the sought character occurrences have been found and changed.
    For example, here would be some sample runs of the program:
    /home/you> lab13q1 gbc g Y
    Modified string is: gbY

    /home/you> lab13q1 "abc deaf hiaklar" a X
    Modified string is: abX deafXhiakXar

    the "abc deaf hjaklar" doesn't work propery cause it's trying to read after the string has ended. I tried putting if statement that will print "wtf" once reaches null but it's not working. I'm justt getting 3 XXX at the end of the 2nd example. How do I correct it? Thanks.



    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (int argc, char *argv[]) {
    char *str = argv[1], value1 = *argv[2], value2 = *argv[3], *temp;
    temp = str;
    while ((temp = strchr(temp, value1)) != NULL) {
    if ((temp + 2) != NULL) {
    *(temp + 2) = value2;
    temp++;
    }
    else
    printf ("wtf");
     }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    You also need
    char *end = str + strlen(str);

    Then your if statement becomes
    if ( (temp+2) < end )
    At that point, you know you're still within bounds of the string, and you can change a character.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    Ok. cool...that looks like a better way of doing....for my learning curve though, do you know why what I didn't doesn't work?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Well it's like picking any random integer (say 1234) and then trying to write
    if ( 1234 + 2 != 0 )
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    What do you think about:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (int argc, char *argv[]) {
    char *str = argv[1], value1 = *argv[2], value2 = *argv[3], *temp;
    temp = str;
    while ((temp = strchr(temp, value1)) != NULL) {
    if ( strlen(temp)>2 )
    temp[2] = value2;
    temp++;
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I think you have a very odd way of doing whatever it is you think you are doing.


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

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Odd? You don't know what you say.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Put it this way, if you have a string 1K long full of comma separated values (probably a couple of hundred), you're going to be calling strlen() an awful lot of times (with results that count down steadily from 1000).

    In my example, a pointer to the end is already calculated, so all there is is a simple comparison.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    I took salem's advise and got it working perfectly


    Code:
    char *end = str + strlen(str);
    while ((temp = strchr(temp, value1)) != NULL && (temp +2) < end)
    Now, another part of questions asks to write to stderr....I can write the function and works fine but totally don't get the point of it. Can someone explain why I would use it? Why wouldn't i just use printf without using stderr. Also, can i retrieve the error message from stderr? Good Quality links to read about stderr would be sufficient, if they are for C.

    thanks a million

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer Arithmetic
    By taurus in forum C Programming
    Replies: 5
    Last Post: 11-14-2008, 02:28 AM
  2. Pointer arithmetic
    By depietro in forum C Programming
    Replies: 13
    Last Post: 03-29-2007, 06:03 AM
  3. Arithmetic on pointer...?
    By Volair in forum C Programming
    Replies: 7
    Last Post: 11-22-2005, 12:58 PM
  4. pointer arithmetic
    By xyz4rm in forum C Programming
    Replies: 3
    Last Post: 03-21-2004, 11:56 AM
  5. pointer arithmetic
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-04-2001, 06:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21