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;
  • $\begingroup$ Something like the following might help at least a bit. The key is the left-right rule as discussed there. 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. $\endgroup$
    – Buffy
    Commented Aug 31, 2017 at 14:23
  • 1
    $\begingroup$ 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. $\endgroup$
    – Jay
    Commented Aug 31, 2017 at 15:06
  • 1
    $\begingroup$ This also seems to be a question about C and its usage, not a question about education. The StackOverflow forum will have better answers. $\endgroup$
    – Buffy
    Commented Aug 31, 2017 at 15:23
  • 1
    $\begingroup$ 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. $\endgroup$
    – Buffy
    Commented Aug 31, 2017 at 17:24
  • 1
    $\begingroup$ 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). $\endgroup$ Commented Sep 2, 2017 at 17:50

3 Answers 3


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.

  • $\begingroup$ 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. $\endgroup$
    – Jay
    Commented Aug 31, 2017 at 15:13
  • $\begingroup$ 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. $\endgroup$
    – Jay
    Commented Aug 31, 2017 at 15:29
  • $\begingroup$ Yes. Fixed. But still, not an appropriate question for CSEducators. $\endgroup$
    – Buffy
    Commented Aug 31, 2017 at 15:30
  • $\begingroup$ #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. ) $\endgroup$ Commented Aug 31, 2017 at 16:54
  • $\begingroup$ 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. $\endgroup$
    – Buffy
    Commented Aug 31, 2017 at 17:26

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.


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


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.

  • $\begingroup$ Whole SO community is after you. Good luck dude! stackoverflow.com/a/406819/1698143 $\endgroup$ Commented Sep 2, 2017 at 11:35
  • 1
    $\begingroup$ @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. $\endgroup$ Commented Sep 2, 2017 at 13:52
  • 1
    $\begingroup$ It is appropriate for languages with "typeless" variables. But I agree some notation convention helps the beginner: Thing my_thing, *your_thing_ptr; $\endgroup$ Commented Sep 5, 2017 at 8:43
  • $\begingroup$ @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). $\endgroup$ Commented Sep 5, 2017 at 9:26
  • $\begingroup$ @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 github.com/torvalds/linux/blob/master/include/linux/amba/bus.h $\endgroup$ Commented Sep 5, 2017 at 9:41

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.

  • $\begingroup$ I have updated my question to pinpoint the exact place where things are doing south. $\endgroup$
    – Jay
    Commented Aug 31, 2017 at 14:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.