Wednesday, 15 February 2012

c++ - What's wrong with this strcpy with pointers function? -



c++ - What's wrong with this strcpy with pointers function? -

i've been tearing apart programme hours trying find program. i've limited couple lines of code, still i'm still stupid (or tired) find issue.

all is string re-create function using pointers. can over?

void stringcopy(char *sourceptr, char *destptr) { while(*sourceptr!='\0') { *destptr=*sourceptr; destptr++; sourceptr++; } }

it's injecting garbage values strings, i'm somehow going out of limits of string.

also, it's used re-create strings of length less 10. declared source , destination arrays of size 20. hard-coded.

i utilize strcpy assignment class , it's not allowed.

edit: forgot input final null char destination! sorry trouble, guys!

simplest strcpyx() function:

void strcpyx(char *dest, const char *src){ while(*src) *dest++ = *src++; *dest = '\0'; }

remember, work reserve plenty space destination.

your destination must terminated '\0'(which isn't in code right now) print correctly!

c++ pointers strcpy

No comments:

Post a Comment