A.
There are three types of pointers used in C language. These pointers are based on the old system architecture. The types are as follows:
A.
A dangling pointer is one that has a value (not NULL) which refers to some memory which is not valid for the type of object you expect. For example if you set a pointer to an object then overwrote that memory with something else unrelated or freed the memory if it was dynamically allocated.
A.
The c in argument count, argc stands for the number of the command line argument that the program is invoked with.
And v in argument vector, argv is a pointer to an array of the character string that contains the argument.
A.
A.
Pointer variables are initialized in two ways:
A.
E.g. int main(); OR int main(int argc, char *argv[]);
A.
Argument : An expression which is passed to a function by its caller for the function to perform its task.
Arguments are of two types: actual and formal.
Actual argument: These are the arguments passed in a function call. They are defined in the calling function.
Formal argument: Formal arguments are the parameters in a function declaration. Their scope is local to the function definition. They belong to the function being called and are a copy of actual arguments. Change in formal argument doesn't get reflected in actual argument.
A.
Pointers are variables which store the address of other variables.
Advantages of using pointers:
A.
You can change the size of the array if it is defined dynamically, by using the realloc() function. realloc() function's syntax is written as:
dynarray = realloc(dynarray, 20 * sizeof(int));
But, realloc() function can't just enlarge the memory, and this memory allocation depends on the space available in the system. If realloc() can?t find the space available, then it returns null pointer.
A.
Macro are simple statements which are written to, dynamically execute the method or function. The function call syntax is:
MACRO(arg1, arg2);
Macro can be included with the conditional statements using the external else clause. You can include simple expressions in macro with no declarations or loops like:
#define FUNC(arg1, arg2) (expr1, expr2, expr3)
A.
There are no arithmetic operations that can be performed on void* pointer because the compiler doesn’t know the size of the pointed objects. Arithmetic operations can only be done on the pointed objects. Some compilers make it an exception to perform the arithmetic functions on the pointer.
A.
A function can return multiple values in several ways by using the pointers. These include the examples like hypothetical calculation of polar-to-rectangular coordinate functions. The pointer allows you to have a function in which you can pass multiple values and use them.
Example code: polar_to_rectangular(double a, double b, double *xp, double *yp) { a = 10; b = 20; *xp = a; *yp = b; }
A.
It is not advised to use "this" pointer inside the constructor, because of the object initialization, as it takes more time for the object to be ready for the execution. You can use "this" pointer in the constructor body part and also in the initialization list. The rules of the programming language have to be known before using "this" operator in the constructor.
A.
A break statement causes the loop to terminate. The control then passes to the statement following the body of the loop.
A.
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. The following program shows examples of both the strcpy() and the memcpy() functions:
#include#include typedef struct cust_str { int id; char last_name[20]; char first_name[15]; } CUSTREC; void main(void); void main(void) { char* src_string = "This is the source string"; char dest_string[50]; CUSTREC src_cust; CUSTREC dest_cust; printf("Hello! I'm going to copy src_string into dest_string! "); /* Copy src_string into dest_string. Notice that the destination string is the first argument. Notice also that the strcpy() function returns a pointer to the destination string. */ printf("Done! dest_string is: %s ", strcpy(dest_string, src_string)); printf("Encore! Let's copy one CUSTREC to another. "); printf("I'll copy src_cust into dest_cust. "); /* First, initialize the src_cust data members. */ src_cust.id = 1; strcpy(src_cust.last_name, "Strahan"); strcpy(src_cust.first_name, "Troy"); /* Now, use the memcpy() function to copy the src_cust structure to the dest_cust structure. Notice that, just as with strcpy(), the destination comes first. */ memcpy(&dest_cust, &src_cust, sizeof(CUSTREC)); printf("Done! I just copied customer number #%d (%s %s).", dest_cust.id, dest_cust.first_name, dest_cust.last_name); }
When dealing with strings, you generally should use the strcpy() function, because it is easier to use with strings. When dealing with abstract data other than strings (such as structures), you should use the memcpy() function.
A.
In C, functions are global by default. The "static" keyword before a function name makes it static. Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files.
A.
Dangling Pointer is a pointer that doesn’t point to a valid memory location. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory.
A.
NULL is used to indicate that the pointer doesn’t point to a valid location. Ideally, we should initialize pointers as NULL if we don’t know their value at the time of declaration. Also, we should make a pointer NULL when memory pointed by it is deallocated in the middle of a program.
A.
A.
#includeint main() { // ASCII value of ; is 59 if (printf("%c", 59)) { } }
A.
int main(void) { if (printf("Hello World")) ; }
A.
auto, register, static, extern