C(Pointers)

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

goto,longjmp(),setjmp() statements

A goto statement implements a local jump of program execution, and the longjmp() and setjmp() functions implement a nonlocal, or far, jump of program execution. Generally, a jump in execution of any kind should be avoided because it is not considered good programming practice to use such statements as goto and longjmp in your program. A goto statement simply bypasses code in your program and jumps to a predefined position. To use the goto statement, you give it a labeled position to jump to. This predefined position must be within the same function. You cannot implement gotos between functions. the longjmp() and setjmp() functions implement a nonlocal goto. When your program calls setjmp(), the current state of your program is saved in a structure of type jmp_buf. Later, your program can call the longjmp() function to restore the program's state as it was when you called setjmp(). Unlike the goto statement, the longjmp() and setjmp() functions do not need to be implemented in the same function.

What is a 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! 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.)

when is void pointer used?

A void pointer is used for working with raw memory or for passing a pointer to an unspecified type. memcpy() is used to move data from one location to another: void *memcpy( void *addr1, void *addr2, size_t n ); void pointers are used to mean that this is raw memory being copied. NUL characters (zero bytes) aren't significant, and just about anything can be copied.

What is indirection?

If you have a pointer to a variable, or any other object in memory, you have an indirect reference to its value. *p means "apply the indirection operator to p

Can an array be an lvalue?

Is an array an expression to which we can assign a value? The answer to this question is no, because an array is composed of several separate array elements that cannot be treated as a whole for assignment purposes. The following statement is therefore illegal: int x[5], y[5]; x = y;

Is NULL always defined as 0(zero)?

NULL is defined as either 0 or (void*)0. These values are almost identical; either a literal zero or a void pointer is converted automatically to any kind of pointer, as necessary, whenever a pointer is needed (although the compiler can't always tell when a pointer is needed).

Is left-to-right or right-to-left order guaranteed for operator precedence?

The simple answer to this question is neither. The C language does not always evaluate left-to-right or right-to-left. Generally, function calls are evaluated first, followed by complex expressions and then simple expressions.

strcpy() and strncpy()

strcpy() is used to copy data from one string to another, and strncpy() is used to copy at most a certain length string to another: char *strcpy( char *str1, const char *str2 ); char *strncpy( char *str1, const char *str2, size_t n );


Kaugnay na mga set ng pag-aaral

LACS 101 (College of Charleston) Final Exam- Chapter 9

View Set

Transposition of the Great Arteries

View Set

Chapter 8.2 The Louisiana Purchase

View Set

AQA GCSE physics Electromagnetism P15 (Paper 2)

View Set

Chemistry- (Unit 1) Cumulative Review

View Set

Renewable and non renewable resources

View Set

IST 110 Penn State - Exam 1 Dr. K

View Set