473,881 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about pointers?

I am under the the impression that a variable is what is stored in a memory
address and a pointer is the memory address of the variable?

I was looking to use a function that does not return a value void
whatever(whatev er) and not have a variable global but in main and change the
value in the void whatever function.

using namespace std;
void whatever(int);

int main()
{
int test;
test = 1;
whatever(test);
return 1;
}
void whatever(int testing)
{
&test = testing
// The above line I am unsure of how to exactly do it.
//what would I need to change in order to change the
//value of test in main though the whatever function
//without actually having to return a value.
}
Thank you,
Shawn Mulligan
Jul 19 '05 #1
11 2582
kazack wrote:
I am under the the impression that a variable is what is stored in a memory
address and a pointer is the memory address of the variable?

I was looking to use a function that does not return a value void
whatever(whatev er) and not have a variable global but in main and change the
value in the void whatever function.

using namespace std;
void whatever(int);

int main()
{
int test;
test = 1;
whatever(test);
return 1;
}
void whatever(int testing)
Just make the above:
void whatever(int& testing)

and any changes made to `testing' will be reflected in the caller.
{
&test = testing
// The above line I am unsure of how to exactly do it.
//what would I need to change in order to change the
//value of test in main though the whatever function
//without actually having to return a value.
}


....and *please* look up `reference' in your favorite C++ documentation.

HTH,
--ag
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Jul 19 '05 #2
kazack wrote:
I am under the the impression that a variable is what is stored in a
memory address and a pointer is the memory address of the variable?
More or less. You could rather say that a variable must be somewhere in
memory, and every piece of memory has an address. A pointer can hold
such an address.
I was looking to use a function that does not return a value void
whatever(whatev er) and not have a variable global but in main and
change the value in the void whatever function.

using namespace std;
void whatever(int);

int main()
{
int test;
test = 1;
whatever(test);
return 1;
}
void whatever(int testing)
{
&test = testing
// The above line I am unsure of how to exactly do it.
//what would I need to change in order to change the
//value of test in main though the whatever function
//without actually having to return a value.


You cannot change the variable 'test' from main this way, because it is
passed to the function by value, i.e. a copy of it is made, and your
whatever() function only sees that copy. Any modification to the
'testing' parameter will only modify the local copy in the function.
For doing what you want to do, you need to pass by reference, i.e.:

void whatever(int& testing)
{
//do some changes to testing
}

This way, testing will be a reference, which means that it is just
another name for the variable 'test' from main(), so any change to
'testing' in whatever() will result in a changed 'test' in main().
Jul 19 '05 #3
Is there anyway possible also with the use of pointers to reference a
variable, that is not global and is not passed directly into a function?

Or does it have to be global or passed?

Thank You,
Shawn Mulligan
Jul 19 '05 #4

"kazack" <ka****@talon.n et> wrote in message
news:Ip******** ************@ne ws1.epix.net...
Is there anyway possible also with the use of pointers to reference a
variable, that is not global and is not passed directly into a function?
Yes.

#include <iostream>

void changeit(int *arg)
{
*arg = 42;
}

int main()
{
int i = 0;
std::cout << i << '\n'; /* prints 0 */
changeit(&i)
std::cout << i << '\n'; /* prints 42 */
return 0;
}

This is the way it's done in C (and can also be done this
way in C++, although references are more 'idiomatic'
in C++).

Or does it have to be global or passed?


All functions have access to a 'global' object.
This is the reason why such 'globals' should be
avoided if possible, it leaves open the possibility
of a function changing something which it should
not.
-Mike
Jul 19 '05 #5
In article <7y************ ********@news1. epix.net>,
kazack <ka****@talon.n et> wrote:
I am under the the impression that a variable is what is stored in a memory
address and a pointer is the memory address of the variable?


I would say that a variable is a memory location whose contents can be
changed (unless it is declared as const), and a pointer is a variable that
contains a memory address. That address is usually the address of another
variable (or at least it's intended to be).

--
Jon Bell <jt*******@pres by.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #6
"kazack" <ka****@talon.n et> wrote in message news:<Ip******* *************@n ews1.epix.net>. ..
Is there anyway possible also with the use of pointers to reference a
variable, that is not global and is not passed directly into a function?


You have to pass it in. If a variable is local to a function and you
call another function, the only way to access that variable is by
passing it in by reference or it's address as a pointer.

Actually this isn't exactly true... you can store the address in an
external pointer, a la:

int *bad_example;

int main() {
int value_to_change = 10;
bad_example = &value_to_chang e;
foo();
cout << value_to_change ; // Should print 20
}

void foo() {
*bad_example = 20;
}

But you *do* need a global variable. And at any rate, the above
example is bad bad and bad.

It violates good design rules left and right. Global variables are
shunned in general, and using one to change a local variable is even
worse. Generally what a function does should be clear from it's name
and the parameters; foo() above doesn't make clear what it is doing at
all. So if your function needs to change another value, either pass in
the value as a reference parameter (or pointer; some people prefer
this for some good reasons, though syntactically references are
clearer) or return it.
Jul 19 '05 #7
Wow I should of just stuck with VB. I am learning alot between the book I
am using and you guys here.

The sample program I am writing from the book which I have working is to
JUST read i a file with three sides to a triangle, multiple triangle
dimensions listed, and tell what type of triangle it is if it is a valid
triangle. Well I got that done with no problem but I like pushing the
envelope a little bit more than just doing what they ask. I have added the
ability to check for file existance, which was OS dependant until I was told
about cerrno header file. Big help, I didn't even know this existed, it's
not in my book anywhere, a matter of fact in none of my books I have and I
have 3 c++ books and 2 c books. Now I am learning about pointers 2 chapters
ahead of time to accomplish what I need done. I need to call a function
from within a function from within a function and I did not feel like having
to pass variables from the first function to the second function so I can
work with it in the third. Is this the standard way to do things? Or is
this bad coding habits? I am teaching myself here as you guys can prolly
tell by my questions i am and have asked?

Is it good or bad to pass a variable into a function just to beable to pass
it into another function so it can be used?

Thank you guys again for your help,
Shawn Mulligan

"Evan" <ee****@psu.edu > wrote in message
news:3f******** *************** ***@posting.goo gle.com...
"kazack" <ka****@talon.n et> wrote in message

news:<Ip******* *************@n ews1.epix.net>. ..
Is there anyway possible also with the use of pointers to reference a
variable, that is not global and is not passed directly into a function?


You have to pass it in. If a variable is local to a function and you
call another function, the only way to access that variable is by
passing it in by reference or it's address as a pointer.

Actually this isn't exactly true... you can store the address in an
external pointer, a la:

int *bad_example;

int main() {
int value_to_change = 10;
bad_example = &value_to_chang e;
foo();
cout << value_to_change ; // Should print 20
}

void foo() {
*bad_example = 20;
}

But you *do* need a global variable. And at any rate, the above
example is bad bad and bad.

It violates good design rules left and right. Global variables are
shunned in general, and using one to change a local variable is even
worse. Generally what a function does should be clear from it's name
and the parameters; foo() above doesn't make clear what it is doing at
all. So if your function needs to change another value, either pass in
the value as a reference parameter (or pointer; some people prefer
this for some good reasons, though syntactically references are
clearer) or return it.

Jul 19 '05 #8

"kazack" <ka****@talon.n et> wrote in message
news:SU******** ************@ne ws1.epix.net...
Wow I should of just stuck with VB. I am learning alot between the book I
am using and you guys here.

The sample program I am writing from the book which I have working is to
JUST read i a file with three sides to a triangle, multiple triangle
dimensions listed, and tell what type of triangle it is if it is a valid
triangle. Well I got that done with no problem but I like pushing the
envelope a little bit more than just doing what they ask. I have added the ability to check for file existance, which was OS dependant until I was told about cerrno header file. Big help, I didn't even know this existed, it's
not in my book anywhere, a matter of fact in none of my books I have and I
have 3 c++ books and 2 c books. Now I am learning about pointers 2 chapters ahead of time to accomplish what I need done. I need to call a function
from within a function from within a function and I did not feel like having to pass variables from the first function to the second function so I can
work with it in the third. Is this the standard way to do things? Or is
this bad coding habits? I am teaching myself here as you guys can prolly
tell by my questions i am and have asked?

Is it good or bad to pass a variable into a function just to beable to pass it into another function so it can be used?

Thank you guys again for your help,
Shawn Mulligan
The program you are writing doesn't sound particularly complicated, so I'm
guessing that it is not unreasonably long. Why don't you post your code and
maybe someone can point out any flaws in your reasoning. Yes, it can be
annoying to pass a value from one function to another, but it's not bad
coding.

By the way, to the best of my knowledge, VB won't allow you to get the
address of a variable that is inside another function, so I don't see how
staying with VB would have helped you.

"Evan" <ee****@psu.edu > wrote in message
news:3f******** *************** ***@posting.goo gle.com...
"kazack" <ka****@talon.n et> wrote in message

news:<Ip******* *************@n ews1.epix.net>. ..
Is there anyway possible also with the use of pointers to reference a
variable, that is not global and is not passed directly into a
function?
You have to pass it in. If a variable is local to a function and you
call another function, the only way to access that variable is by
passing it in by reference or it's address as a pointer.

Actually this isn't exactly true... you can store the address in an
external pointer, a la:

int *bad_example;

int main() {
int value_to_change = 10;
bad_example = &value_to_chang e;
foo();
cout << value_to_change ; // Should print 20
}

void foo() {
*bad_example = 20;
}

But you *do* need a global variable. And at any rate, the above
example is bad bad and bad.

It violates good design rules left and right. Global variables are
shunned in general, and using one to change a local variable is even
worse. Generally what a function does should be clear from it's name
and the parameters; foo() above doesn't make clear what it is doing at
all. So if your function needs to change another value, either pass in
the value as a reference parameter (or pointer; some people prefer
this for some good reasons, though syntactically references are
clearer) or return it.


Jul 19 '05 #9
"kazack" <ka****@talon.n et> wrote in message news:<SU******* *************@n ews1.epix.net>. ..
The sample program I am writing from the book which I have working is to
JUST read i a file with three sides to a triangle, multiple triangle
dimensions listed, and tell what type of triangle it is if it is a valid
triangle. Well I got that done with no problem but I like pushing the
envelope a little bit more than just doing what they ask.
This is good.
I have added the
ability to check for file existance, which was OS dependant until I was told
about cerrno header file.
cerrno? What are you doing to open files? This is a header inherited
from C that shouldn't be necessary unless I'm missing something.
Big help, I didn't even know this existed, it's
not in my book anywhere, a matter of fact in none of my books I have and I
have 3 c++ books and 2 c books.
This is not a surprise; even in Stroustrup I can't seem to find
mention other than in the list of standard headers.
Now I am learning about pointers 2 chapters
ahead of time to accomplish what I need done. I need to call a function
from within a function from within a function and I did not feel like having
to pass variables from the first function to the second function so I can
work with it in the third. Is this the standard way to do things? Or is
this bad coding habits?
It depends on the functions; I'd have to see your specific example to
give you my opinion. However, there are times when a function doesn't
do anything with one or more of its argument except pass it to another
function.
I am teaching myself here as you guys can prolly
tell by my questions i am and have asked?


That's what we're here for. If we weren't learning there'd be no
purpose for the newsgroup. :-p
Jul 19 '05 #10

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

Similar topics

16
2028
by: cppaddict | last post by:
Hi, I am deleting some objects created by new in my class destructor, and it is causing my application to error at runtime. The code below compiles ok, and also runs fine if I remove the body of the destructor. So I think I am somehow using delete incorrectly, but I'm not sure exaclty what I'm doing wrong. I'd apprecitate any clarification and suggestions for rewriting the below properly.
11
1799
by: DamonChong | last post by:
Hi, I am new to c++. I recently spend an enormous among of time troubleshooting a seeminly innocuous piece of code. Although I narrow down this piece of code as the culprit but I don't understand why. Can some guru help to enlighten me? Thank you. // I created an array of pointers to object pointers: Object ** obs = new Object * ;
27
3418
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined as ff: typedef struct { float *data ; int count ;
20
6582
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some area of ram that's not reserved by the program. Accessing memory through such pointer is likely to result in core dump (memory access violation)"
9
1959
by: Alfonso Morra | last post by:
Hi, I am having some probs with copying memory blocks around (part of a messaging library) and I would like some confirmation to make sure that I'm going about things the right way. I have some data types defined thus: typedef enum { ONE ,
9
5282
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where w1 and w2 make up the first string and, w3 and w4 make up the second string. I do not want to allocate memory, then put the words together to create a string only to facilitate strcmp() comparison. My question; Does anyone know how to get the...
21
1860
by: Bo Yang | last post by:
As long as I write c++ code, I am worry about the pointer. The more the system is, the dangerous the pointer is I think. I must pass pointers erverywhere, and is there a way to ensure that every object pointered by any pointer will be deleted and only be deleted once?
3
1629
by: Filimon Roukoutakis | last post by:
Dear all, assuming that through a mechanism, for example reflexion, the Derived** is known explicitly. Would it be legal (and "moral") to do this conversion by a cast (probably reinterpret would work here)? The conversion is done for this purpose: I have an std::map<std::string, Base*>. I want to "associate" Derived* handles to the stored Base* so when Base* in the map changes (ie points another address), the Derived* handle outside of...
21
1570
by: Chad | last post by:
At the following url http://c-faq.com/lib/qsort2.html, they have the following Q: Now I'm trying to sort an array of structures with qsort. My comparison function takes pointers to structures, but the compiler complains that the function is of the wrong type for qsort. How can I cast the function pointer to shut off the warning? A: The conversions must be in the comparison function, which must be declared as accepting ``generic...
17
2338
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the same as its address. The second one simply depends on a term that is not well-defined. Most people consider the type to be an important part of the notion of
0
9928
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
9776
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
11100
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...
0
10718
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10402
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9555
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7953
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
7111
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();...
1
4597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.