Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers
Antivirus Faqs_Questions_And_Answers Software Tips Faqs_Questions_And_Answers ERP Faqs_Questions_And_Answers Internet Faqs_Questions_And_Answers Spoken English Faqs_Questions_And_Answers Web Designing Faqs_Questions_And_Answers SEO Tips Faqs_Questions_And_Answers Hacking
Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers
SAP Faqs
C Faqs        C++ Faqs        Java Faqs        J2ee Faqs        Springs Faqs        Hibernate Faqs        Web sphere Faqs        Web Logic Server Faqs   
WordPress        Siebel Faqs        Shell Scripting Faqs        Perl Scripting Faqs        Open Source        Data Ware Housing Faqs        Joomla Faqs
Ajax        Oracle Apps Faqs        Testing Tools Faqs        Mainframes Faqs        Tibco Faqs        PHP Faqs        .Net Faqs       Operating System
 
 
C Interview Questions
Custom Search
43

What is a “null pointer assignment” error? What are bus errors, memory faults, and core dumps?

Ans:

These are all serious errors, symptoms of a wild pointer or subscript. Null pointer assignment is a message you might get when an MS-DOS program finishes executing. Some such programs can arrange for a small amount of  memory to be available “where the NULL pointer points to” (so to speak). If the program tries  to write to that area, it will overwrite the data put there by the compiler. When the program is done, code generated by the compiler examines that area. If that data has been changed, the compiler-generated code complains with null pointer assignment. This message carries only enough information to get you worried. There’s no way to tell, just from a null pointer assignment message, what part of your program is responsible for the error. Some debuggers, and some compilers, can give you more  help in finding the problem. Bus error: core dumped and Memory fault: core dumped are messages you might see from a program running under UNIX. They’re more programmer friendly. Both mean that a pointer or an array  subscript was wildly out of bounds. You can get  these messages on a read or on a write. They  aren’t restricted to null pointer problems. The core dumped part of the message is telling you about a file, called core, that has just  been written in your current directory. This is a dump of everything on the stack and in the heap at the time the program was running. With  the help of a debugger, you can use the core dump to find where the bad pointer was used. That might not tell you why the pointer was bad, but it’s a step in the right direction. If you don’t have write permission in the current directory, you won’t get a core file, or the core dumped message.

44

What is a void pointer?

Ans: A void pointer is a C convention for “a raw address.” The compiler has no idea what type of object a void Pointer “really points to.” If  you write
         int *ip;   ip points to an int. If you write
         void *p;   p doesn’t point to a void!
In C and C++, any time you need a void pointer, you can use another pointer type. For example, if you have a char*, you can pass it to a function that expects a void*. You don’t even need to cast it. In C (but not in C++), you can use a void* any time you need any kind of  pointer, without casting. (In C++, you need to cast it).  A void pointer is used for working with raw memory or for passing a pointer to an unspecified type. Some C code operates on raw memory. When C was first invented, character pointers (char *) were used for that. Then people started getting confused about when a character pointer was a string, when it was a character array, and when it was raw memory.
45

What does it mean when a pointer is used in an if statement?

Ans:

Any time a pointer is used as a condition, it means “Is this a non-null pointer?” A pointer can be used in an if, while, for, or do/while statement, or in a conditional expression.

46

How do you use a pointer to a function?

Ans:

The hardest part about using a pointer-to-function is declaring it. Consider an example. You want to create a pointer, pf, that points to the strcmp() function. The strcmp() function is declared in this way: int strcmp(const char *, const char * ) To set up pf to point to the strcmp() function, you want a declaration that looks just like the strcmp() function’s declaration, but that has *pf rather than strcmp:  int (*pf)( const char *, const char * ); After you’ve gotten the declaration of pf, you can #include and assign the address of strcmp() to pf: pf = strcmp;

47

Why should we assign NULL to the elements (pointer) after freeing them?

Ans:

This is paranoia based on long experience. After a pointer has been freed, you can no longer use the pointed-to data. The pointer is said to “dangle”; it doesn’t point at anything useful. If you “NULL out” or “zero out” a pointer immediately after freeing it, your program can no longer get in trouble by using that pointer. True, you might go indirect on the null pointer instead, but that’s something your debugger might be able to help you with  immediately. Also, there still might be copies of the pointer that refer to the memory that has been deal located; that’s the nature of C. Zeroing out pointers after freeing them won’t solve all problems;

48

When would you use a pointer to a function?

Ans: Pointers to functions are interesting when you pass them to other functions. A function that takes function pointers says, in effect, “Part of what I do can be customized. Give me a pointer to a function, and I’ll call it when that part of the job needs to be done. That function can do its part for me.” This is known  as a “callback.” It’s used a lot in graphical user interface libraries, in which the style of  a display is built into the library but the contents of the display are part of the application. As a simpler example, say you have an array of character pointers (char*s), and you want to sort it by the value of the strings the character pointers point to. The standard qsort() function uses function pointers to perform that task. qsort() takes four arguments,a pointer to the beginning of the arrathe number of elements in the array,the size of each array element, anda comparison function, and returns an int.
49

Is it better to use malloc() or calloc()?

Ans:

Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slightly different from the other. malloc() takes a size and returns a pointer to a chunk of memory at least that big: void *malloc( size_t size );calloc() takes a number of elements, and the size of each, and returns a pointer to a chunk of memory at least big enough to hold them all: void *calloc( size_t numElements, size_t sizeOfElement ); There’s one major difference and one minor difference between the two functions. The major difference is that malloc() doesn’t initialize   the allocated memory. The first time malloc() gives you a particular chunk of memory, the  memory might be full of zeros. If memory has been allocated, freed, and reallocated, it probably has whatever junk was left in it. That means, unfortunately, that a program might run in simple cases (when memory is never reallocated) but break when used harder (and when memory is reused). calloc() fills the allocated memory with all zero bits. That means that anything there you’re going to use as a char or an int of any length, signed or unsigned, is guaranteed to be zero. Anything you’re going to use as a pointer is set to all zero bits. That’s usually a null pointer, but it’s not guaranteed. Anything you’re going to use as a float or double is set to all zero bits; that’s a floating-point zero on some types of machines, but not on all. The minor difference between the two is that calloc() returns an array of objects; malloc() returns one object. Some people use calloc() to  make clear that they want an array.

50

What is the benefit of using an enum rather than a #define constant?

Ans: The use of an enumeration constant (enum) has many advantages over using the traditional symbolic constant style of #define. These advantages include a lower maintenance requirement, improved program readability, and better debugging capability.

1) The first advantage is that enumerated constants are generated automatically by the
compiler. Conversely, symbolic constants must be manually assigned values by the programmer. For instance, if you had an enumerated constant type for error codes that could occur in your program, your enum definition could look something like this:
        enum Error_Code
        {
           OUT_OF_MEMORY,
           INSUFFICIENT_DISK_SPACE,
           LOGIC_ERROR,
           FILE_NOT_FOUND
          };
In the preceding example, OUT_OF_MEMORY is automatically assigned the value of 0 (zero) by the compiler because it appears first in the definition. The compiler then continues to automatically assign numbers to the enumerated constants, making INSUFFICIENT_DISK_SPACE equal to 1, LOGIC_ERROR equal to 2, and FILE_NOT_FOUND equal to 3, so on. If you were to approach the same example by using symbolic constants, your code would look something like this:
 
                                #define OUT_OF_MEMORY 0
                                #define INSUFFICIENT_DISK_SPACE 1
                                #define LOGIC_ERROR 2
                                #define FILE_NOT_FOUND 3
 
values by the programmer. Each of the two methods arrives at the same result: four
constants assigned numeric values to represent error codes. Consider the maintenance required, however, if you were to add two constants to represent the error codes DRIVE_NOT_READY and CORRUPT_FILE. Using the enumeration constant
method, you simply would put these two constants anywhere in the enum definition. The compiler would generate two unique values for these constants. Using the symbolic constant method, you would have to manually assign two new numbers to these constants. Additionally, you would want to ensure that the numbers you assign to these constants are unique.

2) Another advantage of using the enumeration constant method is that your programs are more readable and thus can be understood better by others who might have to update your program later.

3) A third advantage to using enumeration constants is that some symbolic debuggers can print the value of an enumeration constant. Conversely, most symbolic debuggers cannot print the value of a symbolic constant. This can be an enormous help in debugging your program, because if your program is stopped at a line that uses an enum, you can simply inspect that constant and instantly know its value. On the other hand, because most debuggers cannot print #define values, you would most likely have to search for that value by manually looking it up in a header file.

51

How are portions of a program disabled in demo versions?

Ans: If you are distributing a demo version of your program, the preprocessor can be used to enable or disable portions of your program. The following portion of code shows how this task is accomplished, using the preprocessor directives #if and #endif:
                            int save_document(char* doc_name)
                                {
                                #if DEMO_VERSION
                                printf(“Sorry! You can’t save documents using
                                the DEMO version of this program!n”);
                                return(0);
                                #endif
                                ...
                                }
52

What is the difference between #include <file> and #include “file”?

Ans: When writing your C program, you can include files in two ways. The first way is to surround the file you want to include with the angled brackets < and >. This method of inclusion tells the preprocessor to look for the file in the  predefined default location. This predefined default location is often an INCLUDE environment  variable that denotes the path to your include  files. For instance, given the INCLUDE variable
   INCLUDE=C:\COMPILER\INCLUDE;S:\SOURCE\HEADERS;
using the #include version of file inclusion, the compiler first checks the
C:\COMPILER\INCLUDE
directory for the specified file. If the file is not found there, the compiler then checks the
S:\SOURCE\HEADERS directory. If the file is  still not found, the preprocessor checks the current directory. The second way to include files is to surround  the file you want to include with double quotation marks. This method of inclusion tells the preprocessor to look for the file in the current directory first, then look for it in the  predefined locations you have set up. Using the  #include “file” version of file inclusion and applying it to the preceding example, the preprocessor first checks the current directory for the specified file. If the file is not found in the current directory, the C:COMPILERINCLUDE directory is searched. If the file is still not found, the preprocessor checks the          S:SOURCEHEADERS directory. The #include method of file inclusion is often used to include standard headers such as stdio.h  or  stdlib.h. This is because these headers are rarely (if ever) modified, and they should  always be read from your compiler’s standard include file directory.
The #include “file” method of file inclusion is often used to include nonstandard header files that you have created for use in your program. This is because these headers are often modified in the current directory, and you will want the preprocessor to use your newly modified version of the header rather than the older, unmodified version.
53

How many levels of pointers can you have?

Ans:

The answer depends on what you mean by “levels of pointers.” If you mean “How many  levels of indirection can you have in a single declaration?” the answer is “At least 12.”
                                int i = 0;
                                int *ip01 = & i;
                                int **ip02 = & ip01;
                                int ***ip03 = & ip02;
                                int ****ip04 = & ip03;
                                int *****ip05 = & ip04;
                                int ******ip06 = & ip05;
                                int *******ip07 = & ip06;
                                int ********ip08 = & ip07;
                                int *********ip09 = & ip08;
                                int **********ip10 = & ip09;
                                int ***********ip11 = & ip10;
                                int ************ip12 = & ip11;
                                ************ip12 = 1; /* i = 1 */
The ANSI C standard says all compilers must handle at least 12 levels. Your compiler might support more.

Page :    1 | 2 | 3 | 4 | 5 | 6
Top