I was trying to implement "strcat" myself,
however, I found that the following commands simply won't work:

".....
char *p;
p="string";
*(p+2)=a;
"


I was thinking that this will change "p" to
"staing". But no.
It compilies fine, but when I tried to print out p, i got error message: "Bus Error".

Thanks for helping me clear the issue.

char str[10]={0x0};
char *p=str;
strcpy(str,"string");
*(p+2)='a';

You have to allocate storage in memory for data - first. Then place data in the storage next - if you want to be able to modify the data.

Thanks for reply!

char str[10]={0x0};
char *p=str;

-----Storage is already allocated, at this point. Isn't it?

strcpy(str,"string");

-----The whole point, rather, is that I wish to write my own "strcpy", "strcat". Just for understanding purposes.


char str[10]={0x0};
char *p=str;
strcpy(str,"string");
*(p+2)='a';

You have to allocate storage in memory for data - first. Then place data in the storage next - if you want to be able to modify the data.

Strings: Concatenation

Thanks!

I played around and foud:

1.
if I do,
char p[10]="string";
char *q;
q=mystrcat(p, " test");

Things work.

2.

If I do:
char p[10];
p="string";-----Error Message! Why?

3.

char *p="string";
char *q;
q=mystrcat(p, " test");---Compile, but "Bus error", if print out "q". Why?

Thanks!!!

2.

If I do:
char p[10];
p="string";-----Error Message! Why?

An array is not a pointer. Arrays cannot be assigned.

3.

char *p="string";
char *q;
q=mystrcat(p, " test");---Compile, but "Bus error", if print out "q". Why?

A string literal is not modifiable, and p points to a string literal.

-----The whole point, rather, is that I wish to write my own "strcpy", "strcat". Just for understanding purposes.

Another one to compare with:
Strings: Copying

Thank you SO MUCH!!

An array is not a pointer. Arrays cannot be assigned.

A string literal is not modifiable, and p points to a string literal.

Another one to compare with:
Strings: Copying

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.