Let's look at your post:
You just post code, and give no description of your issue at all, other than a comment somewhere in all of your code. People that read these forums don't want to read your entire program just to hunt down a comment that may or may not be there. More often than not, it'll just get ignored. The only reason I looked at it, is because I read the thread from the bottom to the top.
Your code is littered with incorrect issues. This is fine, in that you're posting to actually learn something. Or that's what we hope anyway.
First one:
Code:
/* scpy2() tries to allocate memory to d[] inside the body. */
void scpy2(char d[], const char s[]);
Luck for you arrays degrade into pointers to the type of their first element. But luck aside, this doesn't even make sence. You don't allocate space for arrays. Arrays are created at compile time (C99 excluded), as a set size. Thus, you wouldn't be passing an array to this. Your argument you would be passing wouldn't even match up to this function prototype, assuming more luck and somehow it worked out. You'd end up doing something like:
Code:
char *foo;
...
scpy2( foo, ... );
foo isn't an array, so it wouldn't fit your prototype. Again, ignoring the fact that you purely luck out here with array to pointer degradation. The syntax logic of it doesn't fit. People should think more about what it is they're trying to do.
Here's your next one...There's a FAQ on this. Go read it.
Code:
char* b = 0;
char* c = 0;
Zero does not a pointer make. This isn't C++ or Java, this is C. Thus, you should be assigning NULL to pointers. Not zero.
Now why would you dod this?
Code:
printf("b : %s\n", b); // b is yet NULL.
printf("c : %s\n", c); // c is yet NULL.
Don't try and dereference null values. Bad programmer. You luck out again in the way that prinf handles NULL and null characters.
No need to typecast malloc and its family members...
Code:
/* memory to b is allocated in main(). */
b = (char*)malloc(strlen(a) + 1);
There's a FAQ on that too.
You've already been told what's wrong with this.
Code:
scpy2(c, a); // but c is passed as NULL.
But even if you hadn't been, c is not an array. It doesn't match your prototype. Luck again on your part, because I seriously doubt you knew arrays degrade into pointers to their first element, considering the rest of your code. Even if it does, it's ugly syntax the way you attempt to use it.
Code:
printf("c : %s\n", c); // shows c is still NULL.
/* --------------------------------------------- */
/* Why scpy2() behaves for c the way it is? */
/* How can I assign memory to c inside scpy2()? */
/* --------------------------------------------- */
Pointer to a pointer. Or return it and assign it that way. Those are your only options.
Code:
/* scpy1() copies string to a same size char array. */
void scpy1(char d[], const char s[])
{
int i = 0;
int j = 0;
if ((s != NULL) && (d != NULL))
I hate your abuse of syntax here. You don't test an array to see if it is null. Arrays are never null. They just exist. Pointers however, can be NULL.
The rest we've gone over already, so I'll stop there.
Quzah.