472,958 Members | 2,305 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Can anybody tell me the reason

can anybody tell me the reason why following code is not working?
int *funct()
{
int p=10;
return &p;
}
int *ptr;
funct() = ptr; //------->Giving Compile time error
(LValue)


can anybody plz tell me the reason of this.

Jul 4 '06 #1
12 1466
Ashu wrote:
int *funct()
{
int p=10;
return &p;
}
First do not return address of local variable. This is not a compile
error but the effect is undefined and it may lead to

exception. Compiler gives a warning on this statement.
int *ptr;
funct() = ptr; //------->Giving Compile time error
(LValue)

can anybody plz tell me the reason of this.
it should be like this..

ptr = funct();

we should assign a value to left hand side variable (even in general
math).

-- Murali Krishna.

Jul 4 '06 #2

"Ashu" <as***********@gmail.comwrote in message
news:11**********************@j8g2000cwa.googlegro ups.com...
can anybody tell me the reason why following code is not working?
int *funct()
{
int p=10;
return &p;
}
int *ptr;
funct() = ptr; //------->Giving Compile time error
func() is a rhv (Right hand value).
ptr is a lhv (Left hand value/variable).
In C and C++ you put on the left the thing you want to get changed (the
left hand value).

ptr = func();
means, Load into ptr the value of func().

func() = ptr;
does not work because you can not load a function with a value, you can only
pass them values (well, in some cases you can in c++, but that's a different
story).
(LValue)


can anybody plz tell me the reason of this.

Jul 4 '06 #3
Geo

Ashu wrote:
can anybody tell me the reason why following code is not working?
int *funct()
{
int p=10;
return &p;
}
int *ptr;
funct() = ptr; //------->Giving Compile time error
(LValue)


can anybody plz tell me the reason of this.
Looks like you haven't bothered to read even the most basic C++ book
!!!!

Jul 4 '06 #4

Geo wrote:
Ashu wrote:
can anybody tell me the reason why following code is not working?
int *funct()
{
int p=10;
return &p;
}
int *ptr;
funct() = ptr; //------->Giving Compile time error
(LValue)


can anybody plz tell me the reason of this.

Looks like you haven't bothered to read even the most basic C++ book
!!!!



Boss I think u need to study some book nd for kind information of u
all, u can assign like this

func() = ptr
there is no problem in that if ptr is reference to some int and func()
also returning some reference but the problem is coming in case of
pointers only and i want to know why.

and please please u all who are saying that this is wrong or we do it
other way in maths also , study some good books about LValue and then u
will know that this works and how exactly C++ works.
nyways thanks for ur replies

Jul 5 '06 #5
Ashu wrote:
>
Boss I think u need to study some book nd for kind information of u
all, u can assign like this

func() = ptr
there is no problem in that if ptr is reference to some int and func()
also returning some reference but the problem is coming in case of
pointers only and i want to know why.

and please please u all who are saying that this is wrong or we do it
other way in maths also , study some good books about LValue and then u
will know that this works and how exactly C++ works.
nyways thanks for ur replies
Can we have that again in something approaching English?

--
Ian Collins.
Jul 5 '06 #6
"Ashu" wrote:
int *funct()
{
int p=10;
return &p;
}
You're trying to return the address of a local. That won't
work, because the local ceases to exist when you exit funct.
int *ptr;
You failed to initializ ptr, so you don't know where it's
pointing, nor do you know what it's pointing at.
funct() = ptr; //------->Giving Compile time error
There's 3 different errors there:
1. Uninitialized pointer
2. Unknown target
3. Assigning to an rvalue

If you're trying to get one function to alter a variable in
another, that can be done, but you'd have to declare p as "static"
and return a reference, like so:

#include <iostream>
int& funct()
{
static int p = 10;
std::cout << p << std::endl;
return p;
}

int main()
{
int x = 17;
funct() = x; // prints "10", then assigns 17 to "p" in "funct"
funct(); // prints "17"
return 0;
}
--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 5 '06 #7
Geo

Ashu wrote:
Geo wrote:
Ashu wrote:
can anybody tell me the reason why following code is not working?
>
>
int *funct()
{
int p=10;
return &p;
}
>
>
int *ptr;
funct() = ptr; //------->Giving Compile time error
(LValue)
>
>
>
>
can anybody plz tell me the reason of this.
Looks like you haven't bothered to read even the most basic C++ book
!!!!




Boss I think u need to study some book nd for kind information of u
all, u can assign like this

func() = ptr
there is no problem in that if ptr is reference to some int and func()
also returning some reference but the problem is coming in case of
pointers only and i want to know why.

and please please u all who are saying that this is wrong or we do it
other way in maths also , study some good books about LValue and then u
will know that this works and how exactly C++ works.
nyways thanks for ur replies
Besides the fact that you code is completely broken, the fact that it
doesn't even compile suggests that it isn't in fact "how exactly C++
works".

Jul 5 '06 #8
Ashu wrote:
Boss I think u need to study some book nd for kind information of u
all, u can assign like this

func() = ptr

there is no problem in that if ptr is reference to some int and func()
also returning some reference but the problem is coming in case of
pointers only and i want to know why.

and please please u all who are saying that this is wrong or we do it
other way in maths also , study some good books about LValue and then u
will know that this works and how exactly C++ works.
nyways thanks for ur replies.
Hi Ashu,

I think you are from India (me too).
My friend, don't just start an argument. People here know more than
you and me.
I have seen people from all over the world answering to complex
questions.
Most of the time I try to analyze what they write.

If you or me really know how C++ works, we wouldn't have come here.
And this is your second post..
Don't be such emotional. C++ is just one more language to learn.

and in math, I never did in that way.

-- Murali Krishna

Jul 5 '06 #9

Murali Krishna wrote:
Ashu wrote:
Boss I think u need to study some book nd for kind information of u
all, u can assign like this

func() = ptr

there is no problem in that if ptr is reference to some int and func()
also returning some reference but the problem is coming in case of
pointers only and i want to know why.

and please please u all who are saying that this is wrong or we do it
other way in maths also , study some good books about LValue and then u
will know that this works and how exactly C++ works.
nyways thanks for ur replies.

Hi Ashu,

I think you are from India (me too).
My friend, don't just start an argument. People here know more than
you and me.
I have seen people from all over the world answering to complex
questions.
Most of the time I try to analyze what they write.

If you or me really know how C++ works, we wouldn't have come here.
And this is your second post..
Don't be such emotional. C++ is just one more language to learn.

and in math, I never did in that way.

-- Murali Krishna

Hi Murali

Thanks buddy for ur reply and I am sorry to all for my remarks.

Actually you are right we all are like small fish in this sea of C++
which is too deep to master fully........

nyways thanks again for the replies, hope to put more such studid
queries soon buddies...

bye

Jul 5 '06 #10
Hi Murali
>
Thanks buddy for ur reply and I am sorry to all for my remarks.
a happy and sentimental ending in Indian cinema way..
Actually you are right we all are like small fish in this sea of C++
which is too deep to master fully........
not a sea. Faculties tell them in that way.. that Java is an ocean,
C++ is vast.

I can't learn some thing if it is like an ocean.

what does buddies mean? I really dont know.

-- Murali Krishna

Jul 5 '06 #11
Ashu schrieb:
Geo wrote:
>Ashu wrote:
>>can anybody tell me the reason why following code is not working?
int *funct()
{
int p=10;
return &p;
}
int *ptr;
funct() = ptr; //------->Giving Compile time error
(LValue)
[...]
>

Boss I think u need to study some book nd for kind information of u
all, u can assign like this

func() = ptr
If func() returns an Lvalue, you can do that.

However, a pointer by itself is an rvalue, just like an int:

int getInt() { return ... }
int* getPointer() { return ... }

int i;

getInt() = 10; // won't work
getPointer() = &i; // won't work

But a pointer can be used to form an lvalue. By dereferencing the
pointer, you get an lvalue for the pointee:

*getPointer() = 10; // works (or better: compiles)

But!! the function above is dangerous:
>>int *funct()
{
int p=10;
return &p;
}
This will return a pointer to a local variable, which lifetime ends by
leaving the function. If you dereference the pointer, you invoke
undefined behaviour.

You can return a pointer to a static variable, which is often used for
singletons (but I would return a reference):

class Singleton;

Singleton* getSingleton()
{
static Singleton instance;
return &instance;
}

--
Thomas
Jul 5 '06 #12
"Murali Krishna" <pm*********@gmail.comwrote in message
news:11*********************@a14g2000cwb.googlegro ups.com...
>Hi Murali

Thanks buddy for ur reply and I am sorry to all for my remarks.

a happy and sentimental ending in Indian cinema way..
>Actually you are right we all are like small fish in this sea of C++
which is too deep to master fully........

not a sea. Faculties tell them in that way.. that Java is an ocean,
C++ is vast.

I can't learn some thing if it is like an ocean.

what does buddies mean? I really dont know.

-- Murali Krishna
"Buddy" is a variation on "chum", "mate", "pal", etc. They all mean
something like "friend".

HTH :)
Stu
Jul 5 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
9
by: andrew | last post by:
Hi, I posted this message recently, but received no response, so I figured I'd try again. I can't find anybody who can tell me what's going on here. I'm having a problem with fieldsets in IE...
29
by: Roy Gourgi | last post by:
Hi, I am new to C#. I have the same time scheduling program written in C++ and it is 5 times faster than my version in C#. Why is it so slow as I thought that C# was only a little slower than...
1
by: Allen Maki | last post by:
/* I am trying to practice multidimensional array in a managed class using dynamical allocation. If I tried to compile the following codes, I would get the under mentioned c2440 error...
1
by: dineshchand | last post by:
CAn anybody tell me about nmake options and how can create custom nmake options for own needs ??
5
by: Mufasa | last post by:
Does anybody have a slick way to tell if people are on the system. I have a login so I can tell if a user is authenticated. What I've done now is make a dataset that I store in the application...
6
by: raylopez99 | last post by:
Anybody use Strong Name Signing? I think this is used by default for Resource files, which is one reason perhaps I can't get my resource files to work (somehow the public key is messed up, perhaps...
2
by: maifs | last post by:
hi..... can anybody tell me that how can i save my all application's work. mean that how can i save the images,circles,rectangle,lines or etl together.. if somebody knows it then plz explain me...
1
by: tarunsingh19 | last post by:
pls anybody tell me wether oracle 8i can be installed in windows xp sevice pack 2?or will it work in XP2, if yes then where we get this software free download file?
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.