for loop question

This is a discussion on for loop question within the C Programming forums, part of the General Programming Boards category; Yes, This may sound like the rest, but this actually has to do with something different. I'm not quite understanding ...

  1. #1
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Arrow for loop question

    Yes,

    This may sound like the rest, but this actually has to do with something different. I'm not quite understanding why im running into the problem. Firstly, here is the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void removeLetters(char *str, int n);
    
    int main() {
    	char str[64] = {"We Remove First And Last Three Letters Now"};
    
    	removeLetters(str, 3);
    
    	printf("%s\n", str);
    
    	return 0;
    }
    
    void removeLetters(char *str, int n) {
    	int i, len, pos = 0;
    
    	len = strlen(str);
    
    	// Go through loop starting at 'n'
    	for (i = n; i < len && pos != len-n*2; i++, pos++) {
    		str[pos] = str[i];
    		str[pos++] = '\0';
    	}
    }
    Next, this is not homework, I have done this code inside out a billion times, but this time it hasn't worked. Though my second favorite way to do this is:

    Code:
    void removeLetters(char *str, int n) {
    	memcpy(str, &str[n], strlen(str));
    	str[strlen( str )-n] = 0x00;
    }
    Though I am trying a different approach. My problem so far is that "pos" never increments in my for loop statement but if I put pos++ inside the statement, not the call it works fine. Example:

    Code:
    for (i = n; i < len && pos != len-n*2; i++) {
    	str[pos] = str[i];
    	pos++;
    	str[pos] = '\0';
    }
    To clarify what I want to do is trim off the first 3 and the last three letters of my sentence. I just dont like having to do at the end the whole "str[strlen(str)-n]" thing. That's why I have tried to write another way.

    Is there a simple explanation why "pos" won't increment in my for loop call?


    Thank you for your time,
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    One possible problem can occur with
    Code:
    memcpy(str, &str[n], strlen(str));
    memcpy is not safe for overlapping memory areas

    Code:
    for (i = n; i < len && pos != len-n*2; i++, pos++) {
    str[pos] = str[i];
    str[pos++] = '\0';
    }
    This would increment pos twice. Also you are assigning str[post] to str[i] and then overwriting it with null.

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Code:
    str[pos++] = '\0';
    this line will incriment position after it dereferences the array and performs the assignment. You could use the prefix version, ++pos, which will incriment it first.
    Its simliar to changing a math equation with parentheses:

    pos++ -> str[pos] = '\0'; pos = pos + 1;
    ++pos -> pos = pos + 1; str[pos] = '\0';

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Actually he should get rid of that incrament all together

  5. #5
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    hmm...you're are correct sir. It would be safer to wait until you have processed the entire string before you set the position of the null term.

  6. #6
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Alright,

    A smoother approach:

    Code:
    void removeLetters(char *str, int n) {
    	int i, len, pos = 0;
    
    	len = n*2 > (int)strlen(str) ? n : strlen(str);
    
    	// Go through loop starting at 'n'
    	for (i = n; i < len && pos != len-n*2; i++) {
    		str[pos++] = str[i];
    	}
    
    	str[pos] = '\0';
    }
    Is that more reasonable?

    - Stack Overflow
    Last edited by Stack Overflow; 06-09-2004 at 11:30 PM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  7. #7
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    that looks nicer, and winds up being better since you're not assigning the null term all the time. Where you incriment pos is strictly your preference. I usually try to keep only one statement in the update step, but every now and then I get rebellious.

  8. #8
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Thanks for the help,

    I just wasnt to sure if my implementation was concise. My code now runs beautifully without memcpy worries, etc... Thanks once again!

    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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