"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