Thread: Question on pointer & files

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    38

    Question Question on pointer & files

    Hi every one,
    I am trying to write a function that deletes a whitespace that seperates the last name followed by a ',' from the first name Initial.
    It is part of a compression program that we have to write.

    I am having trouble understanding pointers. So if anyone could help me, that would be great.

    Here the function I have written so far:

    void DelWhiteSpace( char *ptrStr )
    {
    int i;
    char *temp[ strlen( ptrStr ) + 1 ];

    *temp = '\0';

    for ( i = 0; i < strlen( ptrStr ); ++i )
    {
    if( isspace( ptrStr[ i ] ) != 0 )
    strcat( *temp, strchr( *temp, *( ptrStr + 1 ) ) );
    printf( "|%s|\n", temp );
    puts( "three" );
    }
    ptrStr = *temp;
    }

    My output in cygwin is as follows:

    one /*used to identify how far in the main it goes*/
    two /*used to identify how far in the main it goes*/
    three
    three
    three
    Segmentation fault (core dumped)

    Cheers, Johannes

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>char *temp[ strlen( ptrStr ) + 1 ];
    Dynamic sizing of arrays is not supported under C89, only C99 or C++. Which are you coding for?

    Also, do you realise that you created an array of pointers, not an array of chars? Did you mean to have (no asterisk)
    >>char temp[ LENGTH ];

    >>strcat( *temp, strchr( *temp, *( ptrStr + 1 ) ) );
    What are you trying to do there? It's bad practice to embed functions like that, if strchr() fails it will return NULL and the program will crash. I suggest you tell us what it is you want this bit of code to do, and then we can guide you better.

    Also, please read this
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    38
    Thanks for the fast reply. The DelWhitespace function is supposed to go through the string, look for the space( ' ' ) and then get rid of the space and concatenate the stirng in a fashion that there is no whitespaces in the string at all.
    The Compare function is looking through the string to compare each letter and once the letters of the two strings are not the same anymore, a number representing the count of same lettters, concatenated with the letters different in the string read in last. Ie:
    Code:
    Johnson, F
    Johnas, R
    is supposed to be written to file like this:
    Code:
    0Johnson,F
    4as,R
    Cheers,
    Johannes

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You can't use strcat() or strchr() in this manner, for this purpose.

    Here's a simply way to do this:
    Loop through the array, as you find a space, move all the remaining characters left one to cover that space. This will just involve a lot of character swapping on your behalf.

    For now concentrate on the DelWhiteSpace function, move on when you've got it right.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    If you're just trying to remove whitespace, the idea is to attach two pointers to the
    string, one is the iterator, the other the copier. Only increment the copyier if the
    undesired char isn't present. Null terminate when done. If you plot it out on a piece
    of paper, it will make more sense.


    Code:
    
    char * stremove(char * string, char remove)
    {
     char * iter, * copy = string;
    
     for(iter = string; *iter != 0; ++iter)
      if(*iter != remove)
       *(copy++) = *iter;
    
     *copy = 0;
    
     return string;
    }
    
    
    Which is semantically equivalent to:
    
    
    char * stremove(char * string, char remove)
    {
     char * iter, * copy = string;
     int i, j = 0;
    
     for(i = 0, iter = string; iter[i] != 0; ++i)
      if(iter[i] != remove)
       copy[j++] = iter[i];
    
     copy[j] = 0;
    
     return string;
    }
    
    
    Then it's a simple matter of code reuse:
    
    
    char * eatwhite(char * string)
    {
      return stremove(string, ' ');
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Pointer Question
    By krock923 in forum C Programming
    Replies: 12
    Last Post: 12-08-2007, 12:46 AM
  2. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  3. Pointer and segfaults question
    By kzar in forum C Programming
    Replies: 5
    Last Post: 09-15-2005, 09:03 AM
  4. files question
    By Micko in forum C++ Programming
    Replies: 2
    Last Post: 06-14-2004, 02:12 PM
  5. question about pointer?
    By pnxi in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 01:45 PM