https://www.explainxkcd.com/wiki/index.php/138:_Pointers |
Hi Mom,
I have been reading Programming in C by Stephen Kochan, as I continue my computer studies.
Remember, this blog is often dedicated to my study. Like I wrote about here:
http://sensedoubt.blogspot.com/2017/07/hey-mom-talking-to-my-mother-729-not-my.html
"Not my teaching but my study" from a quote by Montaigne via Warren Ellis.
So, I have been reviewing pointers. Kochan devotes a huge chapter to pointers, but he fails to describe the double-star pointer (which is simply a pointer to another pointer). That's no so hard, but it keeps eluding me in other reading.
Mainly, I am reviewing pointers because I started working on a stack implementation with in the C programming language with a linked list, and I am having errors. I want to be able to claim that I am fluent with C in my current job search, so I have to master this issue.
One thing that took me some time to wrap my head around is that &i sets something to point to something else as in
k = &i;
I know this means take the memory address of the variable of i and store it in k, but the better way to think of it is to "make k point to i."
The following list helps understand pointers pretty simply.
int i = 10; //i is an int, it has allocated storage to store an int.
int *k; // k is an uninitialized pointer to an int.
//It does not store an int, but a pointer to one.
k = &i; // make k point to i. We take the address of i and store it in k
int j = *k; //here we dereference the k pointer to get at the int value it points
//to. As it points to i, *k will get the value 10 and store it in j
Likewise, the star gets at the value. I knew this, also, but in practice with structs (data structures), it can be confusing when I am defining a pointer to a structure and so the type of the struct is pointer, hence something likedata_stack_t *stack
This is a pointer called stack (*stack makes it a pointer) of type data_stack_t.
And so I started reading more about pointers to make sure I fully understand them and that I can complete my program by fixing my errors. Pointers are a concept in C that many people struggle to grasp, and so in my searching for extra information, I cam across this Stack Overflow page that addresses (heh) this issue of pointers, especially double star pointers, like **i.
I love STACK OVERFLOW, and I am not alone in this love.
I decided to share (with due credit to all contributors) the Stack Overflow page here but just a select few of the many responses and discussion that resulted. I had to cut some of the explanations as it added too much meta-data to this post.
Expect a lot more of these posts, Mom. Yes, a limited audience, but it's my study not my teaching.
https://stackoverflow.com/
How to understand the pointer star * in C?
I'm struggling with the pointer sign *, I find it very confusing in how it's used in both declarations and expressions.
For example:
int *i; // i is a pointer to an int
But what is the logic behind the syntax? What does the * just before the i mean? Let's take the following example. Please correct me where I'm wrong:
char **s;
char *(*s); // added parentheses to highlight precedence
And this is where I lose track. The *s between the parantheses means: s is a pointer? But a pointer to what? And what does the * outside the parentheses mean: a pointer to what s is pointing?
So the meaning of this is: The pointer pointing to what s is pointing is a pointer to a char?
I'm at a loss. Is the * sign interpreted differently in declarations and expressions? If so, how is it interpreted differently? Where am I going wrong?
msdn.microsoft.com/en-us/library/1x82y1z4(v=vs.71).aspx – Mitch Wheat Mar 30 '11 at 9:43
| |||
Check out my lecture notes (from my Advanced C++ class at Brooklyn College)on pointers. Lectures 15 & 16dealt with pointers. – Moshe May 31 '12 at 0:16
|
Take it this way:
int *i
means the value to which i points is an integer.char **p
means that p is a pointer which is itself a pointer to a char.
answered Mar 30 '11 at 10:06
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Reflect and connect.
Have someone give you a kiss, and tell you that I love you.
I miss you so very much, Mom.
Talk to you tomorrow, Mom.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Days ago = 846 days ago
- Bloggery committed by chris tower - 1710.28 - 10:10
NEW (written 1708.27) NOTE on time: I am now in the same time zone as Google! So, when I post at 10:10 a.m. PDT to coincide with the time of your death, Mom, I am now actually posting late, so it's really 1:10 p.m. EDT. But I will continue to use the time stamp of 10:10 a.m. to remember the time of your death, Mom. I know this only matters to me, and to you, Mom.