<h2>A <strong>little</strong> Hungarian</h2>

<p>As @talhaIrfan says, the problem is mostly syntax. </p>

<p>This is what I discovered when I was struggling with pointers, while at university. Other students that I introduced the idea to would start to improve shortly after learning this technique. I have also introduced it in professional environments, where the bug rate subsequently reduced.</p>

<p>You can do a lot to improve syntax by introducing a <strong>little</strong> Hungarian notation (I do not advocate too much of this, just enough).</p>

<p>You need to be consistent, unambiguous, and clear. It should allow you to read your code more easily.</p>

<p>I always use an underscore to separate the Hungarian notation from the rest of the name. I always append type information to the end of the name (this is consistent with what the C libraries do).</p>

<p>I chose a set of symbols</p>

<ul>
<li><code>_pt</code> for pointers.</li>
<li><code>_t</code> for types, this is what the C libraries use.</li>
</ul>

<p>Examples:</p>

<pre><code>typedef struct samplestructrec *samplestruct_pt_t;
typedef struct samplestructrec  samplestruct_t;
samplestruct_t *samplestruct_pt;
</code></pre>

<h2>Summary</h2>

<p>Always choose good name for you objects, and <strong>don't do too much Hungarian</strong>. When I was programming in C++, we also had at the beginning of a name <code>p_</code> for parameters, <code>l_</code> for local, <code>m_</code> for a member variable (We did this to avoid name collisions, in retrospect I think that these 3 were not needed. We could have used <code>this.</code> instead of <code>m_</code>). <strong>That was it just 5 Hungarian pre/post fixes. You don't need any more, probably less, in a strongly typed language.</strong> </p>

CC BY-SA 3.0
2017-09-02T11:35:56.810Z
Whole SO community is after you. Good luck dude! https://stackoverflow.com/a/406819/1698143
Failed Scientist
CC BY-SA 3.0
2017-09-02T13:52:24.020Z
@TalhaIrfan added emphasis: not too much. A little goes a long way. Hungarian notation has been over used in the past, it is not evil, if used appropriately.
ctrl-alt-delor
CC BY-SA 3.0
2017-09-05T08:43:38.477Z
It is appropriate for languages with "typeless" variables. But I agree some notation convention helps the beginner:  `Thing my_thing, *your_thing_ptr;`
Michel Billaud
CC BY-SA 3.0
2017-09-05T09:26:30.700Z
@MichelBillaud I still use `_pt` and I have over 30 years experience as a programmer. Both the C library and I use `_t` for types. Though I prefer all caps (Eiffel style) for types/classes (In C all caps is for macros).
ctrl-alt-delor
CC BY-SA 3.0
2017-09-05T09:41:53.560Z
@ctr-alt-delor I'm afraid I'm a bit older. And I'm not convinced by the necessity or _t suffix in user defined data types names. Opening a random file in the linux source tree : there is none https://github.com/torvalds/linux/blob/master/include/linux/amba/bus.h
Michel Billaud
CC BY-SA 3.0
2017-09-06T14:19:42.677Z
@ctrl-alt-delor But I see this (_t suffix) is a recommendation in some coding standards for Embedded C  https://barrgroup.com/Embedded-Systems/Books/Embedded-C-Coding-Standard/Data-Type-Rules/Naming-Conventions
(note they don't seem to know about C11 and recommend C99). Standards, so many of them....
Michel Billaud
CC BY-SA 3.0
2017-08-31T16:45:20.377Z
2017-09-02T13:49:40.527Z
2017-09-02T13:49:40.527Z
ctrl-alt-delor
<p>I have been teaching Programming classes since some time and my experience is:</p>

<ol>
<li>C/C++ Pointers are a nightmare for programming students - whether they have any Java/C#, etc. experience or not.</li>
<li>It's the syntax of the pointers (the terrifying*) which is more difficult for students than its semantics.</li>
<li>Every student of Java/C#, etc. knows that variables are stored in the RAM (and they have studied in Intro to Computing about RAM addresses etc. basics)</li>
</ol>

<p>It's not too much difficult to make non-C/C++ programmers understand about pointers if we take the above points and start from some basic examples like URLs being used to point to web pages etc. It has worked for my case (a great deal), hopefully it will help you and others too.</p>

CC BY-SA 3.0
2017-08-31T14:22:56.093Z
I have updated my question to pinpoint the exact place where things are doing south.
Jay
CC BY-SA 3.0
2017-08-31T14:16:05.377Z
2017-08-31T14:16:05.377Z
Failed Scientist
<p>I'm a bit confused, though, like you, it has been a while since I worked in C. </p>

<p>Confusion (a). Are you putting <em>both</em> those lines in the same header file? They seem to be giving incompatible definitions to samplestructrec.</p>

<p>Confusion (b). I wonder if you are misdiagnosing the problem. You can treat #include just about as you would treat textual inclusion. Is there something in one of the C files that makes it incompatible with the header? </p>

<p>Here is quite a bit on headers. 
<a href="https://www.tutorialspoint.com/cprogramming/c_header_files.htm" rel="nofollow noreferrer">https://www.tutorialspoint.com/cprogramming/c_header_files.htm</a></p>

<hr>

<p>Suppose you say:  </p>



<pre><code>typedef struct samplestructrec *samplestruct;
</code></pre>

<p>Then later (in a C file) you say</p>

<pre><code>samplestructrec foo
</code></pre>

<p>Then <code>foo</code> is a pointer but there is no actual samplestruct yet. You need to use malloc or some equivalent to get foo to point to anything. </p>

<p>On the other hand, if you say  </p>

<pre><code>typedef struct samplestructrec samplestruct;
</code></pre>

<p>and then later say  </p>

<pre><code>samplestructrec boo  
</code></pre>

<p>Then <code>boo</code> is an actual struct (a block of bits on the stack). </p>

<p>typedef just names a type. It creates information in the compiler, not in the runtime. In particular, samplestructrec is just a name, not a thing. </p>

CC BY-SA 3.0
2017-08-31T15:13:36.733Z
Yeah, thats the link I spent time reading as well, before posting the question. I am confused if I should use the one with the * or without the *, not using them together. and what do they signify in this context, the one with the * and the one without.
Jay
CC BY-SA 3.0
2017-08-31T15:29:23.307Z
You mean "On the other hand, if you say typedef struct samplestructrec samplestruct;" right? without the *. Assuming that is the case, that explanation solves my problem.
Jay
CC BY-SA 3.0
2017-08-31T15:30:55.390Z
Yes. Fixed. But still, not an appropriate question for CSEducators.
Buffy
CC BY-SA 3.0
2017-08-31T16:54:25.813Z
`#include` is not, “just about as you would treat textual inclusion”, it is textual inclusion. It is an instruction to the C pre-processor. It tells it to replace the `#include` line with the content of the file. (There are some other `__LINE__` and `__FILE__` stuff included,  but this is just to reset some values used by the code that outputs the debug info. You may see this if you look at the output of `cpp` the C pre-processor. )
ctrl-alt-delor
CC BY-SA 3.0
2017-08-31T17:26:34.250Z
OK, I yield on that. It's been a while since I used C and haven't kept up on advances in processing it, so I was being cautious in my statement. But textual inclusion is in my memory bank.
Buffy
CC BY-SA 3.0
2017-08-31T15:10:46.963Z
2017-08-31T16:44:46.423Z
2017-08-31T16:44:46.423Z
Buffy
<p>I have already looked at <a href="https://cseducators.stackexchange.com/questions/299/simple-pointer-examples-in-c">this</a> and <a href="https://cseducators.stackexchange.com/questions/2691/is-it-important-to-teach-pointers-in-a-first-course-using-java?rq=1">this</a>, but it does not help me at all. </p>

<p>I am teaching a guy who is something of a Java expert. Unfortunately, due to the mandatory-ness of the university curriculum, he is forced to downgrade himself to learning C. ( I use the word downgrade in a strictly technical sense, no offence meant to people who are fans of C ). The thing is, I am able to get him through the whole C syllabus except for pointers. </p>

<p>More importantly, we want to use pointers inside structs, which are actually declared in a header file. I am teaching him to build sharable  c libraries/header files. If you need more details, let me know in the comments, and I can share the code I am using in a repo and so on. </p>

<p>Update 1 - I am also looking at <a href="https://stackoverflow.com/questions/14699538/declare-struct-in-header-file">this</a>, but its not very clear to me. I haven't used C in a decade, so all this is a little blurry for me.</p>

<p>Update 2 - getting even more specific, what is really happening in these two lines, that I am putting in the header file. I am defining the samplestruct in the corresponding .c file.</p>

<pre><code>typedef struct samplestructrec *samplestruct;
typedef struct samplestructrec samplestruct;
</code></pre>

CC BY-SA 3.0
2017-08-31T14:23:30.480Z
Something like the following might help at least a bit. The key is the left-right rule as discussed there. http://unixwiz.net/techtips/reading-cdecl.html. I might try to discuss the C data types as a way to represent Java references, and the structs as a way to build objects (with embedded references). Not a perfect analogy, but maybe a place to start.
Buffy
CC BY-SA 3.0
2017-08-31T14:25:53.360Z
I have faced no challenge in using or explaining pointers when they are used within a C file. It is only when I decided to do 'separate files' with the .h and .c, I ran into issues. You will find a sample code in the question, which I just updated.
Jay
CC BY-SA 3.0
2017-08-31T14:36:24.850Z
Pointers should behave exactly the same whether you're using them in a header file or not. In any case, after your edits, it seems like your actual question is more of a technical one -- I would try asking it on StackOverflow rather then here.
Michael0x2a
CC BY-SA 3.0
2017-08-31T14:48:45.537Z
@Michael0x2a Agree!
Failed Scientist
CC BY-SA 3.0
2017-08-31T15:06:22.047Z
Thats all very fair folks. I was hoping if someone could point me to a Pointer + header Files + Struct resource online, or explain how it works in a post....that would have really helped me...like how things have been explained in the 'Pointer Examples in C' post.
Jay
CC BY-SA 3.0
2017-08-31T15:23:54.483Z
This also seems to be a question about C and its usage, not a question about education. The StackOverflow forum will have better answers.
Buffy
CC BY-SA 3.0
2017-08-31T16:56:25.757Z
@Buffy I thought this question, was asking for help on teaching pointers in C: What can I do to make it easier for my student to learn pointer? If so then it should be on topic. Am I wrong?
ctrl-alt-delor
CC BY-SA 3.0
2017-08-31T17:24:49.570Z
Just because a question arises in a teaching context doesn't make it about education. Had it started out "We are building the next Facebook replacement and are running into a header file problem..." and then continued at the 3rd paragraph. The question would be the same. It isn't the context that makes it about education, though the context is needed. It was a specific C usage question.
Buffy
CC BY-SA 3.0
2017-09-01T03:06:37.127Z
The JVM is implemented in C. C pointers are a key part of the Java reference mechanics. You might start from there.
pojo-guy
CC BY-SA 3.0
2017-09-02T17:50:42.633Z
Using "typedef" to hide real definitions provides a way to give some opaqueness (example : FILE) to user defined data types.  But It makes more harm than good when you're trying to explain pointers to beginners. BTW you used the same name in both typedefs. Better start with functions over some structs (without pointers, like coordinates or dates with day/month/year) and explain pointers for parameter passing of structs. Keep dynamic allocation and chaining for a later stage (topic won't be unfamiliar to your java student).
Michel Billaud
<undergraduate><c>
Teaching pointers (C) to a Java guy
# Teaching pointers (C) to a Java guy

(Asked by: username_0 on 2017-08-31)

I have already looked at this and this, but it does not help me at all. 
I am teaching a guy who is something of a Java expert. Unfortunately, due to the mandatory-ness of the university curriculum, he is forced to downgrade himself to learning C. ( I use the word downgrade in a strictly technical sense, no offence meant to people who are fans of C ). The thing is, I am able to get him through the whole C syllabus except for pointers. 
More importantly, we want to use pointers inside structs, which are actually declared in a header file. I am teaching him to build sharable  c libraries/header files. If you need more details, let me know in the comments, and I can share the code I am using in a repo and so on. 
Update 1 - I am also looking at this, but its not very clear to me. I haven't used C in a decade, so all this is a little blurry for me.
Update 2 - getting even more specific, what is really happening in these two lines, that I am putting in the header file. I am defining the samplestruct in the corresponding .c file.
typedef struct samplestructrec *samplestruct;
typedef struct samplestructrec samplestruct;

**Tags:** undergraduate, c

> **Comments:**
> **username_1**: Something like the following might help at least a bit. The key is the left-right rule as discussed there. http://unixwiz.net/techtips/reading-cdecl.html. I might try to discuss the C data types as a way to represent Java references, and the structs as a way to build objects (with embedded references). Not a perfect analogy, but maybe a place to start.  
> **username_0**: I have faced no challenge in using or explaining pointers when they are used within a C file. It is only when I decided to do 'separate files' with the .h and .c, I ran into issues. You will find a sample code in the question, which I just updated.  
> **username_2**: Pointers should behave exactly the same whether you're using them in a header file or not. In any case, after your edits, it seems like your actual question is more of a technical one -- I would try asking it on StackOverflow rather then here.  
> **username_3**: @username_2 Agree!  
> **username_0**: Thats all very fair folks. I was hoping if someone could point me to a Pointer + header Files + Struct resource online, or explain how it works in a post....that would have really helped me...like how things have been explained in the 'Pointer Examples in C' post.  
> **username_1**: This also seems to be a question about C and its usage, not a question about education. The StackOverflow forum will have better answers.  
> **username_4**: @username_1 I thought this question, was asking for help on teaching pointers in C: What can I do to make it easier for my student to learn pointer? If so then it should be on topic. Am I wrong?  
> **username_1**: Just because a question arises in a teaching context doesn't make it about education. Had it started out "We are building the next Facebook replacement and are running into a header file problem..." and then continued at the 3rd paragraph. The question would be the same. It isn't the context that makes it about education, though the context is needed. It was a specific C usage question.  
> **username_5**: The JVM is implemented in C. C pointers are a key part of the Java reference mechanics. You might start from there.  
> **username_6**: Using "typedef" to hide real definitions provides a way to give some opaqueness (example : FILE) to user defined data types.  But It makes more harm than good when you're trying to explain pointers to beginners. BTW you used the same name in both typedefs. Better start with functions over some structs (without pointers, like coordinates or dates with day/month/year) and explain pointers for parameter passing of structs. Keep dynamic allocation and chaining for a later stage (topic won't be unfamiliar to your java student).  

---

## 3 answers

### Answer by username_1 on 2017-08-31. Score: 1 | Accepted answer

I'm a bit confused, though, like you, it has been a while since I worked in C. 
Confusion (a). Are you putting both those lines in the same header file? They seem to be giving incompatible definitions to samplestructrec.
Confusion (b). I wonder if you are misdiagnosing the problem. You can treat #include just about as you would treat textual inclusion. Is there something in one of the C files that makes it incompatible with the header? 
Here is quite a bit on headers. 
https://www.tutorialspoint.com/cprogramming/c_header_files.htm

Suppose you say:  
typedef struct samplestructrec *samplestruct;

Then later (in a C file) you say
samplestructrec foo

Then foo is a pointer but there is no actual samplestruct yet. You need to use malloc or some equivalent to get foo to point to anything. 
On the other hand, if you say  
typedef struct samplestructrec samplestruct;

and then later say  
samplestructrec boo  

Then boo is an actual struct (a block of bits on the stack). 
typedef just names a type. It creates information in the compiler, not in the runtime. In particular, samplestructrec is just a name, not a thing.

> **Comments:**
> **username_0**: Yeah, thats the link I spent time reading as well, before posting the question. I am confused if I should use the one with the * or without the *, not using them together. and what do they signify in this context, the one with the * and the one without.  
> **username_0**: You mean "On the other hand, if you say typedef struct samplestructrec samplestruct;" right? without the *. Assuming that is the case, that explanation solves my problem.  
> **username_1**: Yes. Fixed. But still, not an appropriate question for CSEducators.  
> **username_4**: `#include` is not, “just about as you would treat textual inclusion”, it is textual inclusion. It is an instruction to the C pre-processor. It tells it to replace the `#include` line with the content of the file. (There are some other `__LINE__` and `__FILE__` stuff included,  but this is just to reset some values used by the code that outputs the debug info. You may see this if you look at the output of `cpp` the C pre-processor. )  
> **username_1**: OK, I yield on that. It's been a while since I used C and haven't kept up on advances in processing it, so I was being cautious in my statement. But textual inclusion is in my memory bank.  

---

### Answer by username_4 on 2017-08-31. Score: 2

A little Hungarian
As @talhaIrfan says, the problem is mostly syntax. 
This is what I discovered when I was struggling with pointers, while at university. Other students that I introduced the idea to would start to improve shortly after learning this technique. I have also introduced it in professional environments, where the bug rate subsequently reduced.
You can do a lot to improve syntax by introducing a little Hungarian notation (I do not advocate too much of this, just enough).
You need to be consistent, unambiguous, and clear. It should allow you to read your code more easily.
I always use an underscore to separate the Hungarian notation from the rest of the name. I always append type information to the end of the name (this is consistent with what the C libraries do).
I chose a set of symbols

_pt for pointers.
_t for types, this is what the C libraries use.

Examples:
typedef struct samplestructrec *samplestruct_pt_t;
typedef struct samplestructrec  samplestruct_t;
samplestruct_t *samplestruct_pt;

Summary
Always choose good name for you objects, and don't do too much Hungarian. When I was programming in C++, we also had at the beginning of a name p_ for parameters, l_ for local, m_ for a member variable (We did this to avoid name collisions, in retrospect I think that these 3 were not needed. We could have used this. instead of m_). That was it just 5 Hungarian pre/post fixes. You don't need any more, probably less, in a strongly typed language.

> **Comments:**
> **username_3**: Whole SO community is after you. Good luck dude! https://stackoverflow.com/a/406819/1698143  
> **username_4**: @TalhaIrfan added emphasis: not too much. A little goes a long way. Hungarian notation has been over used in the past, it is not evil, if used appropriately.  
> **username_6**: It is appropriate for languages with "typeless" variables. But I agree some notation convention helps the beginner:  `Thing my_thing, *your_thing_ptr;`  
> **username_4**: @MichelBillaud I still use `_pt` and I have over 30 years experience as a programmer. Both the C library and I use `_t` for types. Though I prefer all caps (Eiffel style) for types/classes (In C all caps is for macros).  
> **username_6**: @ctr-alt-delor I'm afraid I'm a bit older. And I'm not convinced by the necessity or _t suffix in user defined data types names. Opening a random file in the linux source tree : there is none https://github.com/torvalds/linux/blob/master/include/linux/amba/bus.h  
> **username_6**: @username_4 But I see this (_t suffix) is a recommendation in some coding standards for Embedded C  https://barrgroup.com/Embedded-Systems/Books/Embedded-C-Coding-Standard/Data-Type-Rules/Naming-Conventions (note they don't seem to know about C11 and recommend C99). Standards, so many of them....  

---

### Answer by username_3 on 2017-08-31. Score: 1

I have been teaching Programming classes since some time and my experience is:

1. C/C++ Pointers are a nightmare for programming students - whether they have any Java/C#, etc. experience or not.
2. It's the syntax of the pointers (the terrifying\*) which is more difficult for students than its semantics.
3. Every student of Java/C#, etc. knows that variables are stored in the RAM (and they have studied in Intro to Computing about RAM addresses etc. basics)

It's not too much difficult to make non-C/C++ programmers understand about pointers if we take the above points and start from some basic examples like URLs being used to point to web pages etc. It has worked for my case (a great deal), hopefully it will help you and others too.

> **Comments:**
> **username_0**: I have updated my question to pinpoint the exact place where things are doing south.  

---

