I was testing around with sprintf
a bit to convert an int
into a string
/char
and noticed something:
string
like thischar *str = "anything";
no matter what I write in it, sizeof
always returns a value of 8 byte.
But as far as I know, a char
is ususally the size of 1 byte, so why doesn't the number of characters in the string/array change its size?
If I write
char str[15];
I get a size of 15 bytes, so why do I not get a size of 15 bytes if I type
char *str = "11111111111111\0"
but a size of 8 bytes?
char str[] = {"67"}
sizeof
returns a value of 3 byte, which makes sense since it is 6, 7 and \0.
And then I write this
int aInt = 368;
sprintf(str, "%d", aInt);
to convert int aInt 368
into a string and write that string into str
.
These are three char
then, and therefor, sizeof
should return a value of 4 byte, counting \0 in.
But it still returns 1, even through I wrote 3 chars.
Can anyone explain this to me, please?
This is the piece of code with which I've been testing this:
#include <stdio.h>
int main(int argc, char *arfv[])
{
int aInt = 368;
printf("value of aInt: %d\n", aInt);
char str[15];
//Other ways of creating a string/array which were tested:
//char *str = "anything";
//char str[] = {"67"};
//printf("value of str before: %s\n", str); //value of str before converting
printf("size of str before: %ld\n", sizeof(str)); //size of str before converting
sprintf(str, "%d", aInt); //convert aInt into a string
printf("value of str after: %s\n", str); //value of str after converting
printf("size of str after: %ld\n", sizeof(str)); //size of str after converting
return 0;
}
char *A is a character pointer. it's another way of initializing an array of characters, which is what a string is. char A, on the other hand, is a single char. it can't be more than one char.
While their size is the same in practice, their alignment requirements are not. If you are doing "kernel coding", that already limits the number of platforms the code can run on. Check with the OS spec what is supported.
char *str - This is a single value which is a pointer to a character. When used as a string in C it is implied that in subsequent memory addresses are subsequent charcters in the string, until the end of the string which is marked with a NULL (ascii 0)
well, char * means a pointer point to char, it is different from char array. char amessage[] = "this is an array"; /* define an array*/ char *pmessage = "this is a pointer"; /* define a pointer*/ And, char ** means a pointer point to a char pointer.
A char*
is just a pointer to some memory, and has a size of whatever a pointer is on your architecture (8 bytes would indicate a 64-bit architecture, 4 bytes would indicate 32-bit).
Since pointers convey no information about the size of the allocation they point to, sizeof
will not evaluate to the size of the allocation, it will simply tell you how large the pointer is. Further, note that sizeof
is a compile time construct; its value is evaluated fully at compile time.
char *ptr = "1234567890";
char str[10] = "12345";
int numbers[10];
sizeof(ptr)
evaluates the size of the pointer. The value will typically be 4 (32-bit) or 8 (64-bit).sizeof(*ptr)
evaluates the size of char
. (1)sizeof(str)
evaluates the size of the array. (Its size is 10 bytes, even though only 6 were assigned.)sizeof(numbers)
will evaluate to sizeof(int) * 10
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With