Copy paste and run the example now remove the comment /*test2();*/
in main function ... Its not working .. Whyyyy ?? Behavior is really
queer !!!! I know the method
"int addDynamicMemor y1(char *ptr, int size)" is not the right context
of use here But I want to get the solution to this particular problem
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void test1();
void test2();
int addDynamicMemor y1(char *ptr, int size);
int addDynamicMemor y(char **ptr, int size);
int addDynamicMemor y1(char *ptr, int size)
{
/* See and chek whether size memory is available or not */
int currSize;
if(ptr == NULL)
{
ptr = (char*) malloc(size * sizeof(char));
if(ptr != NULL)
{
printf("Initial ized memory as null \n");
return -1;
}
else
{
printf("Can not Initialized memory as null \n");
return -1;
}
}
currSize = strlen(ptr);
size = currSize + size;
ptr = (char*) realloc(ptr, size*sizeof(cha r));
if(ptr != NULL)
{
printf(" re Allocation size is %d\n",size);
return 0;
}
printf(" re Allocation failed \n");
return -1;
}
int addDynamicMemor y(char **ptr, int size)
{
/* See and chek whether size memory is available or not */
int currSize;
if(*ptr == NULL)
{
*ptr = (char*) malloc(size * sizeof(char));
if(*ptr != NULL)
{
printf("Initial ized memory as null \n");
return -1;
}
else
{
printf("Can not Initialized memory as null \n");
return -1;
}
}
currSize = strlen(*ptr);
size = currSize + size;
*ptr = (char*) realloc(*ptr, size*sizeof(cha r));
if(ptr != NULL)
{
printf(" re Allocation size is %d\n",size);
return 0;
}
printf(" re Allocation failed \n");
return -1;
}
int main(void)
{
test1();
/*test2();*/
}
void test2()
{
char *test = NULL;
addDynamicMemor y(&test, 40);
printf("At first test value is %s\n",test);
strcpy(test,"44 4444444");
printf("After allocation val is %s\n", test);
addDynamicMemor y1(test, 60);
strcat(test,"66 6666666666");
printf("After allocation val is %s\n", test);
addDynamicMemor y1(test, 60);
strcat(test,"88 8888888888888") ;
printf("After allocation val is %s\n", test);
}
void test1()
{
char *test = NULL;
addDynamicMemor y(&test, 40);
printf("At first test value is %s\n",test);
strcpy(test,"44 4444444");
printf("After allocation val is %s\n", test);
addDynamicMemor y(&test, 50);
strcat(test,"55 55555555");
printf("After allocation val is %s\n", test);
addDynamicMemor y1(test, 60);
strcat(test,"66 6666666666");
printf("After allocation val is %s\n", test);
addDynamicMemor y1(test, 60);
strcat(test,"88 8888888888888") ;
printf("After allocation val is %s\n", test);
}
in main function ... Its not working .. Whyyyy ?? Behavior is really
queer !!!! I know the method
"int addDynamicMemor y1(char *ptr, int size)" is not the right context
of use here But I want to get the solution to this particular problem
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void test1();
void test2();
int addDynamicMemor y1(char *ptr, int size);
int addDynamicMemor y(char **ptr, int size);
int addDynamicMemor y1(char *ptr, int size)
{
/* See and chek whether size memory is available or not */
int currSize;
if(ptr == NULL)
{
ptr = (char*) malloc(size * sizeof(char));
if(ptr != NULL)
{
printf("Initial ized memory as null \n");
return -1;
}
else
{
printf("Can not Initialized memory as null \n");
return -1;
}
}
currSize = strlen(ptr);
size = currSize + size;
ptr = (char*) realloc(ptr, size*sizeof(cha r));
if(ptr != NULL)
{
printf(" re Allocation size is %d\n",size);
return 0;
}
printf(" re Allocation failed \n");
return -1;
}
int addDynamicMemor y(char **ptr, int size)
{
/* See and chek whether size memory is available or not */
int currSize;
if(*ptr == NULL)
{
*ptr = (char*) malloc(size * sizeof(char));
if(*ptr != NULL)
{
printf("Initial ized memory as null \n");
return -1;
}
else
{
printf("Can not Initialized memory as null \n");
return -1;
}
}
currSize = strlen(*ptr);
size = currSize + size;
*ptr = (char*) realloc(*ptr, size*sizeof(cha r));
if(ptr != NULL)
{
printf(" re Allocation size is %d\n",size);
return 0;
}
printf(" re Allocation failed \n");
return -1;
}
int main(void)
{
test1();
/*test2();*/
}
void test2()
{
char *test = NULL;
addDynamicMemor y(&test, 40);
printf("At first test value is %s\n",test);
strcpy(test,"44 4444444");
printf("After allocation val is %s\n", test);
addDynamicMemor y1(test, 60);
strcat(test,"66 6666666666");
printf("After allocation val is %s\n", test);
addDynamicMemor y1(test, 60);
strcat(test,"88 8888888888888") ;
printf("After allocation val is %s\n", test);
}
void test1()
{
char *test = NULL;
addDynamicMemor y(&test, 40);
printf("At first test value is %s\n",test);
strcpy(test,"44 4444444");
printf("After allocation val is %s\n", test);
addDynamicMemor y(&test, 50);
strcat(test,"55 55555555");
printf("After allocation val is %s\n", test);
addDynamicMemor y1(test, 60);
strcat(test,"66 6666666666");
printf("After allocation val is %s\n", test);
addDynamicMemor y1(test, 60);
strcat(test,"88 8888888888888") ;
printf("After allocation val is %s\n", test);
}
Comment