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.
>
>
>
> To unsubscribe, send a blank message to <mailto:[EMAIL PROTECTED]>. 
> Yahoo! Groups Links
>
>
>
>
>
>
>
>
>        
> ____________________________________________________________________________________
> Don't get soaked.  Take a quick peak at the forecast
> with the Yahoo! Search weather shortcut.
> http://tools.search.yahoo.com/shortcuts/#loc_weather
>
>
> To unsubscribe, send a blank message to <mailto:[EMAIL PROTECTED]>. 
> Yahoo! Groups Links
>
>
>
>
>   

Reply via email to