how void* iterate for next element
Code:#include<iostream> using namespace std; void check(void *t){ while(*(int*)t){ cout << *(int*)t << endl; // *(t++); } } int main(){ int a[] = {5,4,7,8,0}; check(a); }
how void* iterate for next element
Code:#include<iostream> using namespace std; void check(void *t){ while(*(int*)t){ cout << *(int*)t << endl; // *(t++); } } int main(){ int a[] = {5,4,7,8,0}; check(a); }
Cast the pointer type to an int.
Code:*((int*)t)++;
Since you can make a cast for t and use it, have you thought about, say
int *u = reinterpret_cast<int*>(t);
u++;
That should iterate fine. Or, you know, stop using void* at all.
Thanks for reply, my prob is short out
but how can i find at run time which data type send to void*, above code work fine when sending data is known otherwise it give error result.Code:int *u = reinterpret_cast<int*>(t); u++;
Last edited by Salem; 05-13-2011 at 06:06 AM. Reason: Remove unreadable yellow...
You can't. C/C++ basic datatypes are stored in memory as "contents only"; they do not contain any meta-information about themselves and therefore there is no way for the compiler to tell what datatype this content belongs to.
For example, a regular int is (usually) 4 bytes, and a short 2 bytes. They are not null-terminated, meaning unless the compiler is told in advance whether a particular variable is a short int or a regular int, it cannot tell how many bytes to read from memory. This is why you must correctly cast the type of void -- if you do not cast, you will get a compiler error, and if you cast wrongly, you will cause a problem. If you want it to read two bytes and treat that as a number, use short. If you want 4 bytes, use int.
Some languages do maintain meta-information about all variables*, and allow for typechecking of everything, but this is a performance hit and not part of C/C++. You could write such a system (and there may be some available in optional libraries), but most likely, you should not need to do so if you think about what you are doing in a way more consistent with conventional C++ programming.
* AFAIK these are all interpreted languages, which use a common runtime interpreter to do this. The overhead for doing so is substantial.
Last edited by MK27; 05-13-2011 at 06:06 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
MK27 is right. However, I think what you really want is something like this:
Now you can call check() for any type of array, as long as the very last item equals 0. You could even call it for your own datatypes as long as the proper functions are overloaded.Code:template<typename T> void check(T *t){ while(*t){ cout << *t << endl; t++; } }
Note that this might seem counterintuitive, as it doesn't seem to match what MK27 said. But it does; the type will be determined at compile time, not at runtime.
Note that actually C++ does contain runtime type information for classes, but you should barely ever have to use it.
O_o
The path you are on... leads to getting conked with the ban hammer.
Soma
How can determine length of array integer * pm in main when check (length 5) return int**
Code:#include<iostream> using namespace std; int** check(){ int a=10,b=2,c=3,d=4,e=5; int *p[5] = {&a, &b, &c, &d, &e}; return p; } int main(){ int **pm = check(); cout << **pm << endl; }
Well none of that would work anyway, since you're returning a pointer to a local variable (with pointers to yet more local variables).
All these pointers are INVALID when the function exits.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.