#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;

int main(){
char s[100];
cout<<"enter the string :"<<endl;
gets(s);
//memcpy
char a[100];
memcpy(a,s,strlen(s)+1);//here if we decrease bytes to be copied to a value less than length of s i.e.(strlen(s)), then s is getting destroyed ...some garbage value is coming
cout<<" a : "<<a<<" s : "<<s<<endl;
}

So, what's your question?

);//here if we decrease bytes to be copied to a value less than length of s i.e.(strlen(s)), then s is getting destroyed ...some garbage value is coming

Try null-terminating a after you copy. You'll probably be seeing random characters after the copied string.

thnx for ur reply ....but it is not still clear to me that while we are copying the value of char array s to char array a......should it happen that s gets some random characters?????y calling some function is effecting s.....we are just copying s to a.

I don't think s is being affected. Only a is changed by memcpy. Try the following to see that you need to null terminate your string in a.

int main(){
   char s[100];
   cout<<"enter the string :"<<endl;
   gets(s);
   //memcpy
   char a[100];
   ZeroMemory(a, sizeof(a));  // fill array with a know quantity
   memcpy(a,s,strlen(s)+1);//here if we decrease bytes to be copied to a value less than length of s i.e.(strlen(s)), then s is getting destroyed ...some garbage value is coming
   cout<<" a : "<<a<<" s : "<<s<<endl;
   }

Note the ZeroMemory command.

memcpy(a,s,strlen(s)+1) instead if this run the same prgm using memcpy(a,s,strlen(s)-1)...you will get to know that s is changing.

Note the ZeroMemory command.

What is the point of replying to you if you don't bother to listen.

memcpy(a,s,strlen(s)+1) instead if this run the same prgm using memcpy(a,s,strlen(s)-1)...you will get to know that s is changing.

If I run that, a gets displayed as whatever I typed in less the last char; how is this surprising in any way??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.