Coder wrote:
> Patrick wrote:
>
>>Hello
>>
>>I have the following "easy" problem. I have a string which contains
>>1000 chars.
>>Now my task is to cut the string at his 650 position. I tried strcpy,
>>to copy the first 650 chars into another string but I got a memory
>>access error. I have heard that i can use this function only to copy
>>500 chars is that true? What other options do i have?
>>
>>Best Regards
>>Patrickint
>
>
> I think the following code solves ur problem...
>
When you answer, try to be sure that what you are proposing works ok...
> main()
> {
Shoyld always be
int main(void)
{
> char st1[1000],st2[650];
> int i;
>
> for (i = 0; i < 1000; i++)
> st1[i] = 'a';
>
> st1[i] = '\0';
You have already passed the limits of your st1... You try to access
st1[1000] which is wrong and produces an error in a well-written system..
>
> strncpy(st2,st1,sizeof(st2));
> st2[650] = '\0';
Same mistake here. Moreover, strncpy() does copy the ending '\0' so
there is no need for the erroneous st2[650] = '\0';
>
> for (i = 0; st2[i] != '\0'; i++)
> printf("%d\t%c\n",i,st2[i]);
>
> printf("%d",i);
>
And add a
return 0;
> }
>
--
one's freedom stops where other's begin
Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.