Like Tree2Likes
  • 1 Post By whiteflags
  • 1 Post By MK27

how void* iterate for next element

This is a discussion on how void* iterate for next element within the C++ Programming forums, part of the General Programming Boards category; how void* iterate for next element Code: #include<iostream> using namespace std; void check(void *t){ while(*(int*)t){ cout << *(int*)t << endl; ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    6

    how void* iterate for next element

    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);
           
    }

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Cast the pointer type to an int.

    Code:
    *((int*)t)++;

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    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.
    Salem likes this.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    6
    Thanks for reply, my prob is short out
    Code:
    int *u = reinterpret_cast<int*>(t);
    u++;
    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.
    Last edited by Salem; 05-13-2011 at 07:06 AM. Reason: Remove unreadable yellow...

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by chaman View Post
    but how can i find at run time which data type send to void*
    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 07:06 AM.
    rags_to_riches likes this.
    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

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    MK27 is right. However, I think what you really want is something like this:
    Code:
    template<typename T>
    void check(T *t){
    
      while(*t){
        cout << *t << endl;
        t++;
      }
    
    }
    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.

    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.

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    19
    Quote Originally Posted by EVOEx View Post
    MK27 is right. However, I think what you really want is something like this:
    Code:
    template<typename T>
    void check(T *t){
    
      while(*t){
        cout << *t << endl;
        t++;
      }
    
    }
    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.

    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.
    yes , the template programming just can solve the question !

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,370
    O_o

    The path you are on... leads to getting conked with the ban hammer.

    Soma

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    6
    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;
    }

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    Undefined behavior at its finest!
    Furthermore, there is no (standard) way to determine the size of an array outside the function it was defined in.
    You should use std::array or std::vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need to iterate through an std::map...
    By Programmer_P in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2011, 12:46 PM
  2. vector remove element of element
    By Ducky in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2010, 04:24 PM
  3. Replies: 12
    Last Post: 03-27-2009, 03:36 PM
  4. ::iterate
    By Coding in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2008, 06:46 AM
  5. iterate a 3d vector
    By Coding in forum C++ Programming
    Replies: 12
    Last Post: 07-31-2008, 06:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21