6 Replies - 405 Views - Last Post: 30 July 2011 - 07:40 PM Rate Topic: -----

#1 pradigm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-July 11

I have a question about pointers

Posted 30 July 2011 - 09:34 AM

My question is what is the point of pointers. Why are pointers so useful. Why can't I write this peice of code:


#include <iostream>

using std::cout;
using std::cin;

int main() {
int x;

cout << "Enter a number: ";
cin >> x;
cout  << "\n" << "You entered: " << x;
}


Instead of this:

#include <iostream>

using std::cout;
using std::cin;

int main() {
int x;
int *p;

cout << "Enter a number: ";
cin >> x;
p = &x;
cout  << "\n" << "You entered: " << *p;
}



I honestly don't get the point of pointers. I have searched several different tutorials on c++ but they don't really tell me what the point of them is.

Is This A Good Question/Topic? 0
  • +

Replies To: I have a question about pointers

#2 ahura_24  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 69
  • Joined: 07-March 10

Re: I have a question about pointers

Posted 30 July 2011 - 09:47 AM

why pointer is usefull ?!!!!!!!!! how much know about pointer ?!!!
consider this code :

void sort(int *s, int *e, bool (*fun)(int, int))
{
	for (int *i = s; i < e - 1; i++)
		for (int *j = i + 1; j < e; j++)
			if ( fun(*i, *j) ) // function pointer
				swap(*i, *j);
}

bool fun1(int a, int B)/>
{
	return a < b;
}

bool fun2(int a, int B)/>
{
	return b < a;
}

void main()
{
	int arr[] = {1, 3, 2, 5, 8, 9};
	
	int *s = arr, *e = arr + 6;

	sort(s, e, fun1);

	for (int i = 0; i < 6; i++)
		cout<< arr[i] << "  ";
	cout << endl;

	sort(s, e, fun2);

	for (int i = 0; i < 6; i++)
		cout<< arr[i] << "  ";

	cin.get();
}



compile it and see your question !!!

this is the power of C++ and of course pointers !
Was This Post Helpful? 0
  • +
  • -

#3 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: I have a question about pointers

Posted 30 July 2011 - 10:25 AM

View Postahura_24, on 30 July 2011 - 10:47 AM, said:

why pointer is usefull ?!!!!!!!!! how much know about pointer ?!!!
consider this code :


It's actually a frequently asked question amongst people who are new to the concept of pointers.

Quote

My question is what is the point of pointers. Why are pointers so useful.


Keeping this strictly to pointers in C and C++, pointers are very useful. The most obvious advantage is simply optimization. Passing a reference/pointer to data rather than a copy of the data is often faster due to the fact that when you pass a reference, you're simply passing a 32-bit integer (address).

However, if we move away from the slightly more abstract concept of pointers that C and C++ provides, we see that pointers are used everywhere at the low level. For example, just moving data into a variable using primitive CPU instructions requires the use of a pointer, a pointer to the location of the variable, although of course the high level programmer won't need to worry about such things. If you go even lower than that, right down to the hardware level, you find that chips and circuit boards contain address buses to reference memory locations.

So, basically, pointers in languages like C and C++ are very important, but at the system level, "pointers" (the ability to reference memory) is essential to the functioning of computer systems as we know them.
Was This Post Helpful? 1
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5821
  • View blog
  • Posts: 12,674
  • Joined: 16-October 07

Re: I have a question about pointers

Posted 30 July 2011 - 10:52 AM

View Postahura_24, on 30 July 2011 - 12:47 PM, said:

this is the power of C++ and of course pointers !


The power of C++ is to tame pointers. That's more the power of C. ;)

I'm not sure you've proven your point, though. Any novice programmer would use a sig like:
void sort(int list[], int size)



Granted, list is really passed as a pointer. Even that compare function could be avoided with a struct reference implementing a base interface, which is apparently how all the cool kids are doing it these days.

To the OP: In C++, you should generally use pass by reference. However, understand that pointers are everywhere in C++, even if you don't actually recognize them. Essentially, without pointers you'd have no way to pass most objects around. Nor to dynamically allocate them. Any non trivial program needs this things.

This post has been edited by baavgai: 30 July 2011 - 10:53 AM

Was This Post Helpful? 0
  • +
  • -

#5 ahura_24  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 69
  • Joined: 07-March 10

Re: I have a question about pointers

Posted 30 July 2011 - 01:16 PM

ummm because im STL programmer :whistling: hey man dont take it hard :D my code is nice, isn't? :D
C++ inheritance from C . and more powerfull that !!! i believe C++ popular and strongest language in the word !! i really like it

This post has been edited by ahura_24: 30 July 2011 - 01:19 PM

Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: I have a question about pointers

Posted 30 July 2011 - 06:19 PM

This topic of "why pointers are useful" is very common. In fact just last week we had this topic.

here is a post I made about it all a while back:

Re: Why use pointers? What's the point? #entry1070393
Was This Post Helpful? 0
  • +
  • -

#7 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: I have a question about pointers

Posted 30 July 2011 - 07:40 PM

do we have something in the FAQ about pointers? if not we need something, we get this question at least twice a month.

some notable tutorial maybe? that topic you just posted? something to cover this

This post has been edited by ishkabible: 30 July 2011 - 07:41 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1