PDA

View Full Version : Some Imp. question in C.


amit_avesh
09-02-2004, 09:44 AM
1. What is differnce between far, near & rear pointer.
2. Can we specify variable field width in a scanf() format string? If possible how?
3. Out of fgets() and gets() which function is safe to use and why?
4. Difference between strdup and strcpy?
5.Write down the equivalent pointer _expression for referring the same element a[i][j][k][l]?

RobSeace
09-02-2004, 01:53 PM
1. See this very recent thread here (http://www.developerweb.net/forum/viewtopic.php?t=1056)...

2. No, I'm afraid not... Field width must be hard-coded in the format
string for *scanf()... A bit of an oversight on ANSI's part, really...
(Of course, you could kluge around it by generating your format
string on the fly, sprintf()'ing in your variable field width, so that it
becomes part of the format string you later pass to *scanf()... ;-))

3. fgets() is perfectly safe... Assuming you use it correctly, at
least... ;-) Ie: you pass in the correct size of your buffer... However,
gets() is completely unsafe, because it doesn't even take an arg
to tell it how long your buffer is, and will simply keep reading until
it hits a newline, and so can very easily overflow your buffer, if your
input happens to have long enough lines...

4. strcpy() simply copies the contents of one string (char array)
to another... Ie: it copies a sequence of bytes terminated by a null
char from one memory location to another... But, strdup() actually
creates a brand new memory location for you, and THEN copies
in the contents of the source string, returning to you a pointer to
the newly allocated string... Ie: a strdup() is essentially a malloc()
followed by a strcpy()...

5. Heh. Are you getting us to do your homework for you?? ;-)
That sounds suspiciously like some class assignment question...
However, since I'm in a generous mood this morning, I'll play
along... ;-) "*(*(*(*(a+i)+j)+k)+l)" should do it... ;-)