473,761 Members | 9,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1544
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.goog legroups.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

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

Similar topics

303
17744
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. Yahoo store was originally written in Lisp. c. Emacs The issues with these will probably come up, so I might as well mention them myself (which will also make this a more balanced
9
1656
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 moving around on me as I hover from one table to another. The problem only appears in IE (I'm using v6). If I hover over the tables row, the bottom fieldset moves up and down as I move from on table to other.
29
2265
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 C++? What am I doing wrong? Here is my code: using System;
1
1028
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 message. It is only practice. You may notice that I am not using "private"
1
1475
by: dineshchand | last post by:
CAn anybody tell me about nmake options and how can create custom nmake options for own needs ??
5
1181
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 variable and everytime somebody loads a page, I update that dataset ( based on sessionid ) with the time the user last loaded a page. When it reaches more the 20 minutes, I make the assumption that the people must have gone elsewhere. Just...
6
5683
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 since I've installed so many versions of Visual Studio) RL http://msdn.microsoft.com/en-us/library/h4fa028b.aspx Deployment in Visual Studio
2
1189
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 with some code... i konw that image can be save via one of the Bitmap's method i.e: imageObj.Save(fileName);
1
2089
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?
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9376
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9923
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.