I'm a beginner programmer in C so excuse me if my language/style is not professional.
Question: I'm trying to determine the size of a character array, so that I can later on know the size to allocate using malloc().
My code:
char *dest;
char *src1 = "Test String";
char src2[] = "Testing!";
dest = (char *) malloc( sizeof(src1) );
The problem is I want to determine the size in memory of src1 and src2. i.e. size of src1 should be 11, and size of src2 should be 8. But the results I have received from sizeof() are:
- sizeof(*src1) is 1 since that the sizeof(char)
- sizeof(src1) is 4 <-- I don't know why can someone explain please?
- sizeof(src2) is 9 <-- I'm guessing this is what I want and the null terminator counts into the size as well
But then how to properly determine size of src1? and how come sizeof(src2) works but sizeof(src1) doesn't?
If I'm unclear in what I'm trying to do, please reply and I'll try to make it clearer.
Thanks in advance.