C Pointers

  Home  C Language  C Pointers

“C Pointers frequently Asked Questions by expert members with experience in C pointers. These questions and answers will help you strengthen your technical skills, prepare for the new job test and quickly revise the concepts”

31 C Pointers Questions And Answers

1⟩ What is the difference between strcpy() and memcpy() function?

The following are the differences between strcpy() and memcpy():

- memcpy() copies specific number of bytes from source to destinatio in RAM, where as strcpy() copies a constant / string into another string.

- memcpy() works on fixed length of arbitrary data, where as strcpy() works on null-terminated strings and it has no length limitations.

- memcpy() is used to copy the exact amount of data, whereas strcpy() is used of copy variable-length null terminated strings.


2⟩ What is the use of bit field?

Packing of data in a structured format is allowed by using bit fields. When the memory is a premium, bit fields are extremely useful. For example:

- Picking multiple objects into a machine word : 1 bit flags can be compacted

- Reading external file formats : non-standard file formats could be read in, like 9 bit integers

This type of operations is supported in C language. This is achieved by putting :’bit length’ after the variable. Example:

struct packed_struct {

unsigned int var1:1;

unsigned int var2:1;

unsigned int var3:1;

unsigned int var4:1;

unsigned int var5:4;

unsigned int funny_int:9;

} pack;

packed-struct has 6 members: four of 1 bit flags each, and 1 4 bit type and 1 9 bit funny_int.

C packs the bit fields in the structure automatically, as compactly as possible, which provides the maximum length of the field is less than or equal to the integer word length the computer system.

The following points need to be noted while working with bit fields:

The conversion of bit fields is always integer type for computation

Normal types and bit fields could be mixed / combined

Unsigned definitions are important.


3⟩ Tell me what are bitwise shift operators?

<< - Bitwise Left-Shift

Bitwise Left-Shift is useful when to want to MULTIPLY an integer (not floating point numbers) by a power of 2.

Expression: a << b

This expression returns the value of a multiplied by 2 to the power of b.

>> - Bitwise Right-Shift

Bitwise Right-Shift does the opposite, and takes away bits on the right.

Expression: a >> b

This expression returns the value of a divided by 2 to the power of b.


4⟩ Explain #pragma statements?

The #pragma Directives are used to turn ON or OFF certain features. They vary from compiler to compiler.

Examples of pragmas are:

#pragma startup // you can use this to execute a function at startup of a program

#pragma exit // you can use this to execute a function at exiting of a program

#pragma warn –rvl // used to suppress return value not used warning

#pragma warn –par // used to suppress parameter not used warning

#pragma warn –rch // used to suppress unreachable code warning


6⟩ Can you please compare array with pointer?

A pointer is an address location of another variable. It is a value that designates the address or memory location of some other value (usually value of a variable). The value of a variable can be accessed by a pointer which points to that variable. To do so, the reference operator (&) is pre-appended to the variable that holds the value. A pointer can hold any data type, including functions.


7⟩ What is the difference between exit() and _exit() function?

The following are the differences between exit() and _exit() functions:

- io buffers are flushed by exit() and executes some functions those are registered by atexit().

- _exit() ends the process without invoking the functions which are registered by atexit().


8⟩ What are the advantages of using macro in C Language?

In modular programming, using functions is advisable when a certain code is repeated several times in a program. However, everytime a function is called the control gets transferred to that function and then back to the calling function. This consumes a lot of execution time. One way to save this time is by using macros. Macros substitute a function call by the definition of that function. This saves execution time to a great extent.


9⟩ What is the C Language function prototype?

The basic definition of a function is known as function prototype. The signature of the function is same that of a normal function, except instead of containing code, it always ends with semicolon.

When the compiler makes a single pass over each and every file that is compiled. If a function call is encountered by the compiler, which is not yet been defined, the compiler throws an error.

One of the solution for the above is to restructure the program, in which all the functions appear only before they are called in another function.

Another solution is writing the function prototypes at the beginning of the file, which ensures the C compiler to read and process the function definitions, before there is a change of calling the function. If prototypes are declared, it is convenient and comfortable for the developer to write the code of those functions which are just the needed ones.


10⟩ Do you know the difference between malloc() and calloc() function?

The following are the differences between malloc() and calloc():

- Byte of memory is allocated by malloc(), whereas block of memory is allocated by calloc().

- malloc() takes a single argument, the size of memory, where as calloc takes two parameters, the number of variables to allocate memory and size of bytes of a single variable

- Memory initialization is not performed by malloc() , whereas memory is initialized by calloc().

- malloc(s) returns a pointer with enough storage with s bytes, where as calloc(n,s) returns a pointer with enough contiguous storage each with s bytes.


15⟩ Do you know the use of fflush() function?

In ANSI, fflush() [returns 0 if buffer successfully deleted / returns EOF on an error] causes the system to empty the buffer associated with the specified output stream.

It undoes the effect of any ungetc() function if the stream is open for input. The stream remains open after the call.

If stream is NULL, the system flushes all open streams.

However, the system automatically deletes buffers when the stream is closed or even when a program ends normally without closing the stream.


16⟩ Explain void pointer?

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!


17⟩ Explain null pointer?

There are times when it's necessary to have a pointer that doesn't point to anything. The macro NULL, defined in <stddef.h>, has a value that's guaranteed to be different from any valid pointer. NULL is a literal zero, possibly cast to void* or char*. Some people, notably C++ programmers, prefer to use 0 rather than NULL.<stddef.h>


19⟩ Tell me when would you use a pointer to a function?

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.


20⟩ Can we add pointers together?

No, you can't add pointers together. If you live at 1332 Lakeview Drive, and your neighbor lives at 1364 Lakeview, what's 1332+1364? It's a number, but it doesn't mean anything. If you try to perform this type of calculation with pointers in a C program, your compiler will complain.