distinguish between char* and char[x]

M

mosfet

Hi,

how can i make the difference between a char* and a char[x]
because I need to do this

void myfun(char * tab)
{
int nTmp;

nTmp = sizeof (tab);
}

So if I do this it's ok :

char tab[10];
myfunc(tab); // OK because sizeof(tab) = 10

char *tab;
tab = new char [10];
myfunc(tab); // KO because sizeof(tab) = 4 pointer size!!
 
P

Peter Koch Larsen

mosfet said:
Hi,

how can i make the difference between a char* and a char[x]
because I need to do this

void myfun(char * tab)
{
int nTmp;

nTmp = sizeof (tab);
}

So if I do this it's ok :

char tab[10];
myfunc(tab); // OK because sizeof(tab) = 10

char *tab;
tab = new char [10];
myfunc(tab); // KO because sizeof(tab) = 4 pointer size!!

You can't. sizeof(tab) will be sizeof(char *) no matter what you do. My
recommendation is to stay away from this lowlevel stuff and use a
std::vector instead.

Kind regards
Peter Koch Larsen
 
M

Mike Wahler

mosfet said:
Hi,

how can i make the difference between a char* and a char[x]
because I need to do this

void myfun(char * tab)
{
int nTmp;

nTmp = sizeof (tab);
}

So if I do this it's ok :

char tab[10];
myfunc(tab); // OK because sizeof(tab) = 10

char *tab;
tab = new char [10];
myfunc(tab); // KO because sizeof(tab) = 4 pointer size!!

When passed to a function, an array 'decays' to a pointer
to its first element.

If the called function needs the array's size (which is
almost always), you need to pass this information as another
argument.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

void myfun(char *tab, size_t elements)
{
size_t i = 0;

for(i = 0; i < elements; ++i)
printf("tab[%lu] == %c\n",
(unsigned long)i, isprint(tab) ? tab : ' ');
}

int main()
{
char array[] = "Hello world";

printf("array has %lu elements of %lu bytes each.\n",
(unsigned long)(sizeof array / sizeof *array),
(unsigned long)(sizeof *array));

printf("array occupies a total of %lu bytes\n",
(unsigned long)sizeof array);

myfun(array, sizeof array / sizeof *array);

return 0;
}


-Mike
 
R

Rolf Magnus

mosfet said:
Hi,

how can i make the difference between a char* and a char[x]
because I need to do this

void myfun(char * tab)
{
int nTmp;

nTmp = sizeof (tab);

tab is a pointer to char, so sizeof(tab) returns the size of a pointer
to char.
If tab points to the first element of an array and you need the size of
the array in the function, you have to provide that information to that
function yourself, e.g. by adding an extra parameter for the size or by
adding a special value as last array element that marks the end. Or you
could make the size fixed and provide a reference to an array, like:

void myfun(char (&tab)[10])

which knows said:
}

So if I do this it's ok :

char tab[10];
myfunc(tab); // OK because sizeof(tab) = 10

Right. tab is of type "array of 10 chars", so sizeof(tab) returns the
size of an array of 10 chars.
char *tab;
tab = new char [10];
myfunc(tab); // KO because sizeof(tab) = 4 pointer size!!

Right. Same as in the function.
 
R

Rolf Magnus

Rolf said:
So if I do this it's ok :

char tab[10];
myfunc(tab); // OK because sizeof(tab) = 10

Right. tab is of type "array of 10 chars", so sizeof(tab) returns the
size of an array of 10 chars.

Sorry, I overlooked that you call the function here, and not sizeof(tab)
directly. Your function is taking a pointer to char (you defined it
that way), and it will always only get such a pointer, nothing else. In
the case of calling the function with an array as parameter, as you're
doing in the above example, the array decays into a pointer. That means
that not the array is passed (which is not possible, since arrays are
not copyable), but instead a pointer to its first element. The
function, and thus the sizeof in it always just sees a pointer to char.
Also note that sizeof doesn't do any dynamic typing.
 
M

Mike Cox

mosfet said:
Hi,

how can i make the difference between a char* and a char[x]
because I need to do this

void myfun(char * tab)
{
int nTmp;

nTmp = sizeof (tab);
}

So if I do this it's ok :

char tab[10];
myfunc(tab); // OK because sizeof(tab) = 10

char *tab;
tab = new char [10];
myfunc(tab); // KO because sizeof(tab) = 4 pointer size!!


You should do this:
void myfun(char * tab)
{
int nTmp = strlen(tab)+1;
}
 
R

Rolf Magnus

Mike said:
how can i make the difference between a char* and a char[x]
because I need to do this

void myfun(char * tab)
{
int nTmp;

nTmp = sizeof (tab);
}

So if I do this it's ok :

char tab[10];
myfunc(tab); // OK because sizeof(tab) = 10

char *tab;
tab = new char [10];
myfunc(tab); // KO because sizeof(tab) = 4 pointer size!!


You should do this:
void myfun(char * tab)
{
int nTmp = strlen(tab)+1;
}

No, he shouldn't. strlen() requires its argument to be a pointer to a
valid C style string (i.e. terminated by a '\0' character), but the
array is uninitialized.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top