+ 51

I really don't understanding pointers, so I need someone to help me.

2nd Feb 2017, 6:54 AM
Livingstone Chengo
Livingstone Chengo - avatar
39 Respuestas
+ 140
Every variable you have needs to be stored somewhere in memory. int x = 5; /* the 5 is now stored somewhere */ To find out where you can use &x. This gives you the location of x in memory (= the pointer to x). Let's make a new pointer and let it point to x. As x stores an integer value we'll need a pointer that points to an integer: int *p; /* this is the pointer p */ p = &x; /* p now points at x */ To access the value of x with p we can use *p: *p = 10; /* now x is 10. it's always good to cout all the values if you're not sure how they've changed */ Now arrays are technically also pointers. int a[10]; /* a is a pointer that points to the first element of the array */ To get the first element of an array you normaly write a[0], but as a is also a pointer you can use *a for the same result. For the second element you'd normaly use a[1]. But as all the elements of an array are stored at the same location in memory one after the other you can just increase the pointer by 1 (= let it point to the next space in memory) and you'll get the second element of the array again: *(a+1) = 6; /* sets the value of a[1] to 6 */ In general a[n] is the same as *(a+n) and (a+n) is the same as &a[n] (location of an element in the array a). Of corse pointers are also just values (they store a number, you can cout them and you'll see to what memory address they point), so you can also create a pointer to a pointer: int x = 5; int *p = &x; int **pp = &p; You can even access x with pp via **pp. Pointers are often used in function if you want to return more than one value. You give the function a pointer to a value and then the function can change the original value: EDIT: Looks like the comment is too long and the rest got cut off so I'll post it in a new reply!
2nd Feb 2017, 8:36 AM
Robobrine
Robobrine - avatar
+ 49
What didn't fit into my first reply: Pointers are often used in function if you want to return more than one value. You give the function a pointer to a value and then the function can change the original value: void func(int *x){ *x *= 2; /* don't forget x is a pointer, to access its value you need *x! */ } int main(){ int x = 8; func(&x); /* func needs a pointer to an integer so we can't just use func(x);, instead we give it the pointer to x */ cout << x << endl; /* even though func doesn't return anything and we didn't change x in main it will still output 16 now */ return 0; }
2nd Feb 2017, 8:37 AM
Robobrine
Robobrine - avatar
+ 15
// declaring different types of pointers int* p; char* q; double* r; // using pointer to point to the address of a variable, let's say, variable x. p = &x; // utilising the pointer cout << *p; // I know these will not be enough, so it would be good if you can query in specific what you don't understand about pointers.
2nd Feb 2017, 7:10 AM
Hatsy Rei
Hatsy Rei - avatar
+ 14
Using pointers in programming is like juggling soap bars in prison, it's fun until you make a mistake...
7th Feb 2017, 6:26 PM
Shahar Levy
Shahar Levy - avatar
+ 12
basically, if I have variable and I want another variable have the same value of that variable and any one of them can change the value and the other can see this change. so in this case we need the pointer. for example: int x= 10; int *p=&x; in this case x and p have the same value, and any change can make x or p .it will effect on both. the both share the same value. if I make x=5; now also p will be 5 too hopefully what I said is right. thanks
3rd Feb 2017, 9:47 AM
whbe
whbe - avatar
+ 10
let's understand it with real world example.. suppose I want to access you to invite you for my birthday party then I have 2 options 1) I can directly invite you for that I can simply use your name LivingStone = "you are invited"; this is equivalent to accessing a variable by its name,. ex: x = 10; 2) I can invite you at your address for that first I have to get your address on a postcard(pointer) string *yourAddress; //making a postcard, using * to know that it's a postcard. yourAddress = "address of LivingStone"; //writing your address on the postcard then I can invite you as *yourAddress = "you are invited";. // here * means traveling to your address and then inviting you, it is nothing to do with the * used in declaration This is equivalent to accessing a variable through its address, ex: int *p; //use * to declaration a pointer p = &x;. // first store the address of x in p *p = 10; // assign 10 to x using it's address which is already stored in p.
2nd Feb 2017, 2:19 PM
POJO
POJO - avatar
+ 9
Pointers are just like a reference variable who refer to the address of variable,object, arrays and array of character (string)...
9th Feb 2017, 10:04 AM
{ sood(Hemant , Prakash); }
{ sood(Hemant , Prakash); } - avatar
+ 9
I am starting my answer with the Concept of RAM. as we all know that RAM consist of memory cell and each cell having a size of 1 byte also each cell having a address(hexadecimal format say 0x1555). When a declare a variable say int x it acquire a 2 byte (it means it reserve 2 memory cell in ram in consecutive manner) now let's us suppose we assigning a value to variable x =2; the value will store in location 0x1555. In general programming we r accessing this location using variable(actually variable is nothing just a name of particular memory cells in RAM) for easy understanding. Which is indirect way of accessing the memory . In pointer we are accessing value which is in RAM directly manner that's why pointer is so fast and it has both advantage and disadvantage. Now When we declare a variable as a pointer variable it store Address of some particular variable(memory location )
9th Feb 2017, 11:24 AM
Krishna Aaraf
Krishna Aaraf - avatar
+ 8
a pointer point to the address of variable it contains the address of variable not a value of variable
3rd Feb 2017, 10:27 AM
L Boudhar
L Boudhar - avatar
+ 8
They point to a certain address
8th Feb 2017, 5:57 AM
Jeroen
Jeroen - avatar
+ 7
Pointer is just a storage area which stores the address of a variable.It tells us that the variable is stored at x location in the memory.
6th Feb 2017, 4:44 PM
decoder
decoder - avatar
+ 7
hello, in simple terms pointers points to some memory location.So what is a memory location? whenever we declare a variable, some space is allocated in the memory. Each allocated space is associated with an address(represented in hexadecimal, e.g 0xaaff1d). whenever we assign a value to a variable , a value is associated with that address. pointer is a variable that holds the address of another variable.example, int a = 10; //lets assume that addrs of variable a is 0xAD12E6 //value of variable a is 10 int *p = &a; //variable p is a pointer to an integer //lets assume that addrs of variable p is 0x11AA44 //However value of p is the address of variable a, that is 0xAD12E6 There are two types of operator : & - address of operator...it returns the address of a variable as in the example above. * - value at address operator let us print the value of variable a using pointer p c++ code : cout<< *p; //outputs 10 pointer variable p had the address of variable a using '*' operator returns the value at that address cout<< *(&a); //outputs 10
8th Feb 2017, 5:25 AM
Rupsena Purkayastha
Rupsena Purkayastha - avatar
+ 6
A pointer simply is just a variable that holds the location of where another hunk of data is located in memory. Imagine a person named John Doe, who has a girlfriend named Jane Doe. He needs to get to his girlfriends house for movie night. How does he do this? He reaches into his back pocket and pulls out a piece of paper with her address on it and then drives to her house. So John Doe is the pointer variable. Jane Doe is the data that John's address book is pointing to. John could not get to Jane's house with the ADDRESS. Same thing applies to programming. The computer is just storing the ADDRESS or LOCATION in memory, of where another piece of data is located in memory. Get my point? (pun intended)
3rd Feb 2017, 12:39 PM
Calvin Wilkinson
Calvin Wilkinson - avatar
+ 6
Wow nice one! I also get confused on this pointer issue. But i think i understand now. Thanks to all the contributors
7th Feb 2017, 3:44 PM
Timmy
Timmy - avatar
+ 5
a pointer has an address of a variable for example int x=7 if x is located at address memory of 0x4567 *p contain 0x4567 not 7
2nd Feb 2017, 5:29 PM
L Boudhar
L Boudhar - avatar
+ 5
mostly its a relic from the legacy era
7th Feb 2017, 7:43 PM
anwer
anwer - avatar
+ 5
pointer is variable itself. it stores the address of another variable in the memory. example : *p= &x; it read as : a pointer p has address of x
9th Feb 2017, 8:35 AM
Shravan K Malviya
Shravan K Malviya - avatar
+ 5
*p Is a pointer ** Is a pointer to a pointer ***p is a pointer to a pointer to a pointer *&p is a pointer to an address The best way to understand pointers is to play with them. Reading about them also helps.
18th May 2017, 4:50 AM
jay
jay - avatar
+ 4
Can someone also tell me why we need pointers?
2nd Feb 2017, 7:48 PM
Justin Charis Chandra
Justin Charis Chandra - avatar
+ 4
pointer is simply used to store address of other variable.. for example, if we r designing a linked list..we need to create links ..these links r made by pointers hope u got it :)
3rd Feb 2017, 1:00 PM
Siddiqui Shazia
Siddiqui Shazia - avatar