Home > Blockchain >  C Programming - String Copier
C Programming - String Copier

Time:11-10

I'm having issues with the following program.

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


void copy(char *dst, char *src) {
    // Code for copying a string goes here
    while(*src != '\0'){ // run loop while last character of src is not terminating character  
        *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   1

    copy(dstString, srcString); // Calling copy function with parameters
    printf("%s", dstString); // Printing dstString == srcString

}

Basically I'm trying to create my own strcpy() function to learn how strcpy() works under the hood. srcString is supposed to be copied to dstString by using pointers. When I run the program in Clion I don't get any output.

CodePudding user response:

Turns out I never decalred that char dstString was an array. Also I needed to include my strlen(srcString) 1 formula within the curly brackets [] to initialize array like so:

char dstString[strlen(srcString) 1];

  • Related