PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Stupid C question.

Hi, I know it may sound stupid but I don't know how to make a variable point to another one of the same type in C. It's pretty easy with char * and pointer type variables, but how to do it with simple variables?

ex:

Code:
double d;
double d2;

d = &d2; // This doesn't work but I want d to point to d2.
d = 10; // This should set d2 too.


Any help would be appreciated.

Regards,
Kornalius
Neither d nor d2 or pointers. They store actual doubles, not pointers to doubles. Change d to a pointer, and then dereference it ( *d ) to access the variable it points to.

Code:

double *d;
double d2;

d = &d2; // I want d to point to d2.
*d = 10; // This sets d2 (because d points to d2)


In your example, you are setting d to the numerical value of d2's address, then when you say d=10 you are setting d to 10.

In my example if you did d=10 (note no deference operator *) then you are setting d to point to the memory address 10, which would not be the desired behavior (plus you would get a compiler error).

Dan East
Is it the only way, because my variable (double d), cannot be a (double *) at this point. :( I have already gone too far in my project to change the design. I absolutely have to use a pointer?

Regards,
Kornalius
Code:

double d;
double d2;


There is no other way, since with this declaration 2 different places on stack will be allocated. With other words, the lvalue of d will never be be lvalue of d2 (which are of type const double* ).
ok thanks.

Regards,
Kornalius
Tala wrote:
Code:

double d;
double d2;


There is no other way, since with this declaration 2 different places on stack will be allocated. With other words, the lvalue of d will never be be lvalue of d2 (which are of type const double* ).



Is that true? What about using C++ references. Couldn't you have:

double d;
double& d2=&d;

Though be aware that references must be constant pointers.
It would be:

DOUBLE d;
DOUBLE &d2;=d; //not =&d;

I guess that would work, but it is a terrible programming practice. I can just imagine the convoluted mess that would result, since it would not be clear which vars are "real" and which are references. The advantage of a pointer is that it really is a different type.

This is on a completely different note, but I feel it is bad practice to declare like this:
DOUBLE& d2;
instead of this:
DOUBLE &d2;

We discussed this a couple years ago. The former gives the appearance that you are using the type DOUBLE&, when the ampersand is really a modifier for a single variable.

Here's where that syntax is problematic:
DOUBLE& d1, d2;

That gives the appearance that both d1 and d2 are "DOUBLE &", whereas only the d1 is a reference, not d2.

DOUBLE &d1;, d2;

That makes it perfectly clear what is being declared.

Dan East
I agree with you Dan - it took me a couple of times running into the problem you describe before I changed, but the part that annoys me is when declaring functions if the return type is a pointer - I swap back eg.:

double& AFunction()
Dan East wrote:
This is on a completely different note, but I feel it is bad practice to declare like this:
DOUBLE& d2;
instead of this:
DOUBLE &d2;


I totally disagree with you Dan. The reference modifier "&" applies to the type (in this case DOUBLE) and not the variable. It is part of the type declaration and not the variable's name. It modifies the type of the variable, not the variable itself.

Dan East wrote:
Here's where that syntax is problematic:
DOUBLE& d1, d2;


Agreed. But then I would argue that you shouldn't declare more then one variable on the same line.

Dan East wrote:
No confusion possible.
DOUBLE &d1;, d2;
That makes it perfectly clear what is being declared.


The following is clearer, and more in line with the spirit of C++ where you shouldn't declare your variables before you use them.

Code:

double& d1;
double d2;
My opinion:

Code:

AType t1;
AType *t2;


is so clear because I can see straight away which is a pointer and which isn't. Generally, I tab all of the variables across as above and it would involve an extra eye movement to scan left and see if it was a pointer or not where generally you know the type by variable name but often don't remember which ones were pointers. But I guess everyone has their own preference.
Okay now, if you want to get proper, then you shouldn't do what you indicated either. All types with modifiers should be made into a completely new type with typedef.

typedef DWORD & DWORD_REF;
DWORD d2;
DWORD_REF d1=d2;

Speaking of disagreeing, I need to run something by you that we might can collaborate on.

Dan East
Dan East wrote:Speaking of disagreeing, I need to run something by you that we might can collaborate on.


Nope, sorry. This is too verbose and gets dangerously close with what I call mental masturbation.

BTW, "typedef" doesn't define a new type (isn't that sad). All it does is define an alias (i.e. gives a new name).

In this example, there is no difference between DWORD& and DWORD_REF. What I am saying is that DWORD_REF is not a new type.

To give you a concrete example using pointers:

typedef DWORD* DWORD_PTR;

DWORD_PTR a = &someVar;
DWORD* b = a;

This is perfectly legal. And this is because DWORD_PTR is not a new type, but simply an alias for DWORD*. I know I know, I am getting pedantic =)


When you have something like "DWORD *a" or "DWORD* a", it is important to understand that the type of "a" is "DWORD*", which I feel isn't clear with your notation.

We both agree that the way & and * are applied when declaring (multiple) variables is problematic. We both resolve it in a different way, and life is good.

I could go on and on about how C++ isn't enough typesafe.
Thierry wrote:

Nope, sorry. This is too verbose and gets dangerously close with what I call mental masturbation.



haha, I remember the day when I found the very strange "mutazt" type in a source code I was reading.

Finally it was just a char*, his definition was : mutable ascii zero terminal.

That was pure mental masturbation and unreadable code.

I think it's dangerous for maintenability to create too much custom var types, especially when the names don't speak by themselves.