Hi buddies,
char *cTest = new char[10];
printf(" \nThe strlen(cTest) is : %d\n",strlen(cTest));
The output of this printf statement should be 10 but itz giving 14.
help please, why it so..
regards,
ashok
This is a discussion on Regarding C pointers within the C++ Programming forums, part of the General Programming Boards category; Hi buddies, char *cTest = new char[10]; printf(" \nThe strlen(cTest) is : %d\n",strlen(cTest)); The output of this printf statement should ...
Hi buddies,
char *cTest = new char[10];
printf(" \nThe strlen(cTest) is : %d\n",strlen(cTest));
The output of this printf statement should be 10 but itz giving 14.
help please, why it so..
regards,
ashok
You arent initialising that data to anything before testing the length
All strlen does is travel up the string until it finds a zero byte......
When you allocate that memory, it could hold anything........only use strlen when you know there's a string in that area of data
It works if you use the sizeof operator and a "static" array:
Code:char cTest[10]; printf("sizeof(cTest) = %d\n", sizeof(cTest));