ExclamationSolvedBasics of Pointers

Posts 1–9 of 9 · Page 1 of 1
Basics of Pointers
#1.
Code:
#include <iostream>
using namespace std;

void IloveC++(double *firstpointer)
{
     *firstpointer=9.95;
}

int main()
{
     double x=5.342;
     *secondpointer=&x;
     IloveC++(secondpointer);
     cin.get()
     system("pause")
}

#2
Code:
#include <iostream>
using namespace std;

void *IloveC++(double firstpointer)
{
     *firstpointer=9.95;
}

int main()
{
     double x=5.342;
     *secondpointer=&x;
     IloveC++(secondpointer);
     cin.get()
     system("pause")
}
My question is, what's exactly the difference between shoving in a pointer (*) between "void" and "IloveC++" , and shoving in a pointer (*) between "double" and "firstpointer"?

For a better understanding...

What is the difference between this:
Code:
void *IloveC++(double firstpointer)
...and this?
Code:
void IloveC++(double *firstpointer)
@giniyat101 specially ;p


---------- Post added at 07:05 PM ---------- Previous post was at 07:04 PM ----------

EDIT: Okay forget the "system("pause")" thing I made a mistake lol.
The general format of a function definition is this:

Code:
[decorations\calling-convention] [return data] [function name] ([argument 1], [argument 2], ... etc)
The first bit can be ommited, the decorations & calling convention.

void IloveCpp(double fValue);

Defines a function that does not return anything (void) but takes a double value as an argument. The data in this situation is coppied over to the function. Generally, for small bits of data, this is acceptable. However, for larger structures you must use pointers or references.

void* IloveCpp(double fValue);

This function returns a pointer to any piece of data. A void pointer cannot be dereferenced. I.e you can not do this:

Code:
void* pReturn = IloveCpp(1.24);
*pReturn = 25;
void pointers typically are used to store arbitrary pointers or references to regions of memory that cannot be effectivley described with a structure. Nowadays, there are a lot of tricky hacks you can pull off with meta-programming to get a structure to accurately describe a region of memory, so try to use a structure pointer wherever possible.

void ILoveCpp(double* pValue)

Describes a function which returns nothing, but takes a pointer to a double value. In C++ you would just use a reference here (if you haven't learned about references don't worry about it.) In this case, a pointer to a value is passed reather than copying the actual value over in to the function's context (much more optimal.)

It is also worth noting that you should not worry about returning structures. It varries with implementation by the comiler, but usually something like this:

MyLargeStructure createStructure(const char* sName);

turns in to something like this when optimized\implemented by the compiler:

void createStructure(const char* sName, MyLargeStructure* pReturn);
Quote Originally Posted by radnomguywfq3 View Post
The general format of a function definition is this:
... blah blah Imma baws blah blah
Oh my god, can it be true? The great Jetafag is once again among us?
Code:
#include <iostream>
using namespace std;

void IloveC++(double *firstpointer)
{
     *firstpointer=9.95;
}

int main()
{
     double x=5.342;
     *secondpointer=&x;
     IloveC++(secondpointer);
     cin.get()
     system("pause")
}
Your function requires a pointer, but you do not necessarily have to create a pointer to pass an address to the function, although sometimes it is beneficial to do so.

Example:
Code:
IloveC++(&x); // Here we are directly passing the address of 'x' to the function.
Just a little bit of extra info I thought I would put in there for you.
Quote Originally Posted by radnomguywfq3 View Post
void pointers typically are used to store arbitrary pointers or references to regions of memory that cannot be effectivley described with a structure. Nowadays, there are a lot of tricky hacks you can pull off with meta-programming to get a structure to accurately describe a region of memory, so try to use a structure pointer wherever possible.

void ILoveCpp(double* pValue)

Describes a function which returns nothing, but takes a pointer to a double value. In C++ you would just use a reference here (if you haven't learned about references don't worry about it.) In this case, a pointer to a value is passed reather than copying the actual value over in to the function's context (much more optimal.)

It is also worth noting that you should not worry about returning structures. It varries with implementation by the comiler, but usually something like this:

MyLargeStructure createStructure(const char* sName);

turns in to something like this when optimized\implemented by the compiler:

void createStructure(const char* sName, MyLargeStructure* pReturn);
Well I do understand what "void" does and I just wanted to try something I haven't for my history of coding.
So basically to say, if you shove in the pointer (*) in the function value, it only recognizes the exact value which it is pointing to, in this case, only 1, and if you shove it in at the function type, then it recognize every value that's within that function?
Sorry I'm kind of confused here. Or maybe I should just move on to learning Classes and Struct variables.


---------- Post added at 12:32 PM ---------- Previous post was at 12:31 PM ----------

Quote Originally Posted by M-I-K-e View Post
Your function requires a pointer, but you do not necessarily have to create a pointer to pass an address to the function, although sometimes it is beneficial to do so.
I know, I just wanted to practice some pointers and functions altogether.
A float pointer holds the address of any given float variable.

You can get the address of any accessible variable in memory by using the '&' token.

I.e

float myFloat = 2.0F

then you can store the address of the float:

float* pMyFloat = &myFloat;

pMyFloat is a pointer to myFloat. It contains the address of myFloat in memory, not the value of myFloat.

You can dereference a pointer by using the dereference operator (*)

float myFloatsValue = *pMyFloat.

It works the same way with function arguments.

I don't fully understand your question, but I think this is what you're asking?
Quote Originally Posted by radnomguywfq3 View Post
A float pointer holds the address of any given float variable.

You can get the address of any accessible variable in memory by using the '&' token.

I.e

float myFloat = 2.0F

then you can store the address of the float:

float* pMyFloat = &myFloat;

pMyFloat is a pointer to myFloat. It contains the address of myFloat in memory, not the value of myFloat.

You can dereference a pointer by using the dereference operator (*)

float myFloatsValue = *pMyFloat.

It works the same way with function arguments.

I don't fully understand your question, but I think this is what you're asking?
:s I don't really understand this.
Well I'm only on part 2 I have much more to go on pointers. I'll have a look at that first.
For now, close @Hassan
i didnt receive the mention :S
may i didnt notice
i think Jetamay already helped
Marked Solved. Jetamay answered it pretty well.
Posts 1–9 of 9 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us