Question about my trim function and pointers

This is a discussion on Question about my trim function and pointers within the C Programming forums, part of the General Programming Boards category; Hey, I wrote this function to eliminate all leading and trailing spaces from a string.. Code: void trim(char** str) { ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    23

    Question about my trim function and pointers

    Hey, I wrote this function to eliminate all leading and trailing spaces from a string..

    Code:
    void trim(char** str) {
    	char ptr[strlen(str)+1];
    	int i = 0, j = 0;
    	while (isspace(str[i]) == 1) {
    			i++;
    		}
    	while (str[i] != '\0') {
    		ptr[j++] = str[i++];
    	}
    	ptr[j] = '\0';
    	int n = strlen(ptr);
    	while (--n >= 0) {
    		if (isspace(ptr[n]) == 0) {
    			break;
    		}
    	}
    	ptr[n+1] = '\0';
    	str = ptr;
    }
    ..and have a quick question about ways of using it, I know to call the above I should:

    Code:
    char *aString = " trim this string ";
    trim(&aString);
    right? due to the passing by reference thing.. but i was thinking if the following changes were made, the result will be exactly the same? just hoping someone can affirm my thoughts.

    Code:
    char *trim(char* str) {
    /* trim code.. */
    return str;
    }
    
    aString = trim(aString);
    is one way better style than the other?

    thanks.
    Last edited by space-oddity; 04-27-2008 at 02:15 AM.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    That isn't exactly C compliant... but as long as your C++ compiler is cool with it, I guess I will let it slide. Well firstly, you can't use an = sign to fill a string buffer. Secondly, I recommend doing the bottom one, only because you can always add error checking and have the errors lead to returning NULL if something goes horribly awry.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    i just edited my code and posed the question a little differently

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    char *aString = " trim this string ";
    aString should be declared as const and cannot be modified
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    Quote Originally Posted by vart View Post
    aString should be declared as const and cannot be modified
    what, i can't modify it, meaning i can't use my function on it?

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    That isn't exactly the case you presented that I agreed to. Your code was initially something to the tune of:

    Example
    Code:
    char aString[20] = "     sloppy string    ";
    
    char *trim(char *);  // was the one I called "a good idea"
    Which is the way to do it. A string literal is always going to be a constant value that can cause pandora's box to be thrust open upon altering. The double indirection is not necessary for your trim function. The parameter is a pointer, you aren't going to be altering the pointer itself, so you may as well not complicate your code with extra dereferencing.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by space-oddity View Post
    what, i can't modify it, meaning i can't use my function on it?
    You can, as I said... but you will have to face the consequences of opening pandora's box. And between you and me.... you don't want the box.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    okay, thanks. so the code at the bottom of the first post is the way to go?

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I believe so for no other reason than error handling. Since you can't throw exceptions or anything in C you should just have errors returned by the function (when possible). In this case lets say the user passes in 0, then having an return value would help the developer debug and know "oh duh, I must've passed in 0 to begin with. Where is malloc() not called." Right?

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by space-oddity View Post
    what, i can't modify it, meaning i can't use my function on it?
    If you did this:
    Code:
    char aString[20] = "     sloppy string    ";
    then you can modify it.
    However, if you did this:
    Code:
    char *aString = " trim this string ";
    then you cannot (or at least, you shouldn't) modify it, because it points to a literal string constant which (depending on compiler/platform) could be stored in Read Only memory.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Also note how your first example is wrong (you should at least get a warning). You are trying to assign an array (one pointer) to a pointer-to-pointer. They are not the same types. As you know, to get the original value, you must dereference the pointer first, so you'd do something like *pOriginal = ptr.
    However, now we get another problem. pOriginal would point to your array inside the function which is placed on the stack. Therefore, that array will be cleaned up and destroyed once the function ends and pOriginal will point to garbage afterwards.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question about pointers in function parameters.
    By sojurn in forum C++ Programming
    Replies: 14
    Last Post: 01-20-2007, 08:21 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

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