C Programming - String Copier

Hi guys,

I can’t get my head round this program. I’ve spent hours trying get it up and running an I’m still getting errors. Can anyone point in the right direction.

#include<stdio.h> #include<string.h> void copy(char *dst, char *src) { while(*src != '\0'){ // while the last character of src is not terminating character enter loop *dst = *src; // copying values from src to dst src++; // increment by 1 dst++; // increment by 1 } *dst = '\0'; // ending string } int main(){ char srcString[] = "We promptly judged antique ivory buckles for the next prize!"; char dstString = strlen(srcString) + 1; // dstString == length of srcString + terminating character copy(dstString, srcString); // Calling copy function with parameters dstString and srcString printf("%s", dstString); // Printing dstString }

Line 15 is not properly formatted. You did:

char dstString = strlen(srcString) + 1;

Instead of:

char dstString[strlen(srcString) + 1];

1 Like