473,396 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

distinguish between char* and char[x]

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
6 2493

"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

"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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: André Rosendaal | last post by:
Hi, I need a test so I can determine whether a url points to a text file or to a binary file. Actually, I need to distinguish between streaming files and metafiles (e.g. asx files). I tried...
5
by: John Bokma | last post by:
I want to use file_get_contents, which according to the documentation returns FALSE if it fails. However, it also fails if I read a file of 0 bytes in size. So I tried to use filesize to check...
1
by: Simon Willison | last post by:
Hi all, I've been experimenting with Python CGIs, and more recently mod_python. The FieldStorage class used by Python's cgi module and mod_python's utility library allows form data sent by a...
3
by: gogomei | last post by:
I have a text file like following and need to read out the names of each person. Since the only accurate info is there is a null line before starting a new name, I have written following code to...
6
by: Sayan | last post by:
How do I distinguish between a heap pointer and a stack pointer? I want the following to work: template<class T> bool isHeapPtr(T* p1,T* p2);//the function I want to write //... int a = 5;...
7
by: Andre | last post by:
Hi, I have a program that sends some output to stdout and some to stderr. I need to separate the two using the command-line so that I direct stderr output to a file, say fileA.txt, and stdout...
5
by: Jon Maz | last post by:
Hi there, I am experimenting with the FileSystemWatcher object. I have set the NotifyFilter as follows: myFileSystemWatcher.NotifyFilter = NotifyFilters.Security | NotifyFilters.CreationTime...
15
by: baibaichen | last post by:
hi, consider the follow code: #ifdef USE_UNSIGNED_INT typedef unsigned int size; #else typedef unsigned int size; #endif
4
by: amit | last post by:
Hi guys!I am trying to write a program which will segregate some selected keywords from a given file.The source code is given alongwith #include<stdio.h> #include<string.h> char...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.