How Do You Use A Pointer To A Function?
How Do You Use A Pointer To A Function?
sprintf(...) writes data to the character array whereas printf(...) writes data to the standard output
device.
Linked Lists -- Can you tell me how to check whether a linked list is circular?
Create two pointers, and set both to the start of the list. Update each as follows:
while (pointer1) {
pointer1 = pointer1->next;
pointer2 = pointer2->next;
if (pointer2) pointer2=pointer2->next;
if (pointer1 == pointer2) {
print ("circular");
}
}
If a list is circular, at some point pointer2 will wrap around and be either at the item just
before pointer1, or the item before that. Either way, its either 1 or 2 jumps until they meet.
Write down the equivalent pointer expression for referring the same element a[i][j][k]
[l]?
a[i] == *(a+i)
a[i][j] == *(*(a+i)+j)
a[i][j][k] == *(*(*(a+i)+j)+k)
a[i][j][k][l] == *(*(*(*(a+i)+j)+k)+l)
in char const* p, the ptr ‘p’ is constant not the character referenced by it, so u cant make
‘p’ to reference to any other location but u can change the value of the char pointed by ‘p’.
Why should we assign NULL to the elements (pointer) after freeing them?
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 deallocated; that’s the nature of C. Zeroing out pointers after freeing them won’t
solve all problems;
What is a null pointer assignment error? What are bus errors, memory faults, and
core dumps?
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
What is the difference between a string copy (strcpy) and a memory copy (memcpy)?
When should each be used?
The strcpy() function is designed to work exclusively with strings. It copies each byte of the
source string to the destination string and stops when the terminating null character ()
has been moved. On the other hand, the memcpy() function is designed to work with any
type of data. Because not all data ends with a null character, you must provide the
memcpy() function with the number of bytes you want to copy from the source to the
destination.
What is a pragma?
The #pragma preprocessor directive allows each compiler to implement compiler-specific
features that can be turned on and off with the #pragma statement. For instance, your
compiler might support a feature called loop optimization. This feature can be invoked as a
command-line option or as a #pragma directive.
To implement this option using the #pragma directive, you would put the following line
into your code:
#pragma loop_opt(on)
Conversely, you can turn off loop optimization by inserting the following line into your
code:
#pragma loop_opt(off)
Can the sizeof operator be used to tell the size of an array passed to a function?
No. There’s no way to tell, at runtime, how many elements are in an array parameter just
by looking at the array parameter itself. Remember, passing an array to a function is
exactly the same as passing a pointer to the first element.