HOME       >>       Programming

Pointers Pointer Question


NelsonTR4N

Are pointers optional because I don't understand them clearly. The tutorials are too not noob-friendly...So:Are pointers optional in mosty programs?Do YOU know any noob-friendly tutorials on pointers?Thanks in advance for taking your time and answering this~!


pringles08

Are pointers optional because I don't understand them clearly. The tutorials are too not noob-friendly...
So:
Are pointers optional in mosty programs?
Do YOU know any noob-friendly tutorials on pointers?


Thanks in advance for taking your time and answering this~!


I know pointers only hold the addresses of the values they are pointing to and also give the variables aliases.

wutske

Pointers aren't optional if you need to pass a huge amount of data to another function.
Normaly, if you pass a variable to another function, it'll be copied in memory. This is not ideal of you need to pass 100MB of data, your program will quickly let a computer run out of memory and eventualy it will crash.

int *my_ptr; //the pointerint my_number=0; //a numbermy_ptr = &my_number; //my_ptr now refers to my_number*my_ptr =5; //my_number now equals 5

That's the basic stuff.
There's a lot of interesting tutorials on pointers on the internet and in books, 5 minutes of searching should give you the right tutorial

Lordrach

hi,
I can summarize the pointers role in one sentence :
pointers are used to access data witch is declared in a different scope.
i will explain more. when you declare a variable in a function, this function is only available within that function's scope, so when you get out of it, the variable is destroyed automatically.
here is an example :

void ChangeValues(int a, int b) { int temp = 0; temp = a; a = b; b = temp; } int main() { int x = 5, y = 1; ChangeValues(x,y); printf("x = %d and y = %d", x,y); return 1; }
guess what ! this will print : x = 5 and y = 1 !!!
why ? because when we call the ChangeValues function, the parameters passed are not the original ones, instead, it's just a copy of them. these parameters are created in the scope of the ChangeValues function, so obviously, when this function end, these variables are destroyed.

how can we solve it ?
simply by changing the ChangeValues function definition to accept parameters by their address so it work on the original copies
here is the code :
void ChangeValues(int * a, int * b) { int temp = 0; temp = *a; *a = *b; *b = temp; } int main() { int x = 5, y = 1; ChangeValues(&x,&y); printf("x = %d and y = %d", x,y); return 1; }
now everything should be OK.

another useful tip is that functions return only one value. we can solve this by giving it variables pointers so that it can store the results directly in them.

i hope you understood the utility of pointers and that they are not optional in C as they are not difficult either.
i hope this helped.

Quatrux

yeah, this is the basic way we usually use pointers and they are not so hard as sometimes it may look, but still if you do programming and you're a newbie, those pointers usually can make you a headache and errors in your code especially if you're moving from another language which is not so low level or even from assembly where everything is usually global :mellow:Can somebody explain then, why sometimes in some codes we use pointers to pointers? what's the purpose anyway to do that? and is it reliable? like * * a = * b; and etc. even though I know how it works and in the lectures of C way back they taught us these kind of things, but I never really ever used it and in the exams they usually put this kind of things to make you a headache to write the answer about the result


xboxrulz1405241485

I think I can say pointers are easy (but dangerous if used improperly) shortcuts that points to the data directly instead of having you constantly passing things around which may get complicated when you forget what goes where. However, not using pointers for beginners in the programming world can be a good thing since they wouldn't overwrite a memory allocated for another software. (Unless DEP is in effect).xboxrulz


iGuest

When I have more time I'll talk about pointers, in 5 minutes I'm travelling for 2 hours, so won't be back for a while.Pointer to pointer points to the address of a pointer, remember pointers themselves also get a memory address. Not to be confused with the commandline argument which is a pointer to an array of pointers.OK, I must go but I will do a thread on pointers here.Cheers,MC


iGuest

OK, now to talk about pointers.

 

Pointers are extremely important in C++, especially for handling memory. Pointers provides us a way of accessing the data in that variable without using the variable's name, in other words they can represent another way of accessing memory with an address instead of a name as with variables. In fact pointers excel excel at working with memory, not just because you can store memory addresses in them, but also because you can assign those addresses at runtime and that's the secret behind dynamic memory allocation in C++.

 

Every pointer you use must have a type in C++. For example a pointer that points to an integer is an integer pointer, a pointer to a double is a double pointer and so on. You can also have a void pointer which you can cast to whatever type you need at the time, which I'll talk about later on when I get the chance.

 

As far as C++ is concerned, memory is divided into three areas, automatic storage where a function's variables are allocated and stored when you enter the function, static storage, where data that is declared static is stored and left for the duration of the program's runtime and the free store (in C it's called the "heap"). The free store is memory set aside for the use of your code at runtime, and you can allocate and handle space in the free store using the C++ new and delete operators.

 

Being able to allocated and free memory at runtime is a great asset to a program, it's probably important to note that when you're dealing with pointers you are also working with memory management. Being able to allocate space for many elements as you need at runtime dynamically, is in most cases far more efficient.

 

The new operator returns a pointer, so when you do say a new double, you need to assign a double pointer to it to be able to access that memory location.

 

Just some code to show what I mean:

 

#include <iostream>using namespace std;int main() { double *ptr = new double;...

In this example I've allocated space for a double variable named ptr with the new operator. When I'm done with the memory I would use delete operator to free it. Another thing to note, is if new is unsuccessful, it will return a NULL pointer which has a value of 0 and most of you would have seen error messages that tell you it's impossible to write/read from address 0x00000000, well this is usually the case when a programmer forgets to check whether new was successful or not (there are other reasons too). So always check if the pointer returned from new is not NULL else you might need to retry doing it or exit your program.

 

Another key thing to know about pointers is the arithmetic, the easiest thing to do is increment a pointer, and this is where a pointer is quite intelligent, a pointer will increment by the sizeof(type of pointer), so if I had an integer pointer and increment it by 1, it will increment by 4 bytes which is the size of an integer, and usually if you've named two integer variables and pointed the pointer to the first one, incrementing it should hopefully get you to the next named variable's address unless some form of protection is in place.

 

There's also similarities between pointers and arrays but that's a bit in depth for me to go into as it'd mean I'd have to explain arrays too and I'm trying to stay with pointers

 

I guess another thing to talk about with pointers is using them with structures. A pointer points to a data item in memory, but with a structure, you need more information. Say you wanted to point to a member of a structure and not just a whole structure itself. In that case, you need to specify exactly what member of the structure you want. So first of all you would point to the whole structure, then you could dereference it and use the dot operator and then the member you want or you could use the proper C++ way which doesn't require the pointer to be dereferenced, using the arrow operator. I guess another example is needed:

 

#include <iostream>#include <string>using namespace std;int main() { struct structure{ string text; } structure1; structure *ptr = &structure1; ptr->text = "Hello, World!"; // Notice no dereference asterisk (*) and the arrow operator - preferred method cout << (*ptr).text << endl; // Notice I'm using the dereference here and the dot operator - still works this way too return 0;}

As you should be able to tell, the preferred method is what you should get use to doing, however some people are use to the other method, both still work the same, so the best thing really is understanding that these two methods are exactly the same so it helps to just know these two ways otherwise you might get stumped by someone elses code (which is quite common, especially when you see their optimising techniques that makes code ugly to read just for performance).

 

OK, I still haven't finished, but there's really a whole lot more to talk about with pointers, things as pointers to functions, passing arguments to functions by references, pointers to objects and object members, creating a copy constructor, virtual methods and runtime polymorphism, pointers in class templates, dynamic_cast with pointers at runtime... well you should get the idea, pointer is very important and you should really get a good understanding of pointers as it will be something you'll need to use in large projects (small projects would be good too if you get use to them now)

 

Now most of this information is just going off by memory, I sort of stopped C++ programming a couple of years ago and just dabble in it every once in a while. I've switched over more to web environment development and tested CGI/C++ (which as far as I know, google has done a bit of that) but I'm completely happy with the web languages available and spend more time doing that than C++ these days but I'm always happy to provide what knowledge I do have of most languages I've learnt along the way, even if I rarely get the chance to visit here as much.

 

Cheers,

 

 

MC


Lordrach

yeah, this is the basic way we usually use pointers and they are not so hard as sometimes it may look, but still if you do programming and you're a newbie, those pointers usually can make you a headache and errors in your code especially if you're moving from another language which is not so low level or even from assembly where everything is usually global
Can somebody explain then, why sometimes in some codes we use pointers to pointers? what's the purpose anyway to do that? and is it reliable? like * * a = * b; and etc. even though I know how it works and in the lectures of C way back they taught us these kind of things, but I never really ever used it and in the exams they usually put this kind of things to make you a headache to write the answer about the result


a pointer to pointers is a multidimensional array . as you know we can use a pointer to declare an array like this :
int * MyArray = (*int) malloc(20 * sizeof(int)); // this is an array of 20 rows

if you want to use the same technique to declare a multidimensional array, you should use this code :
int i; int NumberOfCols = 5; int NumberOfRows = 10; int ** MyArray = (**int) malloc(NumberOfRows * sizeof(int)); // 10 rows for(i = 0; i < NumberOfRows;i++) { MyArray[i] = (*int) malloc(NumberOfCols * sizeof(int)); // each line contains 5 fields }

in the end you have 10 * 50 fields = 50;
something like this : (don't laugh )

#########################
#* #* #* #* #* #* #* #* #* #* #
#########################
#* #* #* #* #* #* #* #* #* #* #
#########################
#* #* #* #* #* #* #* #* #* #* #
#########################
#* #* #* #* #* #* #* #* #* #* #
#########################
#* #* #* #* #* #* #* #* #* #* #
#########################

( * = field )

for 3, 4, ..., n multidimensional Arrays, you use the same technique.
now i think you will not miss this kind of questions anymore .
good luck.

Quatrux

Wow, when you're into pointers a bit, the last 2 posts are really very clear to understanding them even more than before..


ashish17

I am new learner of visual C and i have searched a lot on it and found some link on it...but i want to know more about on it so that i can use it....I m hoping that some tutorials will be a great help...and a best support..thankx in advance and for ur precious time ....


surfermac

Pointers in c provide you a wide range when you are working out with data structures As pointers are not available in java and some other languages they can be ignoredbut if you are taking c and c++ seriously then they might give you a vast scope and help you a lot out in your work



VIEW DESKTOP VERSION REGISTERGET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.