By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,638 Members | 1,983 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,638 IT Pros & Developers. It's quick & easy.

distinguish between char* and char[x]

P: n/a
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!!


Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"mosfet" <tr******@wanadoo.fr> skrev i en meddelelse
news:bq**********@news-reader2.wanadoo.fr...
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
Jul 22 '05 #2

P: n/a

"mosfet" <tr******@wanadoo.fr> wrote in message
news:bq**********@news-reader2.wanadoo.fr...
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[i]) ? tab[i] : ' ');
}

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
Jul 22 '05 #3

P: n/a
mosfet wrote:
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])

Alternatively, you can use something like std::vector<char>, which knows
the number of elements it contains.
}

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.

Jul 22 '05 #4

P: n/a
Rolf Magnus wrote:
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.

Jul 22 '05 #5

P: n/a
"mosfet" <tr******@wanadoo.fr> wrote in message news:<bq**********@news-reader2.wanadoo.fr>...
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;
}
Jul 22 '05 #6

P: n/a
Mike Cox wrote:
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.

Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.