Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using sizeof for char *'s

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello everyone,

I am trying to do something of this sort:

Code:
int foo( char * string )
{
    char astring[ sizeof(string) ];

   ....

}
meaning I want astring to be exactly the size that string is. But the sizeof operator always returns 3 bytes. Is it returning the size of the pointer instead of the memory it points to? Any suggestions on how to make this work?

Thanks for u'r help,
SS
 
int foo(char* string)
{
char* astring = malloc(strlen(string)+1);

...
}

I believe this should work.

Matt

 
int foo(char* string )
{
char* astring=new char[strlen(string+1)];
....
}
by the way, don't use string as variable name. It may cause conflicts. John Fill
1c.bmp


ivfmd@mail.md
 
Actually, I was also thinking of that. But I was wondering if there's any way to use sizeof to do that. Is sizeof in this case returning the size of the pointer variable?

Thanks for u'r replies,
SS
 
yes. It'll never return the size of allocated memory pointed by the variable pointer. But in C++ you can overload sizeof operator such as to return the memory allocated by your own class associated with a pointer. John Fill
1c.bmp


ivfmd@mail.md
 
just a side note...

char* astring=new char[strlen(string+1)];

will do pointer arithmetic first... just move the +1 outside of the ().

Matt
 
In C, there's no way to obtain the size of the allocated memory when the array or pointer is passed to a function using sizeof. sizeof does, indeed, compute the size of the pointer (since this is the type that it's operating on). You can either do as suggested above and compute the size using strlen() using a pointer to obtain a block of dynamic memory:

int foo(char *string)
{
char *astring=malloc(strlen(string)+1);
/* ... */

Or, you can add an extra parameter to your function that holds the size and use dynamic memory:

int foo(char *string,size_t len)
{
char *astring=malloc(len+1);
/* ... */

In C99 (the new C standard), variable length arrays have been added, so using the prototype above you could declare an array of size 'len' + 1:

int foo(char *string,size_t len)
{
char astring[len+1];
/* ... */

This standard has only been implemented by a few compiler vendors, so there's a good chance that your compiler will reject it (not being C99-compliant) and it's probably a little too early to consider it a portable construct.

Also, using the variable name 'string' in this context is fine. If 'string' had external linkage it would not, because identifier names that begin with "str" are reserved by the implementation. But since 'string', being an auto variable, is not exported to the linker there's no problem.
Russ
bobbitts@hotmail.com
 
If you are using an array like char str[10] then you can use the sizeof(str) to find the total memory allocated for that str array and from this you can find the number of elements by giving sizeof(str) / sizeof(str[0]).

If you are using an pointer then sizeof operator will retun the sizeof the pointer variable only.

Maniraja S
 
To add to that:

Whether the string was originally stored in an array of char or a pointer, once it's passed to a function, it decays to a pointer to its first element. So, these two calls to foo() in main() are identical once control enters foo():

void foo(char *string)
{
/* foo() doesn't know if an array
* or pointer was passed. It gets
* a pointer regardless.
* sizeof string==sizeof(char *)
*/
}

int main(void)
{
char array[]="hello";
char *ptr="hello";

foo(array); /* pass an array of char */
foo(ptr); /* pass a pointer to char */

return 0;
}

You can also declare foo() like this, but it makes no difference, foo() still gets a pointer:

void foo(char string[])
{
/* sizeof string *still* equals sizeof(char *)
* because string /is/ a pointer despite the
* array syntax used in the declaration
*/
}

The only way you really can get the size of an array that's passed to a function using sizeof is to have the function accept a pointer to the array itself. This isn't a solution though because the size of the array must be set in the parameter for sizeof to operate on it:

void foo(char (*array_ptr)[10])
{
/* sizeof *array_ptr will report 10, but
* we already knew that
*/
}

void foo(char (*array_ptr)[])
{
/* can't use sizeof on *array_ptr, it's
* an incomplete type
*/
}
Russ
bobbitts@hotmail.com
 
In C++ you can use vectors, maps, autopointers and... The lybary's name is STL. John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top