Seg Fault within function (New to C)



Hello. I have the following function titled cleanSpace that recieves a
string and cleans it up. My program passes a string to the function via
"cleanSpace(c)". The function works the first time through, but the
second time through I recieve a seg fault [core dump] on the second
last line, strcpy.

I am certain something is wrong with the pointer and the way I use it.
Could somebody identify what I have done wrong at that line?

int cleanSpace(char* ptr)
{
char temp[300];
strcpy(temp, ptr);

int i=0,
j=0,
k=0,
m=0;

/* Ultimately this loop will scan for new lines and tabs and replace
them
with spaces. */
for(i=0; temp[i]; i++)
{
if(temp[i] == '\n' || temp[i] == '\t')
temp[i] = ' ';
}

// For loop finds character starting point.
for(i=0; temp[i] == ' '; i++)
{
temp[m] = temp[i+1];
}

// For loop moves all characters next to the first found character.
for(i++; temp[i]; i++)
{
temp[++m] = temp[i];
}
temp[m+1] = '\0';

// For loop removes trailing spaces.
for(i = strlen(temp) - 1; temp[i] == ' '; i--)
{
temp[i] = '\0';
}

// For loop removes excess spaces.
for(i = 0; temp[i]; i++)
{
if(temp[i] == ' ' && temp[i+1] == ' ')
{
j = i;

while(temp[j] == ' ')
{
j++;
}

for(k = i + 1; temp[k]; k++, j++)
{
temp[k] = temp[j];
}
j=0;
}
}
strcpy(ptr,temp); // Copy temp to ptr
return strlen(temp); // Return the length
}

Appreciate any feedback

- Aaron T

.



Relevant Pages

  • Re: Optimization question
    ... PROCEDURE ForOptimize*(s: ARRAY OF CHAR); ... Calling this with a 75-character string yielded "31" for both loops. ... the sentinel null character. ... With C's "for," you define how you continue the loop, which can be (and ...
    (comp.lang.oberon)
  • Re: Can not find older posting: Reading files (fast)
    ... |> "Sucks up an entire file from PATH into a freshly-allocated string, ... (loop for bytes-read = (read-sequence buffer file-stream) ... CHARACTER; or a type specifier for a finite recognizable subtype of INTEGER; or one of the symbols SIGNED-BYTE, UNSIGNED-BYTE, or ... (loop for line = (read-line s nil nil) ...
    (comp.lang.lisp)
  • Re: Cant Figure Out How to Access a List Item
    ... With the alpha character in front you first have to strip it off to get at ... Personally, if the message ID was an alphanumeric string, I'd first try to ... loop through each subject item and send a new email with each subject ... retrieving the object you want with the MessageID, ...
    (comp.lang.basic.visual.misc)
  • Re: Should function argument be changed in function body?
    ... > have to read for the loop is right there at the loop. ... string, the position of the null character happens to be the same with C ... function which modifies its parameters should be short and 'obviously' ...
    (comp.lang.c)
  • Re: READ FROM FILE
    ... Input string would convert the string to a ... function chrreturn character; ... end str; ... for i in slv'range loop ...
    (comp.lang.vhdl)