Using pointers to access variables in structures

This is a discussion on Using pointers to access variables in structures within the C++ Programming forums, part of the General Programming Boards category; Hey guys. I have this project where I need to use a structure without knowing what its name is nor ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    903

    Using pointers to access variables in structures

    Hey guys. I have this project where I need to use a structure without knowing what its name is nor how its members are named. I decided to force the user to add a static const char* Format variable which holds information on the type of the members only (i = int, d = double..) and this way I can somewhat get around this problem. However, it seems that some of my calculations are wrong even though when I tried it with a dummy class and a few pointers in the main() function it worked, now that I integrated all of this into my class, there's a problem. I stepped through with the debugger and all the values are correct; however, the debugger also revealed that only the first variable is modified (i.e. the pointer is not shifted properly to the next variable). How is this possible according to the following code ? I have checked back my algorithm and it seems correct to me.

    Code:
    	template <typename ArgT>
    	void ProcessVar(void* ptr, ArgT* var, std::string tmp)
    	{
    		StrToT(var, tmp);
    		*((ArgT*)ptr) = *var; // This sets the variable pointed to to *var
    		ptr = ((ArgT*)ptr + 1); // This is the line shifting the pointer
    	}
    Thanks.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    I have this project where I need to use a structure without knowing what its name is nor how its members are named. I decided to force the user to add a static const char* Format variable which holds information on the type of the members only (i = int, d = double..) and this way I can somewhat get around this problem.
    Your idea seems to work, but that's what I thought <typeinfo> was there for. You might want to use that instead. As for your problem, have you tried something like:

    ((ArgT*) ptr)++;

    That should shift the void * properly.
    Last edited by whiteflags; 07-26-2006 at 10:52 PM.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Using the post-increment operator makes the compiler complain about the expression not being an l-value and using the typeid operator isn't of much use, if it works as I understand it, because I do not even know what type of variable will be the next variable, thus I cannot cast a pointer to this type and typeid() cannot retrieve any useful information. I don't know if you know what I mean but I'm fairly sure typeid() can't do the job here. Thanks for the suggestions though.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    > Using the post-increment operator makes the compiler complain about the expression not being an l-value

    Yes, but the compiler is usually wrong about that. I'm sure this applies here:

    Quote Originally Posted by ANSI Standard 5.2.9/2
    An expression e can be explicitly converted to a type T using a
    static_cast of the form static_cast<T>(e) if the declaration T t(e);"
    is well-formed, for some invented temporary variable t.
    The effect of such an explicit conversion is the same as performing
    the declaration and initialization and then using the temporary vari-
    able as the result of the conversion. The result is an lvalue if T is
    a reference type, and an rvalue otherwise. The expression
    e is used as an lvalue if and only if the initialization uses it as an
    lvalue.
    Anyway, besides getting that complaint it still didn't work? If that is the case then I'm not really sure either, even though you say that function works, aside from the pointer arithematic being a little messed up.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I have this project where I need to use a structure without knowing what its name is nor how its members are named.
    That severely limits what you can do with it. Can you be more specific about what this project entails?
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > void ProcessVar(void* ptr, ArgT* var, std::string tmp)
    You probably need to pass ptr as a reference. Otherwise the change to ptr won't be seen in the calling function.
    Code:
    	void ProcessVar(void* &ptr, ArgT* var, std::string tmp)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures and setting variables
    By teck in forum Windows Programming
    Replies: 17
    Last Post: 11-30-2007, 04:38 PM
  2. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 06:14 PM
  3. returning an array of pointers to structures
    By dharh in forum C Programming
    Replies: 9
    Last Post: 02-06-2003, 02:26 PM
  4. pointers as function variables
    By PHP in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2003, 05:45 AM
  5. Freeing pointers in structures
    By jim50498 in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 11:53 AM

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