Pointers are confusing!

help with understanding

Page 1 of 1

7 Replies - 1761 Views - Last Post: 27 June 2009 - 02:35 AM Rate Topic: -----

#1 mono15591  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 406
  • Joined: 05-November 08

Pointers are confusing!

Posted 25 June 2009 - 08:09 PM

Ok well I'm making tic-tac-toe and I have an array of spaces you can choose and I want to change the things inside the array after the user determines where he/she wants to place his/her x/o I know I need to request dynamic memory

I'm looking at an example of creating a pointer and requesting memory but I don't understand it
I know a pointer points to a certain block in memory but when I'm writing it down and seeing it written down it just confuses me.

This is the example I'm looking at.
#include <iostream>
#include <new>
using namespace std;

int main ()
{
  int i,n;
  int * p;
  cout << "How many numbers would you like to type? ";
  cin >> i;
  p= new (nothrow) int[i];
  if (p == 0)
	cout << "Error: memory could not be allocated";
  else
  {
	for (n=0; n<i; n++)
	{
	  cout << "Enter number: ";
	  cin >> p[n];
	}
	cout << "You have entered: ";
	for (n=0; n<i; n++)
	  cout << p[n] << ", ";
	delete[] p;
  }
  return 0;
}



Basically I'm just asking for someone to explain how this works even more because the way I'm reading it on other sites doesn't make any sense to me

Is This A Good Question/Topic? 0
  • +

Replies To: Pointers are confusing!

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Pointers are confusing!

Posted 25 June 2009 - 08:29 PM

Quote

Ok well I'm making tic-tac-toe and I have an array of spaces you can choose and I want to change the things inside the array after the user determines where he/she wants to place his/her x/o I know I need to request dynamic memory
I fail to understand. Just allocate an array of size 9, and assign to an element in the array as necessary. You don't need to dynamically allocate memory to change its value. After all, you are changing the values of variables all the time, as in cin >> i;. Is i dynamically allocated? No, because there's no need. You have a single variable and you assign to that as needed.

Dynamic allocation of memory makes sense when you don't know the memory requirements until compile time. For example, you need to have memory for a set of numbers, but you don't know how many numbers until runtime. Then you dynamically allocate as needed.

People trip on two aspects of pointers. The first is the concept. Are you having trouble understanding what a pointer itself is? Forget the syntax, and how you make one in code. Do you get the concept of a pointer, which is to refer to some other variable?

Then there's the syntax. You just need to learn the (not so intuitive) rules in C++ regarding pointers and also arrays. It starts to make sense when you think through the logic.

If you are having trouble with the concept, indicating so, and that can be dealt with first.
Was This Post Helpful? 0
  • +
  • -

#3 mono15591  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 406
  • Joined: 05-November 08

Re: Pointers are confusing!

Posted 25 June 2009 - 08:43 PM

Well I think I understand the concept
it's easier to point to the variables' value than to actually go and get it
but I don't really know what's confusing me so much I guess that the syntax confuses me the most I don't really know why and what every thing that is written does when actually using it with other things. I don't know how this seems to be the hardest thing I'm ever gonna have to learn in C++ lol
Was This Post Helpful? 0
  • +
  • -

#4 mono15591  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 406
  • Joined: 05-November 08

Re: Pointers are confusing!

Posted 25 June 2009 - 08:51 PM

View Postmono15591, on 25 Jun, 2009 - 07:43 PM, said:

Well I think I understand the concept
it's easier to point to the variables' value than to actually go and get it
but I don't really know what's confusing me so much I guess that the syntax confuses me the most I don't really know why and what every thing that is written does when actually using it with other things. I don't know how this seems to be the hardest thing I'm ever gonna have to learn in C++ lol


I also don't understand why you have to use a pointer when you use a variable to choose an element in an array

int x;
int f[5];

cin>>x;
cout<<f[x];


Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Pointers are confusing!

Posted 25 June 2009 - 10:07 PM

Quote

Well I think I understand the concept
it's easier to point to the variables' value than to actually go and get it
I can see why you think of pointers that way, especially since certain (bad) explanations can lead to that kind of conclusion. That's not really it. Pointers are C's (and C++'s) way of implementing referential semantics. Meaning instead of having a variable you can point to one. That's it. The real question then is why anyone cares about pointing versus actually having a variable.

If you can make a detour in your learning experience, try learning a bit about a fundamental data structure called a linked list (Wikipedia entry on linked list). Think about a linked list's ability to link to another node in the list, and you should start to see how pointers (or any language's reference semantics) are useful.

What's really confusing about syntax isn't pointers themselves. The rule of a pointer is if you have a variable of type t. Thus t identifier, so if t meant int, you would have something like int x or if t was char, you would have char somethingElse. Then a pointer to that that type is t* pointerIdentifier.

So int x; int *y = &x; (the & gets the address of x). Or int* x; int **y = &x;. What is the type of x in that example? int*. So if you need to point to it, t*, so int**. Get it?

(Technically I fudged a bit by the way, so if you have int* x, y; x is a pointer to an integer, y is only an integer. The * applies to the variable, not the type. Keep this in the back of your mind somewhere).

Anyway, the rule for pointers is simple. So where does it get confusing? Usually with arrays, which actually are a bit confusing until you unravel the underlying rules in C and C++. I won't cover it, as there's a site better suited to it, and you don't need to deal with the issue anyway at the moment.

Quote

I also don't understand why you have to use a pointer when you use a variable to choose an element in an array
You don't. You didn't use a pointer in your code snippet.
Was This Post Helpful? 0
  • +
  • -

#6 kidicarus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 139
  • Joined: 13-February 09

Re: Pointers are confusing!

Posted 26 June 2009 - 05:02 PM

Aren't pointers the only way to access dynamically allocated memory? So, if you didn't know the size of the array before you ran the program, you could set the size while it is running:

int *x = new int[y]; //y is a value obtained after run time.

This seems to be an important concept in C++, although I think your explanation of them is very good Oler1s.

Do other languages like Fortran, COBOL, etc. allow you to access the memory in such a way?
Was This Post Helpful? 0
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Pointers are confusing!

Posted 26 June 2009 - 07:29 PM

Quote

Aren't pointers the only way to access dynamically allocated memory?
In the sense when you work with dynamically allocated memory in C++, at its most basic level of abstraction, then you must use pointers.

I make this distinction because if you use something like a vector, then accesses to the vector eventually translate to dynamically allocated memory, but this is abstracted from you.

Quote

Do other languages like Fortran, COBOL, etc. allow you to access the memory in such a way?
Well, mainstream languages have some mechanism for dynamic allocation, the real question is what kind of abstraction is used to present it to you. And many don't present it in the way C and C++ do with the pointer concept (nor should they have to). In fact, C99 has something that C++ does not. You can in fact, have variable length arrays. With higher level languages, it's abstracted out even more.
Was This Post Helpful? 1
  • +
  • -

#8 Cha0sBG  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 167
  • Joined: 09-April 09

Re: Pointers are confusing!

Posted 27 June 2009 - 02:35 AM

Hello Mono15591,
Here is a video tutorial from the MSDN how do I? video tutorials. It's explained in a easy way and i really think u will understand pointers a lot better.


http://msdn.microsof...c/cc895215.aspx

~Cha0sBG
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1