• Advertisement
Sign in to follow this  

sizeof string passed in as a parameter

This topic is 4706 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a C function that I am passing a pointer to a string which is of size char string[255]; Inside the function this is passed to I want to get the value 255 however no differnet combination of sizeofs seem to be able to get a value other than 8 or 1. What am I doing wrong? Thanks

Share this post


Link to post
Share on other sites
Advertisement
You're doing nothing wrong. In C if you write this:

char buff[255];
std::cout << sizeof(buff);
you have 255

But if you do:

char buff[255];
void foo(char* b)
{
std::cout << sizeof(b);
}

foo(buff);

the compiler consider the new type (char*) and tell the right result. This is because of the type convertion. In order to make it function you can do:

//C style
void foo(char* b, unsigned int len)
{
std::cout << len;
}

foo(buff, sizeof(buff));

or:

//C++ style
void foo(std::string* str)
{
std::cout << str->length();
}


Share this post


Link to post
Share on other sites
Quote:
Original post by Real World
Inside the function this is passed to I want to get the value 255 however no differnet combination of sizeofs seem to be able to get a value other than 8 or 1. What am I doing wrong?


The problem you have is known as "pointer degradation", arrays are not just pointers, they do carry more info like the size, the compiler knows.

With pointers the compiler can never know as it could be a pointer to single instance or the first element of an array, pointers don't carry any info like that so as soon as you assign the first element of an array to a pointer your degrading to pointers.

Anycase if you have C-style string i.e. null-terminated string and you want the length then use the standard library function "strlen"

Share this post


Link to post
Share on other sites
Quote:
Original post by Ilankt
sizeof(buff) would return 255?


Yes, luckly for him that was all that is needed, general forumla for different types:


foo f[M];

const std::size_t number_of_elems = sizeof(f) / sizeof(foo);


Note the compiler knows the size of an array at compile-time.

Quote:
Original post by Ilankt
wont it suppose to return the size of a pointer(4)?


No, arrays are not pointers, read my comment about "pointer degradation".

Share this post


Link to post
Share on other sites
If you're lucky enough to have a C99 compliant compiler then you should be able to use variable length arrays. Although I still haven't found a compiler that supports them myself, so I'm not even sure about the syntax.
Also note that sizeof(char) is guaranteed to be equal to one, thus the sizeof of a char arrays is always equal to the number of characters in it (plus null).
void foo(char bar
  • ) {
    printf("strlen: %d\n", sizeof(bar) - 1);
    }

    Share this post


    Link to post
    Share on other sites
    Quote:
    Original post by doynax
    If you're lucky enough to have a C99 compliant compiler then you should be able to use variable length arrays. Although I still haven't found a compiler that supports them myself, so I'm not even sure about the syntax.


    GCC has some support for C99 and variable length arrays, as language extensions for C++ & C98 but doesn't support all details descibed in C99 you can do all kinds of stuff with C99 arrays. No doubt Comeau compiler supports it fully.

    Share this post


    Link to post
    Share on other sites
    Sign in to follow this  

    • Advertisement