c++ - My C program is crashing when I call a method from a class instance here is the code -
here class using.
#include<stdio.h> #include <string.h> class ende{ private: int *k; char *temp; public: char * encryptstring(char *str); char * decryptstring(char *str); ende(int *key);}; ende::ende(int *key){ k=key; } char * ende::encryptstring(char *str){ int t=2; t=(int)k[1]*(int)2; (int i=0;i<strlen(str);i++){ temp[i]=str[i]+k[0]-k[2]+2-k[1]+k[3]+t; } char alp=k[0]*57; (int y=strlen(str);y<strlen(str)+9;y++){ //--* temp[y]=alp+y; //--* } temp[(strlen(str)+9)]='\0'; //--* return temp; } char * ende::decryptstring(char *str){ int t=2; t=k[1]*2; (int i=0;i<strlen(str);i++){ temp[i]=str[i]-t-k[3]+k[1]-2+k[2]-k[0]; } temp[(strlen(str)-9)]='\0'; return temp; }
and here main program.
#include <stdio.h> #include "ende.h" int main(void){ char *enc; char op; printf("\ne encrypt , d decrypt"); int opi[4]={1,2,9,1}; ende en(opi); strcpy(enc,en.encryptstring("it c's lounge!! ")); printf("encrypted : %s",enc); return 0; }
something wrong en.encryptstring function
when run program stops working giving error , on removing strcpy(enc,en.encryptstring("it c's lounge!! ")); runs. want problem resolved.
char *enc; strcpy(enc,en.encryptstring("it c's lounge!! "));
you don't provide space copy - enc doesn't point anywhere.
Comments
Post a Comment