Pointers and me never quite got along. I've read so much about them, but I've yet to really encounter a situation where I've thought that it was quintessential to use a pointer.
I had a bit of a problem with my first job in C. When I started I would write code... upon code reviews the guys would always say "OK this looks fine, but why didn't you use a pointer here, here, and here?"
Could somebody here please attempt to explain pointers to me?
I think my problem comes from examples. It's easy to create an example that shows how a pointer works, but it's difficult to create an example where it really makes sense to use one. Towards the end of that job I was pretty proficient with using pointers (especially with pointers to character arrays to edit strings, oh C...) but it's been a while and I found myself quite rusty while trying to help a friend.
Here's what I know (remember) thus far:
& is the "address of" indicator.
That will set my pointer, j, to the address space of i. If I were to look at the literal values, i would equal 2, and j would equal 0x00FF or something like that.
AFAIK a pointer is just a value that tells your program to go to a place in memory. With that being said, I see two good uses for a pointer:
1) Passing a value to a function by reference instead of by value
2) Saving on some memory space instead of keeping duplicate values stored
When else should I use a pointer, and why?
To be honest, pointers are really the only thing that keep me away from using C++ more often. When I see them in code I can understand them, but I have a hard time writing with them and I find that they scramble my brain.
I can use pointers, and I can understand how a pointer is being used in code when reading it... but I just can't for the life of me understand *why* a pointer was used!
So please... I ask that you help me solve my pointer problem once and for all.
I had a bit of a problem with my first job in C. When I started I would write code... upon code reviews the guys would always say "OK this looks fine, but why didn't you use a pointer here, here, and here?"
Could somebody here please attempt to explain pointers to me?
I think my problem comes from examples. It's easy to create an example that shows how a pointer works, but it's difficult to create an example where it really makes sense to use one. Towards the end of that job I was pretty proficient with using pointers (especially with pointers to character arrays to edit strings, oh C...) but it's been a while and I found myself quite rusty while trying to help a friend.
Here's what I know (remember) thus far:
& is the "address of" indicator.
Code:
int i = 2;
int* j;
j = &i;
AFAIK a pointer is just a value that tells your program to go to a place in memory. With that being said, I see two good uses for a pointer:
1) Passing a value to a function by reference instead of by value
2) Saving on some memory space instead of keeping duplicate values stored
When else should I use a pointer, and why?
To be honest, pointers are really the only thing that keep me away from using C++ more often. When I see them in code I can understand them, but I have a hard time writing with them and I find that they scramble my brain.
I can use pointers, and I can understand how a pointer is being used in code when reading it... but I just can't for the life of me understand *why* a pointer was used!
So please... I ask that you help me solve my pointer problem once and for all.