4

I want to calculate the length of an (at first) uninitialized char array.

My code is:

#include <stdio.h>

int main()
{
    char *string_t;
    int loc = sizeof(string_t)/sizeof(*string_t);
    printf("%d", loc);

}

I expect loc to be 0 but instead loc is 8. Can someone tell me why this is and how I can "fix" this to be the answer I expect?

4
  • 4
    could you explain why you expected 0 exactly? Commented Jan 27, 2016 at 3:07
  • @M.M I've seen on a couple forums (stackoverflow.com/questions/6001661/…) that sizeof(string_t)/sizeof(*string_t) is how you find the length of a char array Commented Jan 27, 2016 at 3:10
  • 1
    You don't have any char arrays in your code. Arrays are indicated by [ ] . Commented Jan 27, 2016 at 3:13
  • @M.M oh, I was thinking that since arrays decay into pointers, I basically have an array Commented Jan 27, 2016 at 3:23

5 Answers 5

3

sizeof is a compile time operator (with the exception of VLA). The value of sizeof(string_t)/sizeof(*string_t) is equivalent to sizeof(char *) / sizeof(char), which is the size of a pointer, usually 8 on 64-bit machines.

For uninitialized char * pointer, there is no correct size, you have to initialize it:

char *string_t = "";

and use strlen(string_t) to get 0. However, since it's a pointer to a string literal, it's somehow pointless though.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok that makes sense, however for a next step I want to add some characters to string_t and then check the size again, strlen causes a pointer error for me
That's why I said it's somehow pointless. You need an actual array char string_t[1024] = "", or use dynamic allocation.
2

you don't have a char array, you have a char *. To get what you want you need to something like :-

#include <stdio.h>

int main()
{
    char string[100];
    string[0]=0;
    int loc = strlen(string);
    printf("%d", loc);

}

2 Comments

what if I'm not sure how much space I need?
then you need to malloc and realloc. when you get memory of malloc then you do need a pointer so you can point at the memory it gives you. If you run out, realloc your memory to be bigger
2

char *string_t is a pointer, not an uninitialized char array. Its size is fixed to whatever is the size of a pointer on your system, which appears to be eight.

sizeof(*string_t) is the same as sizeof(char), so it is fixed at 1 by the C standard. There is no way to produce the behavior that you expect without changing from pointers to arrays.

If you would like to make an array that you resize by realloc-ing, you need to make a struct that keeps track of both the pointer and the size (or has two pointers pointing to the beginning and to the end of the allocated block).

struct dynamic_char_array {
    char *array;
    size_t size;
};

void add_char(struct dynamic_char_array* a, char c) {
    char *tmp = realloc(a->array, (a->size)+1);
    if (tmp) {
        a->array = tmp;
    } else {
        // Do something about a failed allocation
        ...
        return;
    }
    a->array[size++] = c;
}

Note: You could also use variable-length arrays, but once their size is set, it cannot be changed.

1 Comment

You write out of bounds if the allocation fails
1

To calculate the length of an uninitialized char array, you have to declare one first. In your code, you wrote char *string_t;. This statement declares a pointer to char, rather than an array of char.

An uninitialized char array should be declared using something like char s[1024];. To create an initialized one, use char s[1024] = "foo";, or char s[1024] = {'f', 'o', 'o'};.

So, actually, you are trying to calculate the length of an undeclared array, which is obviously impossible.

Comments

-1

'char *' is just pointer to char, not char array.
so you can't count array.

use like this

char array[100];
int len = sizeof(array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.