Expert C Programming: Deep C Secrets
All
Stack Overflow 23
This Year
Stack Overflow 2
This Month
Stack Overflow 5
Entertaining and informative. Highly recommended.
You might find "expert c programming" a good read - unpacking this kind of thing is in one of the chapters, if I remember right. It's a long time since I read it, but I remember thinking it was worth the effort at the time. http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp/0131774298
As stated above I don't think the order is important, but this is the order I wished someone would have showed me the stuff.
For more info on point 4 I really recommend chapter 4 "The Shocking truth: C arrays and Pointers Are NOT the Same!" in "Expert C, deep C secrets".
/Johan
Update:
Some links to the book, and there is also a preview of the book. http://books.google.se - Expert C, deep C secrets
And the user comments about this book is true: http://www.amazon.co.uk/Expert-Programming-Peter-van-Linden/dp/0131774298
(For C) Expert C Programming: Deep C secrets without a doubt.
About how to use pointers and the difference between array and pointer, I recommend you read the "expert c programming" (http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp/0131774298/ref=sr_1_1?ie=UTF8&qid=1371439251&sr=8-1&keywords=expert+c+programming).
The best source for learning the complexities of C is the book Expert C Programming by Peter van der Linden (http://www.amazon.co.uk/Expert-Programming-Peter-van-Linden/dp/0131774298).
The name of the book is misleading because it's very easily read by beginners I think.
Expert C Programming: Deep C Secrets https://www.amazon.com/dp/0131774298/
A little dated, still lots of relevant knowledge though.
http://www.amazon.com/dp/0131774298/?tag=akhn-20
Talks about all the weird historical C design decisions.
# 21st century C programming
http://www.amazon.com/dp/0131774298/?tag=akhn-20
Discusses the language and some of the best practices.
https://www.amazon.com/Expert-Programming-Peter-van-Linden/d...
Every C programmer should have this on their bookshelf!
You must read Expert C Programming by Peter van der Linden.

Reference Style - All Levels
Beginner
Intermediate
Above Intermediate
Uncategorized Additional C Programming Books
I'm not sure what you are trying to do but the assignment of a pointer value to an array is what's bothering the compiler as mentioned by Charlie. I'm curious about checking against the NUL character constant
'\0'
. Your sample array is uninitialized memory so the comparison inarrayGen
isn't going to do what you want it to do.The parameter list that you are using ends up being identical to:
for most purposes. The actual statement in the standard is:
If you really want to force the caller to use an array, then use the following syntax:
Your compiler should complain if you call it with anything that isn't a pointer to an array of 10 integers. I can honestly say though that I have never seen this one anywhere outside of various books (The C Book, Expert C Programming)... at least not in C programming. In C++, however, I have had reason to use this syntax in exactly one case:
Your mileage may vary though. If you really want to dig into stuff like this, I can't recommend Expert C Programming highly enough. You can also find The C Book online at gbdirect.
Let me start off by saying something a little off topic:
Anyway, it looks like you're looking at the extra credit exercises from this chapter.
First we need to understand that pointers are not the same as arrays. I've expanded on this in another answer here, and I'm going to borrow the same diagram from the C FAQ. Here's what's happening in memory when we declare an array or a pointer:
So, in the code from the book, when we say:
We get:
I'm going to use an illegal statement for the purpose of explanation - if we could have said:
It would have resulted in:
Both of these can be accessed the same way later on - the first element "23" can be accessed by
ages[0]
, regardless of whether it's an array or a pointer. So far so good.However, when we want to get the count we run in to problems. C doesn't know how big arrays are - it only knows how big (in bytes) the variables it knows about are. This means, with the array, you can work out the size by saying:
or, more safely:
In the array case, this says:
which correctly gives the length of the array. However, for the pointer case, it will read:
which is almost certainly not the same as the length of the array. Where pointers to arrays are used, we need to use another method to work out how long the array is. In C, it is normal to either:
Remember how many elements there were:
or, keep a sentinel value (that will never occur as an actual value in the array) to indicate the end of the array:
(this is how strings work, using the special NUL value '\0' to indicate the end of a string)
Now, remember that I said you can't actually write:
This is because the compiler won't let you assign an implicit array to a pointer. If you REALLY want to, you can write:
But don't, because it is extremely unpleasant to read. For the purposes of this exercise, I would probably write:
Note that the compiler is "decaying" the array name to a pointer to it's first element there - it's as if you had written:
However - you can also dynamically allocate the arrays. For this example code, it will become quite wordy, but we can do it as a learning exercise. Instead of writing:
We could allocate the memory using malloc:
Note that we then need to free
ages
when we're done with the memory:Note that there are a few ways to write the malloc call:
This is clearer to read for a beginner, but generally considered bad style because there are two places you need to change if you change the type of
ages
. Instead, you can write either of:These statements are equivalent - which you choose is a matter of personal style. I prefer the first one.
One final thing - if we're changing the code over to use arrays, you might look at changing this:
But, you don't need to. The reason why is a little subtle. First, this declaration:
says "there is an array of pointers-to-char called argv". However, the compiler treats arrays in function arguments as a pointer to the first element of the array, so if you write:
The compiler will actually see:
This is also the reason that you can omit the length of the first dimension of a multidimensional array used as a function argument - the compiler won't see it.
It declares
f
as an array of pointers to function that returnsint
and takeint *
as an argument.There is a precedence rule for understanding complex declarations which is discussed in the book Expert C Programming: Deep C Secrets:
Therefore, it goes like:
I would suggest to avoid spiral rule as it fails in some cases, for example in case of
int* a[10][15];
.I think this is because of the Maximal Munch Rule. From Wiki:
From Expert C Programming:
Legacy Fortran Soapbox
I helped maintain/improve a legacy Fortran code base for quite a while and for the most part think sixlettervariables is on the money. That advice though, tends to the technical; a tougher row to hoe is in implementing "good practices".
These might sound like obvious things these days, but at the risk of over-generalizing, I claim that most Fortran code shops have an entrenched culture, some started before the term "software engineering" even existed, and that over time what comes to dominate is "Get it done now". (This is not unique to Fortran shops by any means.)
Embracing Gotchas
But what to do with an already existing, grotty old legacy code base? I agree with Joel Spolsky on rewriting, don't. However, in my opinion sixlettervariables does point to the allowable exception: Use software tools to transition to better Fortran constructs. A lot can be caught/corrected by code analyzers (FORCHECK) and code rewriters (plusFORT). If you have to do it by hand, make sure you have a pressing reason. (I wish I had on hand a reference to the number of software bugs that came from fixing software bugs, it is humbling. I think some such statistic is in Expert C Programming.)
Probably the best offense in winning the game of Fortran gotchas is having the best defense: Knowing the language fairly well. To further that end, I recommend ... books!
Fortran Dead Tree Library
I have had only modest success as a "QA nag" over the years, but I have found that education does work, some times inadvertently, and that one of the most influential things is a reference book that someone has on hand. I love and highly recommend
Fortran 90/95 for Scientists and Engineers, by Stephen J. Chapman
The book is even good with Fortran 77 in that it specifically identifies the constructs that shouldn't be used and gives the better alternatives. However, it is actually a textbook and can run out of steam when you really want to know the nitty-gritty of Fortran 95, which is why I recommend
Fortran 90/95 Explained, by Michael Metcalf & John K. Reid
as your go-to reference (sic) for Fortran 95. Be warned that it is not the most lucid writing, but the veil will lift when you really want to get the most out of a new Fortran 95 feature.
For focusing on the issues of going from Fortran 77 to Fortran 90, I enjoyed
Migrating to Fortran 90, by Jim Kerrigan
but the book is now out-of-print. (I just don't understand O'Reilly's use of Safari, why isn't every one of their out-of-print books available?)
Lastly, as to the heir to the wonderful, wonderful classic, Software Tools, I nominate
Classical FORTRAN, by Michael Kupferschmid
This book not only shows what one can do with "only" Fortran 77, but it also talks about some of the more subtle issues that arise (e.g., should or should not one use the EXTERNAL declaration). This book doesn't exactly cover the same space as "Software Tools" but they are two of the three Fortran programming books that I would tag as "fun".... (here's the third).
Miscellaneous Advice that applies to almost every Fortran compiler
One excellent book on "advanced" C programming is Peter van der Linden's Expert C Programming.
You even get an appendix with funny interview stories :)!
Then again, as far as actual grammars go, I've heard C++ is bad enough that the compilers are the standard, and that if you want to be "compliant" with real world C++ code you copy every feature [1] of GCC.
[0]: http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp...
[1]: ftp://ftp.trailing-edge.com/pub/rsx11freewarev2/rsx81b/374001/jargon.txt
http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp...