I know it can in C++ returning a reference variable. Thanks
I know it can in C++ returning a reference variable. Thanks
Is there a reason you haven't just tried?
Code:itsme@dreams:~/C$ cat funclval.c #include <stdio.h> int *func(int *num) { return num; } int main(void) { int a; *func(&a) = 5; printf("%d\n", a); return 0; } itsme@dreams:~/C$ gcc -Wall funclval.c -o funclval itsme@dreams:~/C$ ./funclval 5 itsme@dreams:~/C$
Last edited by itsme86; 03-24-2005 at 07:09 PM.
If you understand what you're doing, you're not learning anything.
if "func" in your example returns an address of an integer, a constant value, say it's a 32 bit address 0x0030FFEE, then why does the dereferencing operator * work with that returned value? I thought it was only legal to dereference a pointer variable.
For example, you couldn't explicitly assign variable "a" like this:
*0x0030FFEE = 5;
or like this:
Code:#include <stdio.h> int main(void) { int a_address, a = 5; a_address = (int) &a; *a_address = 10; /* invalid indirection */ printf("value of a = %d\n", a); /* attempt to display a with 10 */ return 0; }
Of course it's an invalid indirection. You don't dereference anything that isn't a pointer, so why should that work?
Quzah.
Hope is the first step on the road to disappointment.
[edit]But [wrt topic title]...Code:#include <stdio.h> int main(void) { int *a_address, a = 5; a_address = &a; *a_address = 10; printf("value of *a_address = %d\n", *a_address); return 0; }
So no.An lvalue is an expression with an object type or an incomplete type other than void
Last edited by Dave_Sinkula; 03-24-2005 at 10:37 PM.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
consider this definition:
Code:int *func(int *num) { return num; }
what does func return?
1.) a constant address
or
2.) a variable storing a constant address
consider this definition:
what does func2 return?Code:int func2(int num) { return num; }
1.) a constant integer
or
2.) a variable storing a constant integer
the call func2(5) returns the integer constant 5, not a variable holding integer 5. Therefore, func(5) cannot be used on the left side of an assignment, say func(5) = 10, which would evaluate to 5 = 10, which is incorrect syntax.
It's to my understanding that the call func(&a) in the first example will return the constant addresss of a, not a variable holding the constant address of a, so you can't use it in left side assignment, nor dereference a constant object. I don't know if I can elaborate this any further.
I was stranded on a tropical island for over 2 years, so I am not up to par with this stuff. Perhaps a precise definition of the dereferencing operator may help, but I have yet to see any C text to give one. Thanks fellas.
Pointers store addresses. You're returning a pointer, therefore, you're returning an address. Which is, in reality, just another value.Originally Posted by Dave Jay
It returns a value, just like every other function you write.Originally Posted by Dave Jay
The reason you can't use functions as an lvalue is because an lvalue is something you assign to. You cannot assign a value to the returned value of a function. Functions always return values. Every variable of any kind stores some kind of a value.
Now, you can use a function to return a pointer, dereference that pointer to get a variable, and assign something to that, but you still can't just assign directly to a function as an lvalue.
But again, you're not assigning to a function, so no, functions cannot be lvalues. Just like the number 20 cannot be an lvalue.Code:int *foo( ) { ... } *foo( ) = 20;
Quzah.
Hope is the first step on the road to disappointment.
Something similar happened to Arnold in Twins and he turned out to be a genius!I was stranded on a tropical island for over 2 years, so I am not up to par with this stuff.
If you understand what you're doing, you're not learning anything.
this is what has been confusing me but I have, I think, figured out my problem. The semantics of words have confused me a bit too. PLease feel free to correct me if I am wrong.
First, the actual word "pointer" can refer or mean:
a) an address (ex. the function takes three pointers as arguments) You pass an argument in C via value, not variable, and the value in this case is a constant address, or pointer.
b) a variable that stores an address of a specific type
(ex. declared a pointer of type char) Here the word pointer is referred to as the variable that can hold a constant address, or pointer.
The two have subtle, yet important distinctions.
In a similar manner, the word "integer" can refer the literal
mathematical constant (ex. 5) or an actual variable that stores
such a constant. The word is interchangably used without any thought.
In this example above, foo returns a pointer of type int, or an address of type int (definition a above), not an actual variable (definition b).Code:int *foo( ) { ... } *foo( ) = 20;
Let's say for example foo returns the 16 bit address 0x0FFE.
It would not return a pointer variable containing 0x0FFE but simply the constant 0x0FFE, which can be said a pointer.
so the statement
*foo( ) = 20;
is essentially eqivalent to
*0x0FFE = 20;
in this example. However, explictly typing this the 2nd way is not allowed (at least my compiler doesn't allow it). This confused me because the deferencing operator only allows pointer variables as their operands, not pointer constants, yet the first way is allowed and the second way isn't. So, the way I see it, it's just syntax restriction in the C language.
You can dereference expressions which yield a pointer type (except for void pointers). You can not dereference an integer expression.
The expression 0x0FFE yields an integer type and can not be dereferenced. You can use a cast to create an expression that yields a pointer type:
So, in conclusion: An expression that yields a non-void pointer type may be dereferenced (assuming it points somewhere valid).Code:*((int *) 0x0FFE) = 20;
You can still dereference it even if it's not pointing some place valid. It's just not wise to do so.
Quzah.
Hope is the first step on the road to disappointment.
"The expression 0x0FFE yields an integer type and can not be dereferenced. You can use a cast to create an expression that yields a pointer type"
This clears things up a lot.
Thanks guys