As for your pointer question in previous thread( with opengl context ). The only reason why there is pointer in that code, is so that it when passed in the function, it can get modified, as mike explained it already.

Can any one tell me what that means "passed in the function, it can get modified".

Not being narcissistic, but I have no idea what that means.

And to Mike your post sort of is irrelevant on some ends but right on others.

For example you explained that the things I'd need to do in order to make a processor were only the physical ways but not the logical and mechanical/electrical ones.

You also stated that there's no point in making my own language.....But why not? Sure, no one will know it at first. Just like C++....At first no one knew it and now millions of people do. You gotta start with no users and eventually get some. That goes for every thing. So it's kind of wrong to say it's pointless for me to make my own programming language because, for one, even if others wouldn't use it I still would and that was basically the whole point to begin with.

And then say you for some reason you started to create another language, well that hell lot of crap uses other hell lot of crap, and it will be a never ending cycle to you, until you finally, start creating your own hardware in your basement, possibly using tools that you might know how it works and then there goes the cycle again.

No, because I could invent the tools needed to make it and not use any one others, such as tool developing companies and such. I could go old mother nature style and use wooden stakes and steel and stone materials to carve, develop and hardwire things myself with out the involvement of other companies or factions. The only real issue would be creating an instruction set from scratch and then a programming language because I fear that's going to be extremely difficult beyond even your imagination......So that kind of puts me at fear, but I still have desire to do this.

>>"passed in the function, it can get modified"

You can compile and run this to see:

#include <iostream>

void pass_by_pointer(int* ptr) { 
  *ptr = 42;
};

void pass_by_reference(int& ref) {
  ref = 69;
};

void pass_by_value(int value) {
  value = 187;
};

int main() {
  int my_var = 0;
  std::cout << "the value of my_var is " << my_var << std::endl;
  pass_by_pointer(&my_var);
  std::cout << "the value of my_var is now " << my_var << std::endl;
  pass_by_reference(my_var);
  std::cout << "the value of my_var is now " << my_var << std::endl;
  pass_by_value(my_var);
  std::cout << "the value of my_var is now " << my_var << std::endl;
  return 0;
};

That should make it clear. Btw, the syntax is:

int Foo; // declares a variable named Foo, of type int.
int* Foo; // declares a pointer named Foo that points to a variable of type int.
int& Foo; // declares a reference named Foo to a variable of type int.

>>"I could go old mother nature style and use wooden stakes and steel and stone materials to carve, develop and hardwire things myself with out the involvement of other companies or factions."

You are aware that steel is not found in nature. It is manufactured (and not particularly easy to make). So you do acknowledge that _some_ use of things that others have developed is OK. It is a matter of deciding what. You can go back to the stone age and start from scratch, but not using the expertise, knowledge, techniques and skills that mankind has accumulated in the past thousands of years is just ludicrous. If you think you can make good processors, why don't you apply here or here.

>>"passed in the function, it can get modified"

You can compile and run this to see:

#include <iostream>

void pass_by_pointer(int* ptr) { 
  *ptr = 42;
};

void pass_by_reference(int& ref) {
  ref = 69;
};

void pass_by_value(int value) {
  value = 187;
};

int main() {
  int my_var = 0;
  std::cout << "the value of my_var is " << my_var << std::endl;
  pass_by_pointer(&my_var);
  std::cout << "the value of my_var is now " << my_var << std::endl;
  pass_by_reference(my_var);
  std::cout << "the value of my_var is now " << my_var << std::endl;
  pass_by_value(my_var);
  std::cout << "the value of my_var is now " << my_var << std::endl;
  return 0;
};

That should make it clear. Btw, the syntax is:

int Foo; // declares a variable named Foo, of type int.
int* Foo; // declares a pointer named Foo that points to a variable of type int.
int& Foo; // declares a reference named Foo to a variable of type int.

Okay, I ran it and it just shows the same variable with its value changed. But how does that help me understand it better? The code doesn't make enough sense for me to get any real grasp on, and the output of the program doesn't do much but leave me hanging. And it's not that I'm not trying to understand it it's just that it makes very little sense to me.

Plus, you didn't even add any function to hold the window open so I had to add

system("PAUSE");

Okay, I ran it and it just shows the same variable with its value changed. But how does that help me understand it better? The code doesn't make enough sense for me to get any real grasp on, and the output of the program doesn't do much but leave me hanging. And it's not that I'm not trying to understand it it's just that it makes very little sense to me.

Plus, you didn't even add any function to hold the window open so I had to add

system("PAUSE");

I also removed the pointers in my Windows and OpenGL project and it wouldn't compile, so that strikes me that they're obviously needed, but still could use more help understanding how or why.

>>Plus, you didn't even add any function to hold the window open so I had to add
That's just a thing that is specific for Windows and when you are running it from an IDE like Visual Studio or Code.Blocks. I'm using Linux, and I had no way of knowing what you are using.

>>it's just that it makes very little sense to me.
You have to do your part too. First, read the definition of a pointer. Then, read the definition of a reference. Then ask yourself, in the code that I posted, why is it so that the first two function calls change the value of my_var in the main() function, and why the last function call does not even though all three functions have the same implementation.

>>Plus, you didn't even add any function to hold the window open so I had to add
That's just a thing that is specific for Windows and when you are running it from an IDE like Visual Studio or Code.Blocks. I'm using Linux, and I had no way of knowing what you are using.

>>it's just that it makes very little sense to me.
You have to do your part too. First, read the definition of a pointer. Then, read the definition of a reference. Then ask yourself, in the code that I posted, why is it so that the first two function calls change the value of my_var in the main() function, and why the last function call does not even though all three functions have the same implementation.

Well I asked myself and didn't get an answer.

And I already know the definition of pointers and references, but that still doesn't make the cut for me. I still don't get it.

Well I asked myself and didn't get an answer.

And I already know the definition of pointers and references, but that still doesn't make the cut for me. I still don't get it.

It makes no sense, I tried to do what you said and all I got was errors are outputs completely different than the ones you gave.

I used this code:

int *hi;
cout << &hi;

All I get is the address in memory, but what use does that have for me? I have no use to know its' address. And this "passing function" but still makes not a pinch of sense.

You realize there is nobody to blame for the fact that you just don't get it and you will quit programming eventually.It should act as a solid fact to you that other people got it by putting effort into learning pointers and now they are happily writing software or web pages on which you can complain about your inability to comprehend simple stuff.Also real programmers have answered your thread like narue and mike , trying to help you so at least you can come up with a 'thanks'.Finally .. to be on topic a pointer is just an integer representing an address at which some data is stored

int* pointer;
int data;
pointer=&data; // which translates to 'pointer is the address of data'
*pointer; // the object pointed by pointer .. which is 'data'
//this is the basis , it's so simple a ten year old will get it.Just put effort

i think the problem he has is y are pointer necessary like me i use it but i dont what makes it different or special e.g char arr[10]="google";
char *arr;
arr="google";
using pointer doesnt make sense to me but i still use but dont know when to use i just use it anyhow

If I have seen further it is only by standing on the shoulders of giants.

Sir Isaac Newton

Okay, here's a more clear question.....

Judging by the ability of how many understand pointers but not know where and how to use them, why not explain where and how they are normally used in bigger programs(programs that use API, GUI or more than a console window)?

>>why not explain where and how they are normally used in bigger programs(programs that use API, GUI or more than a console window)?

HANDLE is a standard Windows pointer type. If you go to msdn.microsoft.com and make a search for "HANDLE" under win32 API, you will find 495,000 hits (just about every function in the win32 API). Is that enough examples for you?

Qt is a cross-platform tool for building GUIs, in that framework, almost every significant piece of the GUI is stored and used via a pointer to it. This is mainly because of polymorphism in OOP. Although they often use smart-pointers too (which you may or may not consider to be a pointer).

The vast majority of libraries that you can use via DLLs or shared libraries have C-style interfaces that pass pointers to-and-fro their API functions (like Win32 does). A simple example is FreeImage that is a library used to load, manipulate and save any image file format. When you want to access the image's data (like pixels), you call a function, get a pointer back, and dereference the pointer to access the data. A huge number of libraries work in a very similar way.

..... blablabla .... the number of examples of real-life uses of pointers is uncountable. They are ubiquitous. They are only (almost) avoidable in open-architecture projects (basically a library with which you interact directly with the source code of the library and not with an API or DLL or shared library, for example, the Boost Libraries). Closed architecture libraries have to pass through C-style interfaces (APIs) which cannot pass objects or references, so pointers are thus necessary for proper Application Binary Interfacing (ABI) and for memory management.

>>Judging by the ability of how many understand pointers but not know where and how to use them
May I remind you that your original question implied as a premise that you knew what pointers are, where they are used, and how. You were asking whether it was possible to avoid them, that implies you already knew what they were used for and wanted alternatives to pointers to achieve the same results (and that is what my first post was addressing). You can only get answers to the questions you ask. So in summary (off the top of my head):

What:
Pointers are a record of an address in memory. E.g. a pointer such as "int*" can hold the address in memory of a variable of type int (which could possibly be the first int in a sequence of ints, i.e. an array).

Where:
- Pointers is one way to pass to a function an address where some data ought to be written. (the other way is a reference)
- Pointers is one way to have multiple access points to the same variable or array. (the other way is a reference)
- Pointers can be used as opaque references to objects that can only be manipulated by a certain set of functions (e.g. APIs).
- Pointers are used as the implicit argument to any non-static member function call to refer to the object that was attached to the call (the "this" pointer).
- Pointers can also point to functions (C/C++) or member functions (C++) such that any function can be defined as the function to be called when a certain event has occurred or a certain point of execution is reached (e.g. a "callback mechanism").
- Pointers are intrinsically used in program execution to keep track of where the position of the current instruction (or the next one), to identify the position of an instruction point where to jump to (function call), and to identify the bounds of the stack frame (the chunk of memory that a function can play with and store its local variables).
- ... and many more special cases.

How:
- A pointer is declared by adding a * (star) after the type to which it shall hold the address of:

int* ptr; //this declares a pointer to a variable of type int.

- A pointer "ptr" can be made to hold the address of variable "value" by the referencing or address-of operator & (leading the name), as so:

int value = 42; //this declares a variable with value 42.
ptr = &value; //this stores the address of variable "value" into "ptr".

- A pointer can be de-referenced by one of the de-referencing operators. In this simple case of ptr pointing to a single variable, the simplest de-reference operator is a * star leading name of the pointer, e.g. the following will always be true (unless ptr is changed):

( *ptr == value ) //this boolean operation will always be true

- If the pointer points to an array (a sequence of variables), then the indexing operator [ i ] can be used to refer to the ith element after the address stored in the pointer. As in:

int my_array[] = {0,1,2,3,4}; //defines an array with five integers.
int* ptr = my_array; //defines a pointer to the start of the array.
( ptr[0] == 0 ); // this is a true statement, accesses the first element.
( ptr[3] == 3 ); // this is a true statement, accesses the fourth element.

- In case the pointer points to an object of a class, then the members (data or functions) can be accessed via the dereference operator -> (in place of the . (dot) that would normally be used to access the members of an object).

Now... I hope you are finally satisfied. It helps to sometimes read books and online resources. They will go in much more details about what I quickly threw together in the above. Get a book on C++ (or almost any other programming language), I guarantee that any-decent-one of them will have at least an entire chapter dedicated to pointers (if not more).

Sorry, but you didn't really help because I still don't know exactly where to place the pointers in my Windows and OpenGL program.

What I want, as small or large as that may be to you, is for you to tell me exactly where, how and why I need to use pointers to use Windows API and OpenGL.

And you said there's an optional way instead of using pointers, so please...Telling me that would be highly appreciated.

Give an example of how I can use Windows API and OpenGL with out a single pointer and avoiding confusion.

I'd like to point out that the OP is a troll who is just repeating the same procedure that he already went through on other forums using various names.
He's just testing how far he can push people before he finally gets banned.
If you look at his other threads, you'll see they follow the same pattern: he is "extremely confused" about something or another that requires an in-depth explanation, effectively aiming to waste people's time. It's likely that he is actually already a more or less accomplished programmer with a lot of time on his hands.


@spoonlicker: This might be a shot in the dark, but: you wouldn't have used the name "Avast" for extended periods of time a few years back, would you? This would totally make my day.

I'd like to point out that the OP is a troll who is just repeating the same procedure that he already went through on other forums using various names.
He's just testing how far he can push people before he finally gets banned.
If you look at his other threads, you'll see they follow the same pattern: he is "extremely confused" about something or another that requires an in-depth explanation, effectively aiming to waste people's time. It's likely that he is actually already a more or less accomplished programmer with a lot of time on his hands.


@spoonlicker: This might be a shot in the dark, but: you wouldn't have used the name "Avast" for extended periods of time a few years back, would you? This would totally make my day.

No, that's a major lie. I ask because I expect it to be helped and/or explained to me. I am NOT an accomplished programmer because, for one, I can't even use pointers.

"Just like there is no point for me to try to make my own programming language, because I am humble enough to realize that I could not come up with anything better than C, C++, Python, etc.."

I disagree with this a little. I am humble enough to realize to know that I can't make a better language than the 400 other languages that I have learned but I make it a point to implement a language for any product that I am working on, so that it can be extended by others. Implementing a language on top of a product turns it into a platform instead of just a mere product.

"I can't even use pointers"

I suspect you are a troll. Your name is a hoot. In case you are not a troll, just give up on programming right away, because some people are born without the part of a brain that understands pointers, just like I was born without the part of the brain that understands travel.

A pointer is really an adress of s space in memory, no more. What confuses you may be usage of the pointers.

I like studying iterators before pointers. That makes things cristal clear.

Btw Accelerated C++ and C++ Primer Plus 4th edition rulez.

@OP, switch your language to java. There you don't have to worry anything about pointers.

Im not very experienced, but for the most part I think I know pointers at least to a point.(no pun intended -.-)

Before you learn about what a pointer is, I think it would be important you know what the reference operator (&) does. The reference operator stores the MEMORY location of a varible.

For instance this would store the memory location of a variable named "joe" to a variable named "bill" (assuming bill can store the memory location of joe.)

bill = &joe;

The character (*) is also called the "dereference" operator. That is, it dereference's a reference to a memory location. Or in other words, it POINTS to the VALUE of a MEMORY LOCATION.

So for instance, this code stores the value stored in joe, as bill is a reference to the memory location of joe.

bill = &joe;
tim = *bill;

Now I haven't told you yet, but bill is actually a pointer variable, that is why it's allowed to store a reference of joe. To create a pointer variable, you put a * before the varaible name you are declaring like this.

int *bill;

Now, you might be a little befuddled on why you would need such a thing, but for one, it allows you to create dynamic memory. That is, allowing you to store variables while the program is running.

For instance, because of pointers, you are allowed to do this.

int * bill = new int;

Wait--What does this do? Why it makes a new memory location to a int into a pointer variable. You can then use pointer bill to alter the value of the memory location that it points to. This allows you to go around things such as the limited size of an array, but to do so, it can be quite tricky.

A linked list for instance uses a pointer variable to POINT to the next value in it's list, with a pointer pointing to NULL meaning the end of the list is found. This allows you to make a list that can expand almost infinatly, except for a couple of limitations such as the capacity of memory that your computer can store.

Im not very experienced, but for the most part I think I know pointers at least to a point.(no pun intended -.-)

Before you learn about what a pointer is, I think it would be important you know what the reference operator (&) does. The reference operator stores the MEMORY location of a varible.

For instance this would store the memory location of a variable named "joe" to a variable named "bill" (assuming bill can store the memory location of joe.)

bill = &joe;

The character (*) is also called the "dereference" operator. That is, it dereference's a reference to a memory location. Or in other words, it POINTS to the VALUE of a MEMORY LOCATION.

So for instance, this code stores the value stored in joe, as bill is a reference to the memory location of joe.

bill = &joe;
tim = *bill;

Now I haven't told you yet, but bill is actually a pointer variable, that is why it's allowed to store a reference of joe. To create a pointer variable, you put a * before the varaible name you are declaring like this.

int *bill;

Now, you might be a little befuddled on why you would need such a thing, but for one, it allows you to create dynamic memory. That is, allowing you to store variables while the program is running.

For instance, because of pointers, you are allowed to do this.

int * bill = new int;

Wait--What does this do? Why it makes a new memory location to a int into a pointer variable. You can then use pointer bill to alter the value of the memory location that it points to. This allows you to go around things such as the limited size of an array, but to do so, it can be quite tricky.

A linked list for instance uses a pointer variable to POINT to the next value in it's list, with a pointer pointing to NULL meaning the end of the list is found. This allows you to make a list that can expand almost infinatly, except for a couple of limitations such as the capacity of memory that your computer can store.

Okay, but that doesn't explain why they HAVE to be used in some programs and can't be substituted for some thing else.

And yes it's the pointer use that's too damn confusing, not the actual understand of how they work.

How on Earth am I supposed to know every which and whatnot place to put a pointer to access memory in a major 6,000 line program using hundreds of them? It's hard enough remembering how to show a window in Windows programming, but no it just gets even more complicating and I'm sorry but I can not remember 30,000 things, places and points where pointers can go. That just makes programming even harder than the dreadfully tainted structure is already has to be made in.

If you know or have any way to completely OVERRIDE any use of pointers in all contexts then please let me know. And yes in C++.

we all know what pointer means and how to use it.
But the question is why to use it and when to use it.
Why to use it because it help manage memory.
When to use it everytime

we all know what pointer means and how to use it.
But the question is why to use it and when to use it.
Why to use it because it help manage memory.
When to use it everytime

Okay, is there any way to avoid using them in C++ or substitute them for some one easier, on my part?

As said before. Not if you're using any sort of API or library. If you personally write every line of every piece of software you ever create, then maybe, but it's neither easy, nor feasible for anything of any significance.

There is no magic method for eliminating pointers, they are a necessary evil and every programming situation is unique. At this point, you have 2 options:

  • Accept that, learn to use them, and progress.
  • Don't accept that, be lazy, refuse to learn, and don't progress.

Your answer is that simple.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.