LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-22-2007, 07:32 PM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
remove all spaces from a line in C


I'm trying to remove all white spaces include in a line.

for ex.
From this: " a b g f "
Become: "abgf"

Here is my code:
Code:
void trim( char* src )
{
  char *dst = src;

  while (*src != 0) {
    if (*src != ' ')
      {
          *dst++ = *src; // copy
      }

    src++;
  }

  *dst = 0;
}
The problem is that, I'm always receiving SIGBUS error and I don't know why.

1 - What am i doing wrong?
2 - Is there a form of get more detail about the causes of the error during the execution?

Thanks,
 
Old 10-22-2007, 07:37 PM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
check the length of the string and how many iterations of the while loop occur.
Do you have a C programming reference book as you seem to be having a few problems with pointers at the moment.

Last edited by dmail; 10-22-2007 at 07:39 PM.
 
Old 10-22-2007, 07:39 PM   #3
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
The SIGBUS occur when executing:
*dst++ = *src; // copy

I don't know why.
 
Old 10-22-2007, 07:41 PM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
http://en.wikipedia.org/wiki/Segmentation_fault
 
Old 10-22-2007, 07:42 PM   #5
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
I've debugged the code, and the pointers points to the right thing.
 
Old 10-22-2007, 07:46 PM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
while (*src != 0)
Quote:
check the length of the string and how many iterations of the while loop occur.
This is not doing what you may think, try *src != '\0'
 
Old 10-22-2007, 07:47 PM   #7
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
Ie, the pointers point to the same value.

The reason for this error, it's because, pointer dst is setting the same value as src pointer points.

Is there another way to remove all black spaces without errors?
 
Old 10-22-2007, 08:03 PM   #8
turbo_spool
LQ Newbie
 
Registered: Oct 2007
Location: Rome, NY
Distribution: Slackware, openSUSE, Fedora Core
Posts: 18

Rep: Reputation: 0
Quote:
Originally Posted by pedrosacosta View Post
The SIGBUS occur when executing:
*dst++ = *src; // copy

I don't know why.
dst is a copy of a pointer (src) to some char.

*dst dereferences dst so that it is saying 'the actual memory that dst is pointing to'. Without the * dst is just 'the memory address of a char'. At the beginning of your routine you have:

char *dst = src;

Then later:

*dst++ = *src;

That says 'put whats at src (which can be the same exact location as dst) into dst'. This causes a memory error. Better understanding of pointers and memory locations will help you.

I always say the best book to understand using pointers in the C language is the one written by its creator: Kernighan & Ritche's 'The C Programmming Language'.

Hope this helps.

P.S. This is one of the reasons people shy away from C in certain situations. C doesn't hold your hand. As you found you, your code compiled just fine, but then bombed out with a memory error.

Welcome to the world of dynamic memory allocation.
 
Old 10-22-2007, 08:17 PM   #9
turbo_spool
LQ Newbie
 
Registered: Oct 2007
Location: Rome, NY
Distribution: Slackware, openSUSE, Fedora Core
Posts: 18

Rep: Reputation: 0
Well, by the time I posted that, you found the answer.

To answer the next question, it depends on the situation.

For example:

Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
  char *message = "This is a null terminated string.";

  char *new_memory = malloc( sizeof(message) );
  char *pointer = new_memory;
  while (*message != (char)NULL)
  {
    if (*message != ' ')
    {
      *new_memory++ = *message;
    }

    message++;
  }
  *new_memory = '\0';
  printf("%s\n", pointer);

  pointer = NULL;
  free(pointer);
  return 0;
}
When I compile and run this I get:

Thisisanullterminatedstring.

I put it all in a main function since it was a one off specific example. I hope this helps, but again I must recommend a good book like The C Programming Language to truly get going on the subject. I just wanted to help get you going.

Also, this does not perform explicit error checking, which you should always do. For instance:

Code:
char *memory = malloc(SIZE);
if (memory == NULL) {
  // handle memory allocation error
}
Have fun!

Last edited by turbo_spool; 10-22-2007 at 08:24 PM. Reason: Wow, made a memory error typo myself when copying over here from xemacs. Good thing I checked it :p
 
Old 10-22-2007, 08:29 PM   #10
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
My C is rusty and I'm sorry I have posted incorrect information.

[edit]
Your code is valid, the problem is arising due to the string being in a read only section of memory( ie on the stack).

I still prefer while (*src != '\0') to while (*src != 0) though

Last edited by dmail; 10-22-2007 at 08:45 PM.
 
Old 10-22-2007, 08:46 PM   #11
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
I hope this helps

Quote:
The C Programming Language, 270 pages,
by Brian W. Kernighan and Dennis M.Richie, 2nd Edition,
Published by Prentice Hall
ISBN 0-13-110362-8 (paperback)
ISBN 0-13-110370-9
 
Old 10-23-2007, 01:28 AM   #12
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
turbo_spool, your code fine.

But the problem, is that, If I put you code inside a function, the malloc that you do, remove the space memory at the exit of the funcion.

Code:
void remove(char* message, char* new_memory)
{
 char *new_memory = malloc( sizeof(message) );
  char *pointer = new_memory;
  while (*message != (char)NULL)
  {
    if (*message != ' ')
    {
      *new_memory++ = *message;
    }

    message++;
  }
  *new_memory = '\0';
  printf("%s\n", pointer);

  pointer = NULL;
}
 
Old 10-23-2007, 01:29 AM   #13
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
Please replace,
char *new_memory = malloc( sizeof(message) );
for
new_memory = (char*)malloc( sizeof(message) );
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script to remove spaces from multiple filenames jeffreybluml Linux - Newbie 36 07-31-2013 02:10 AM
How to remove spaces from a string pawan_songara Programming 14 08-30-2006 09:20 PM
please help me remove the spaces in filenames in a directory! asilentmurmur Linux - Newbie 3 07-14-2006 07:17 PM
Is it possible to remove spaces Alex_jacobson Linux - General 4 01-16-2005 10:45 AM
Spaces on command line odd Linux - Software 2 05-22-2004 08:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration