StringCopy project in C

I was largely confused as to the how and to the why with this program. First of all, why are we incrementing dst and src prior to the int main? How is this While loop able to operate prior to the int main? Pardon my ignorance as I ma new to coding, if I have asked something stupid I apologize in advance.

Are dst and src apart of dstString and srcString? Or are all 4 variables independent from one another?

lastly when I got to run the program the output of dstString is only 6 characters long and then it stops printing the rest of the string… If someone would be so kind as to reveiw my code I have pasted it below.

#include<stdio.h>
#include<string.h>

void copy(char* dst, char* src){
// Code for copying a string goes here

while(*src != ‘\0’){

*src = *dst;
//*dst = *src;
src++;
dst++;

//printf(“%p\n”, *dst);
}
*dst == ‘\0’;

}

int main(){
char srcString = “We promptly judged antique ivory buckles for the next prize!”;

int len = strlen(srcString) + 1;

char dstString[len];

copy(srcString, dstString);

printf(“%s\n”, dstString);
printf(“%s\n”, srcString);

}

This will output

We pro
We promptly judged antique ivory buckles for the next prize!

Any advice would be much appreciated.

Hi, I am new to this as well but I will try to answer your questions.

dst/src are directly pointing to the memory location where dstString/srcString are stored, as dst/src where declared with * making it a pointer variable.

Looking at your code you forgot to use the strcpy() function in your while loop. As well, when you use == that is for comparison, where a single = is for assigning. Replace *dst == ‘\0’; with *dst = ‘\0’;

My code is seen below and it does work but I lack some understanding as well. A question I have which I am hoping someone can answer is:

When srcString/dstString char arrays are passed to the copy function will it always just assumed in the first round of the while loop that dst/src are already initialized and pointing to dst[0] /src[0]? or is it safer to initialize it before hand some how? Or am I going about this the wrong way?

Any insight is appreciated.

#include<stdio.h>
#include<string.h>

void copy(char* dst, char* src){

while (*src != ‘\0’){
strcpy(dst, src);
src++;
dst++;
}
*dst = ‘\0’;
}

int main(){
char srcString = “We promptly judged antique ivory buckles for the next prize!”;
int len = strlen(srcString) + 1;
char dstString[len] = {};
copy(dstString, srcString);
printf(“%s\n”, dstString);
}

This is how i approached the StringCopy project in C.
I commented out the things I tried that did not work or were incorrect.
I interpreted the while loop to continue iterating/looping until the null terminating operator was included.
I expected this project was intended to have us make use of the reference operator (&) and the dereference operator(*) which didn’t seem to be the case. My code is similar to blog4939900252. I also noted the same errors referenced to teraace61516.
I made use of the reference operator to ouput the memory addresses for src and dst.
And I included a printf statement to confirm that dst is the same as src.
But I too am new to C, so while I feel this project was completed successfully perhaps I made an error I do not see.

#include<stdio.h>
#include<string.h>

void copy(char* dst, char* src){
while(*src != ‘\0’){
//strcpy(*dst, *src);
//*src = *dst;
strcpy(dst, src);
src++;
dst++;
//*dst = ‘\0’;
//printf(“%p\n”, &src); //src memory address
}
//*dst = ‘\0’;
printf(“%p\n”, &src); //src memory address
printf(“%p\n”, &dst); //dst memory address
printf(“%s\n”,“”);
}

int main(){
char srcString = “We promptly judged antique ivory buckles for the next prize!”;
char dstString[strlen(srcString)];
copy(dstString, srcString);
printf(“%s\n”, srcString);
printf(“%s\n”, dstString);

OUTPUT
0x7ffde7b8bd30
0x7ffde7b8bd38

We promptly judged antique ivory buckles for the next prize!
We promptly judged antique ivory buckles for the next prize!

Looking at your code learningcodetoday I do see that you commented out //*dst = ‘\0’; after your while loop. All though the program will still work, you should always have the null terminating character /0 to indicate the end of a string.