ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am stuck in understanding a code given in one of the leading books on linux programming, the program is to print the content of a dir. the place I dont understand , I marked with `?`. I use FC4. Here goes the code :
The initializations you're referring to are assignments which occur inside the conditional "if" and "while" statements. In C, the binary assignment operator ("=") returns the value of the right-hand side, in addition to performing the assignment to the left-hand side as a side-effect. Thus, you commonly see things like "while ((c = fgetc(fp)) != EOF)" where the result of fgetc(fp) is assigned to c and then compared against EOF.
Last edited by taylor_venable; 07-05-2006 at 03:18 PM.
DIR *dp;
struct dirent *entry;
struct stat statbuff;
if((dp = opendir(dir)) == NULL)
{
fprintf(stderr,"cannot open dir %s\n",dir);
return;
}
chdir(dir);
while((entry=readdir(dp)) != NULL) /* <========= entry init'd here */
/* what about initialisation of dp*/
{
lstat(entry->d_name,&statbuff);
/* what about initialisation of entry ?*/
if(S_ISDR(statbuf.st_mode))
{
'entry' is assigned the return value from 'readdir(dp)'.
It is helpful to the readers of your questions if you place [ CODE] [ /CODE] wrappers around your source code.
Even though C/C++ provide these "helpful" constructions, they are confusing (as you saw demonstrated), and you might well elect not to use them. In any modern compiler, they won't make any difference in the final code.
What matters .. what matters most above all .. is that your code is clear, expressive of what you want to do, and maintainable (long after you've been smooshed by a bread-truck .. or, if you prefer, long after you've cashed in your stock-options and retired to Pango-Pango where you are now idly using solid gold coins as poker-chips). So, if you find constructions like these confusing, don't use them in your code (but do be prepared to see them in somebody else's). When you write your code, put meaningful comments in it. Try to write as little of your code as possible at 1:30 AM.
while((entry=readdir(dp)) != NULL) /* <========= entry init'd here */
This is somewhat nitpicky, but with all due respect to theNbomr, this technically isn't initialization. entry is considered an uninitialized variable because it isn't assigned when it is created and thus initially contains garbage.
Last edited by taylor_venable; 07-06-2006 at 09:13 AM.
Reason: Was missing end "code" tag.
t_v, you pick a mean nit :-) and many would feel insulted by your exquisite pedantry. However, you are quite correct. I inadvertantly used the term 'initialize', because it was the word used by the original poster.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.