Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is char *str = "anything" always the size of 8?

I was testing around with sprintf a bit to convert an int into a string/char and noticed something:

  1. When I make a string like this

char *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?

  1. Similar to this, if I type

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;
}
like image 839
Merlin Avatar asked Oct 28 '14 22:10

Merlin


People also ask

Why is a char * a string?

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.

Is char * and int * the same size?

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.

What is the meaning of char * str?

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)

What is a char * in C?

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.


1 Answers

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.
like image 149
cdhowie Avatar answered Nov 05 '22 03:11

cdhowie