conditional statement

Mar 8, 2011 at 10:24am
Can I use the conditional operator in such a way that,if the conditions meets,it will assign the value,and if not it will do nothing.
What I mean is this possible?

apointer=(afunc())?afunc():donothing;

Here, afunc() returns a pointer with the same type as apointer.This function returns NULL value if an error occurs so apointer will be assigned a NULL in this case.How can I avoid that?

Also,this expression
apointer=(afunc())?afunc():NULL;
causes
afunc()
to be called twice,can we make it be called once without using an extra dummy pointer variable?

Mar 8, 2011 at 12:35pm
I've never used a conditional statement before, but are you actually missing the condition?

i.e.
x = y > 3 ? 10 : 20

  If y is greater than 3  
    x = 10
  else
    x = 20


You don't actually have a condition there.
The function is being called twice because it gets called for the condition and then the assignment.

x = y > afunc() ? afunc() : 20

 call afunc() for condition  
    x = call afunc() for assignment  
  else
    x = 20
Mar 8, 2011 at 12:47pm
Thanks Lynx.

I thought that (afunc()) expression below can be condition.I mean,the function will return
something
or
NULL
,so I thought NULL can signal false,and the condition will be true otherwise.

apointer=(afunc())?afunc():donothing;

Doesn't it mean
apointer=(afunc()!=NULL)?afunc():donothing;
?
Mar 8, 2011 at 1:00pm
Here, afunc() returns a pointer with the same type as apointer.This function returns NULL value if an error occurs so apointer will be assigned a NULL in this case.How can I avoid that?
Use a simple if instead of the conditional operator
Also,this expression
apointer=(afunc())?afunc():NULL;
causes

afunc()

to be called twice,can we make it be called once without using an extra dummy pointer variable?
No
Mar 8, 2011 at 1:01pm
I see what you mean now, sorry.

I think you may be better off doing the following. But as I mentioned before, I've never used a conditional statement.

1
2
3
4
if( afunc() )
{
    aPointer = afunc();
}


wow, actually, that's wrong too, it still gets called twice... I'm a bit dumb this morning... ahaha

My last edit now! This should work.

1
2
3
4
5
6
( return type ) tempVariable = afunc();

if( tempVariable )
{
    aPointer = tempVariable;
}
Last edited on Mar 8, 2011 at 1:15pm
Mar 8, 2011 at 1:30pm
Thanks Bazzy and Lynx.
So, without using an extra dummy variable/object it is not possible.
Mar 8, 2011 at 1:39pm
Is the reason for trying to do this without a dummy/object variable, to save on memory?

If so, you could create a new pointer and then delete it after the assignment to the pointer you will be using afterwards.

http://en.wikipedia.org/wiki/New_(C%2B%2B)
Mar 8, 2011 at 1:50pm
That would consume more memory, a pointer on the stack and a pointer on the heap
( instead of only a pointer on the stack )
Mar 8, 2011 at 3:21pm
But wouldn't it be more efficient to create a pointer on the heap if it was being created in the main function? Assuming that it is being made in the main function.

That way, the pointer would only be on the heap until the assignment was made, then it would be removed from the heap. Rather than creating a pointer on the stack until the program had been exited.

new pointer = afunc();

if( new pointer )
    aPointer = new pointer

delete new pointer
Mar 8, 2011 at 3:26pm
When I say:

foo *ptr = new foo;

ptr is not being stored on the heap, only the object it points to. If it goes out of scope, ptr is destroyed because it's an automatic variable, stack allocated, but the object it points to isn't.
Last edited on Mar 8, 2011 at 3:27pm
Mar 8, 2011 at 3:30pm
You could also do

 
apointer = (apointer=afunc()) ? apointer : donothing;


The power of the assignment operator :)
Mar 8, 2011 at 3:34pm
@Lynx876
What you are saying:
1
2
3
4
5
type** new_ptr = new type*;
*new_ptr = afunc();
if ( *new_ptr )
    apointer = *new_ptr;
delete new_ptr;
vs stack allocation of the pointer:
1
2
3
type* ptr = afunc();
if ( ptr )
    apointer = ptr;
Mar 8, 2011 at 4:42pm
1
2
3
4
5
6
7
8
9
You could also do

  	

apointer = (apointer=afunc()) ? apointer : donothing;



The power of the assignment operator :) 



Thanks sachav,that was the power I was looking for :)
although that donothing is still symbolic.

Btw,I think I should use a new dummy pointer to keep the result of the function,in an if statement,as Lynx gave above,to prevent apointer from taking NULL value,because that NULL value creates problem when I display all the pointed objects in the array.
Last edited on Mar 8, 2011 at 4:42pm
Mar 8, 2011 at 4:49pm
apointer = (apointer=afunc()) ? apointer : donothing; Is NOT valid C , you can't assign the same variable twice in an expression.
Mar 8, 2011 at 5:50pm
Wait, what's not valid C?
The said expression is just the diminutive form of
1
2
3
4
if(apointer = afunc())
    apointer = apointer;
else
    donothing;

Right?

Is it the second line that isn't valid? If that's so just use apointer = apointer * 1;
Last edited on Mar 8, 2011 at 5:53pm
Mar 8, 2011 at 5:55pm
Nope, it's a single expression. As I said, in Cplusplus you can't modify the same variable twice.

( ATM there's some issue with the plus sign on the forum )
Mar 8, 2011 at 8:10pm
Its nice to read your discussions,but I passed that example using an extra dummy variable.
The reason I asked such a question is that,the author of the book is so stingy about memory usage.Otherwise, since Im not coding a program with thousands of hundreds of lines and variables, an extra variable is not problem :)
Mar 8, 2011 at 9:37pm
In modern computers, memory is the last of your problems. How old is your book?
Mar 8, 2011 at 9:44pm
What Bazzy said. Unless you need to do that with a bazillion recursions you don't really have to care about the memory you need for one extra variable.
Topic archived. No new replies allowed.