Seg Fault within function (New to C)
- From: "AMT2K5" <Aaron.Train@xxxxxxxxx>
- Date: 17 Jul 2005 10:02:06 -0700
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
.
- Follow-Ups:
- Re: Seg Fault within function (New to C)
- From: akarl
- Re: Seg Fault within function (New to C)
- From: Old Wolf
- Re: Seg Fault within function (New to C)
- From: Arthur Huillet
- Re: Seg Fault within function (New to C)
- From: Emmanuel Delahaye
- Re: Seg Fault within function (New to C)
- Prev by Date: Re: how to get the data type of a veriable
- Next by Date: Re: Implicit addition of const qualifiers
- Previous by thread: database of C standard library functions?
- Next by thread: Re: Seg Fault within function (New to C)
- Index(es):
Relevant Pages
|
|