char *t1 = malloc(sizeof(char)*10);
char *t2 = malloc(sizeof(char)*10);
writing above statements is not ANSI standard and is wrong to get desired result because malloc returns void* (pointer to void mean it returns pointer that points to nothing)
you have to parse the pointer returned by malloc as given below
char *t1 =(char*) malloc(sizeof(char)*10);
char *t2 = (char*)malloc(sizeof(char)*10);
sencondly, t1 and t2 do not hold values, t1 and t2 hold memory address as allocated by malloc so the statement t1=t2 does not assigns the values pointed by t2 but it assigns the address of the location pointed by t2 and after that statement t1 and t2 will hold same address or they will point to the same memory location. if you want to copy values
*t1 = *t2; is the statement.
char *t2 = malloc(sizeof(char)*10);
writing above statements is not ANSI standard and is wrong to get desired result because malloc returns void* (pointer to void mean it returns pointer that points to nothing)
you have to parse the pointer returned by malloc as given below
char *t1 =(char*) malloc(sizeof(char)*10);
char *t2 = (char*)malloc(sizeof(char)*10);
sencondly, t1 and t2 do not hold values, t1 and t2 hold memory address as allocated by malloc so the statement t1=t2 does not assigns the values pointed by t2 but it assigns the address of the location pointed by t2 and after that statement t1 and t2 will hold same address or they will point to the same memory location. if you want to copy values
*t1 = *t2; is the statement.
No comments:
Post a Comment