Hi, Roberto,
What you wanna do is done quite simply by defining "ltrim" function
as follow. You don't have to use dynamic memory allocation.
char *
ltrim(char *str)
{
while ((*(str++) == ' '));
return str - 1;
}
I don't think the solution where you use "char s0[]" instead of
"char *s0" in main function is correct. Indeed, you can avoid the
error resulting from modifying the read-only memory region,
but local variables s0[0], s0[1], ... in main function will change
after calling "ltrim", which is beyond the scope of your assumption,
i.e., "s0" and "d0" point at the same address.
I hope this helps.
----- Original Message ----
From: Knowledge Seeker <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
Sent: Wednesday, April 11, 2007 7:38:02 PM
Subject: Re: [c-prog] Re: Segmentation fault in a simple function.
Do you know how internally the 2 statements represent in memory ??
Look in for embedded comments !
Kayo Hisatomi wrote:
> Hi Roberto,
>
> You're welcome, but read the other comments since they have
> important information, including programming practice and a
> better explanation to the reason of the crash you were getting.
>
> Regards,
> Kayo
>
> ----- Original Message ----
> From: Roberto <[EMAIL PROTECTED]>
> To: [EMAIL PROTECTED]
> Sent: Wednesday, April 11, 2007 5:07:09 PM
> Subject: [c-prog] Re: Segmentation fault in a simple function.
>
> --- In [EMAIL PROTECTED], Kayo Hisatomi <[EMAIL PROTECTED]> wrote:
>
>> Hi Roberto,
>>
>> The problem is in this line in the main() function:
>>
>> char *s0 = " bye 1 ";
>>
10 11 12 13
-----------------------------------
| b | y | e | \0 |
------------------------------------
^
|
|
s0 (100)
------
| 10 |
--------
In other words s0 (whose address is 100) contains the address of start
of the string literal (10); hence it 'points' to "bye" string literal.
>> You are not allocating memory for the string, but just for
>> the pointer. So, try this:
>>
>> char s0[] = " bye 1 ";
>>
10 11 12 13
-----------------------------------
| b | y | e | \0 |
------------------------------------
s0
In other words the s0 (whose address is 10) is itself start of string
literal "bye" ; or; s0 itself represent the base-address of the "bye"
string literal.
>> And the program will work.
>>
>> Regards,
>> Kayo.
>>
>>
>
> Yes, this was the problem!
> I was confused with the two rows above.
> To assign a string to an array is right, but doing the same to a
> pointer need a strcpy or sprintf with the pointer of an allocated
> memory area, as you said.
>
> Thanks a lot, I spent a week to search the problem in the function,
> but the problem was out of it.
>
> Now it works.
____________________________________________________________________________________
Bored stiff? Loosen up...
Download and play hundreds of games for free on Yahoo! Games.
http://games.yahoo.com/games/front
To unsubscribe, send a blank message to <mailto:[EMAIL PROTECTED]>.
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/c-prog/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/c-prog/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:[EMAIL PROTECTED]
mailto:[EMAIL PROTECTED]
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/