• 0

A question about c++ pointers


Question

Hello.

I'm writing this because I am not sure how to approach the subject of pointers. In the book I am using to learn the fundamentals of C++ from, "C++ without fear" by Brian Overland, I had reached the sixth chapter which focuses on pointers.

I have so far found that the explanation of what pointers do to be rather vague, and I am finding myself questioning both the usefulness, and the functions of pointers in c++.

So far all I have really ascertained is that pointers are a syntax system (if you'll call it that) that is rather distinctively a part of c++, and some other languages I'm sure. What I understand so far is that they basically point to the address of a variable stored on the pc, and that these addresses are organized by means of hexadecimal placement, or what-have-you. I have learned the basic use for them, such as *p = x, &a, *p=&a, etc... Whereby of course, the & generally means "the address of" and * would mean something like "this variable points to...", or so it is explained in my book.

I have also learned that they are basically used to change the arguments of functions, and can be used interchangeably by other functions to change the arguments of that very function, or that of other functions. In that sense, they make a program more dynamic.

However, the only example I have been given thus far is of a program using a function that changes the arguments of another function in order to sort an array's information from least to greatest.

To be honest, it's not that I am finding it hard to comprehend the algorithm of pointers, I am just failing to see their practical importance, or how they can be used to really affect a program.

Without delving too deeply into programming algorithm or syntax that I wouldn't understand, could anyone kindly try to elaborate on the concept of pointers, how they may be used and why, as well as giving perhaps even a better definition for what the (&, and *) symbols could be characterized by? (These "names" given to symbols help me remember them and their functions, so a better example would be great.)

Thanks in advance,

Sincerely, Ryuurei.

Link to comment
Share on other sites

12 answers to this question

Recommended Posts

  • 0
Hello.

I'm writing this because I am not sure how to approach the subject of pointers. In the book I am using to learn the fundamentals of C++ from, "C++ without fear" by Brian Overland, I had reached the sixth chapter which focuses on pointers.

I have so far found that the explanation of what pointers do to be rather vague, and I am finding myself questioning both the usefulness, and the functions of pointers in c++.

So far all I have really ascertained is that pointers are a syntax system (if you'll call it that) that is rather distinctively a part of c++, and some other languages I'm sure. What I understand so far is that they basically point to the address of a variable stored on the pc, and that these addresses are organized by means of hexadecimal placement, or what-have-you. I have learned the basic use for them, such as *p = x, &a, *p=&a, etc... Whereby of course, the & generally means "the address of" and * would mean something like "this variable points to...", or so it is explained in my book.

I have also learned that they are basically used to change the arguments of functions, and can be used interchangeably by other functions to change the arguments of that very function, or that of other functions. In that sense, they make a program more dynamic.

However, the only example I have been given thus far is of a program using a function that changes the arguments of another function in order to sort an array's information from least to greatest.

To be honest, it's not that I am finding it hard to comprehend the algorithm of pointers, I am just failing to see their practical importance, or how they can be used to really affect a program.

Without delving too deeply into programming algorithm or syntax that I wouldn't understand, could anyone kindly try to elaborate on the concept of pointers, how they may be used and why, as well as giving perhaps even a better definition for what the (&, and *) symbols could be characterized by? (These "names" given to symbols help me remember them and their functions, so a better example would be great.)

Thanks in advance,

Sincerely, Ryuurei.

Did you mean the normal names of the symbols,or a name specific to pointers? * is an asterisk, & is an ampersand. Pointers can help improve speed when doing repetitive tasks. (sorry, I would elaborate but I generally don't use them)

Link to comment
Share on other sites

  • 0

Almost,

I know what the symbols are, but I would somewhat like a better idea of a way to "name them by meaning".

Similar to how we think of cout as "console out", as it is.

So if they aren't really that big of a deal, one could generally get by without using them unless it's absolutely necessary to for some reason have to change the arguments of their functions or otherwise use variables previously placed, and changing their address?

Link to comment
Share on other sites

  • 0

Pointers are key in computer programming. Think of a very large data structure (i'm supposing you know about the "struct" construct in C++). A pointer represents the memory address of a structure, a class or any data type. It allows you to keep a reference to an object in a small and portable value. Whenever you want to modify this structure in code that isn't in the same method, instead of passing the entire structure, you can simply pass the address. This allows the other piece of code to modify the object directly (by de-referencing the pointer), but most importantly, it saves memory.

Link to comment
Share on other sites

  • 0

I see.

Are there any more practical ways of explaining their actual use, though?

Things, so far have seemed rather ambiguous as to how to effectively use them. Really, I'm not quite picking up on a very good way to use them in an actual program. I don't mean this in terms of how to logically use one, rather I don't see how it is literally and directly applied to the program.

Could you give an example with a small code patch?

Link to comment
Share on other sites

  • 0

Pointers are the memory addresses of objects or variables. Pointers simply accessing multiple objects of the same type. Rather than calling each object by name you can just point the pointer at it then use the pointer to access the object.

The thing to remember with pointers is that they are not objects themselves. They are memory addresses of objects. As such you need to dereference objects when you want to deal with the object and not the pointer itself.

* is used many ways. One way is to create a pointer and the other way is to dereference a pointer. Using & on a pointer will give you the address of the pointer itself. Keep in mind that a pointer actually is something in memory and as such has it's own address. So a pointer is something in memory that contains the address of something else.

Link to comment
Share on other sites

  • 0

Here's an example:

#include <iostream>
using namespace std;

typedef struct {
  int a;
  double b;
} Data;

void modify(Data* dp)
{
  dp->a = 5;
  dp->b = 1;
}

int main() {
  Data data;
  data.a = 4;
  data.b = 5;

  modify(&data);

  // Should show 5:1;
  cout << data.a << ":" << data.b;
}

Using the pointer, I was able to modify the contents of the Data structure from within another method without passing the entire object to that method.

Link to comment
Share on other sites

  • 0

Okay, so in more complicated structures and information-containing variables, pointers are just more useful as it doesn't have to constantly relocate the address of the variable you want to change every time you want to change it, and it doesn't require the manipulation of every piece of data. Thus allowing you to change the process of a function without having to completely rewrite an entire function or data structure, otherwise.

I think I have a decent idea of how it works now.

Link to comment
Share on other sites

  • 0

The purpose of pointers is mainly to manipulate and pass data around without actually copying it in memory. Several entities can use the same object, physically, because they all know its address (a pointer is just that, an address). Without pointers, there would be no way of passing an object to a function, only a copy of it; there would be no way of implementing useful containers because you could not add an already existing object to it, only a copy of the object. Basically you couldn't do any useful programming. It's as simple as that.

In fact, pointers are the most fundamental concept, because what your code ends up compiled into, and what the CPU deals with, is just memory addresses. In Java, object references are basically just pointers. In a non-managed environment like C++ or C, heap memory can only be used with pointers; local objects work in a very similar way, their address is just abstracted away beneath the concepts of functions and the stack, but you can still dereference them and get their address in memory. Anytime you use an object, you are doing an access in memory; C and C++ hide that for local objects, and higher-level languages like Java hide that for all objects. But at a fundamental level, everything is pointers.

Link to comment
Share on other sites

  • 0

If it helps, higher level languages such as C# will pass everything by reference(The &).

Pointers are key to making sure you don't have needless overhead. If you want something easier to use, try references. However, I would learn to not be afraid of pointers, they are a fundamental part of C++. And if that syntax scares you, just wait till you dip into the stl and start using all its templated bull ****.

Link to comment
Share on other sites

  • 0

Hi everyone.

I was trying to make a simple multiplication calculator today using pointers in a function, to help myself get a handle on them, but it seems I'm still having a bit of trouble with the logic on how to use them.

#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
int get_num();
int math(int *p, int z);
int main() {
	cout << "At any time, just press enter to exit the program." << endl;
	system ("pause");
	while (1) {
		  int x, y, z, ans, go, ag;
		  int *p;
		  p = &ans;
		  int *o;
		  o = &z;
		  cout << "Enter a number: ";
		  x = get_num();
		  if (x == 0) {
			 break;
			 }
		  cout << "Enter a second number: ";
		  y = get_num();
		  if (y == 0) {
			 break;
			 }
		  ans = x * y;
		  cout << x << " * " << y << " = " << ans << endl;
		  cout << "Would you like to multiply this by another number?" << endl;
		  cout << "Enter 1 for yes, 0 for no: "; 
		  go = get_num();
		  if (go == 0) {
			 break;
			 }
		  add:
			  cout << "Enter the number to use: ";
			  z = get_num();
			  if (z == 0){
				 break;
				 }
			  cout << "The result is: " << math(p, z) << endl;
			  cout << "To multiply another number onto this, enter a number: ";
			  ag = get_num();
			  if (ag == 0){
				 break;
				 }
		  goto add;
			  }
system ("pause");
return 0;
}

int get_num(){
	 char s[100];

	 cin.getline(s, 99);
	 if (strlen(s) == 0){
				   return 0;
				   }
	 return atoi(s);
	 }

int math(int *p, int *o){
	ans = (*p * *o);
	return ans;
}

If you can give me an idea of what I'm doing wrong, it would greatly help my understanding of pointers and the logic that is used in their actual application to a program.

Thanks.

Link to comment
Share on other sites

  • 0

What does the program do that you don't expect? I had a quick scan and did see anything majorly wrong. I'll have another look at it when it once I've had some food :)

Link to comment
Share on other sites

  • 0

OK a couple of points.

Your function prototype at the top of the program is incorrect, it should be, int math(int *p, int *z)

Secondly on this line you're passing the number by value when it should be by reference. The correct line is:

cout << "The result is: " << math(p, &z) << endl;

Of course the other solution is to remove the * in front of the z in the math function so that it's not a pointer. And in the math function you forgot to declare ans. Apart from that it compiles fine :)

One small unrelated point. You should try and avoid using goto's where-ever possible. They make your program less readable and makes it harder to see the 'flow'. In your program you easily just use a loop instead. AFAIK the main use of gotos is to allow exit from nested loops, where a break statement would not be sufficient.

Hope this helps and post back if anything's unclear. Pointers take a bit of getting used to.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.