Due to server problems the website is temporarily offline! Visit http://www.smorgasbordet.com/pellesc/ to download Pelles C.
You also forgot to free the allocated space.
Quote from: CommonTater on February 12, 2012, 05:55:26 AMYou also forgot to free the allocated space.Well, for doing so, (s)he has to allocate it properly in the first place... Ralf
#include <stdio.h>#include<stdlib.h>#include<string.h>void newStrCpy(char* q, char* p);int main (void){ char src[255]="\00"; printf("\n----------------------------------------"); printf("\nPROGRAM TO COPY THE CONTENTS OF ONE STRING TO ANOTHER "); printf("\n----------------------------------------"); printf("\n\n\t ENTER A STRING...: "); scanf("%s",src); char *dst = malloc(strlen(src)+1); newStrCpy(src,dst); printf("\n\tcopied string is..: %s\n",dst); free(dst); return 0;}void newStrCpy(char* q, char* p){ while( *q != '\0' ) { *p = *q; p++; q++; } *p='\0'; }
CodeCode: [Select]#include <stdio.h>#include<stdlib.h>#include<string.h>void newStrCpy(char* q, char* p);int main (void){ char src[255]="\00"; printf("\n----------------------------------------"); printf("\nPROGRAM TO COPY THE CONTENTS OF ONE STRING TO ANOTHER "); printf("\n----------------------------------------"); printf("\n\n\t ENTER A STRING...: "); scanf("%s",src); char *dst = malloc(strlen(src)+1); newStrCpy(src,dst); printf("\n\tcopied string is..: %s\n",dst); free(dst); return 0;}void newStrCpy(char* q, char* p){ while( *q != '\0' ) { *p = *q; p++; q++; } *p='\0'; }sanide
#include <stdio.h>#include<stdlib.h>#include<string.h> #pragma warn(disable: 2030) char* newStrCpy(char* src) { char *p; // copy pointer char *dst = malloc((strlen(src) + 1) * sizeof(char)); if (!dst) // make sure it worked return NULL; p = dst; while(*p++ = *src++); // copy return dst; } int main (void){ char src[255]={0}; // input buffer char *dst = NULL; // pointer to copy char *nl; // pointer to newline printf("\nPROGRAM TO COPY THE CONTENTS OF ONE STRING TO ANOTHER\n\n"); printf("Enter some text : "); fgets(src,254,stdin); nl = strchr(src,'\n'); // remove trailing newline if (nl) *nl = 0; printf("\nOriginal : %s\n",src); dst = newStrCpy(src); printf("Copy : %s\n",dst); free(dst); return 0;}
#include <stdio.h>int main (void){int a[23]={89,111,117,39,114,101,32,115,111,32,102,117,108,108,32,111,102,32,115,104,105,116,0};for(int i=0;i<23;i++)printf("%c",a);return 0;}sanide
Clean yourself up first.
is this correct and can anybody tell me how to do using double pointers and withoting returning the function like void stringcopy(char **, char *)_________________________#include <stdio.h>#include<stdlib.h>#include<string.h>char* newStrCpy(char* p, char*s);int main(void){ char *src = "mani chandra"; char *dst; printf("%s",newStrCpy(dst,src)); return 0;}char* newStrCpy(char* p, char* s){ int i=0; p = malloc((strlen(s)+1)*sizeof(char)); do { *(p+i)=*(s+i); i++; }while(*(s+i)!='\0'); return p; }
char* newStrCpy(char* s){ char *p; int i=0; p = malloc((strlen(s)+1)*sizeof(char)); // copy only if malloc() succeeds if (p != NULL) { do { *(p+i)=*(s+i); i++; } while(*(s+i)!='\0'); } return p;}
int main(void){ char *src = "mani chandra"; char *dst; dst = newStrCpy(src); printf("%s", dst ); if (dst != NULL) free(dst); return 0;}
void cFree(void **ptr) { free(*ptr); *ptr = NULL; } // called with address of pointer, as... cFree(&dst);
void newStrCpy(char **p char *s){ int i = 0; *p = malloc((strlen(s) + 1) * sizeof(char)); if (*p != NULL) { do { **(p + i) = *(s + i); i++; } while(*(s+i) != '\0'); }} // called with address of destination, as...char *p; newStrCpy(&p,src);