Operators and control Structures Flashcards
void:
its uses:
1) to specify the return type of a function when it is not returning any value, and (2) to indicate an empty argument list to a function. Example: void funct1(void); Another interesting use of void is in the declaration of generic pointers. Example: void *gp; // gp becomes generic pointer
generic Pointer:
Rules:
A generic pointer can be assigned a pointer value of any basic data type, but it may not be dereferenced. For example,
int *ip; // int pointer
gp = ip; // assign int pointer to void pointer
are valid statements. But, the statement,
*ip = *gp;
is illegal. It would not make sense to dereference a pointer to a void value.
dereference:
To access the value or object located in a memory location stored in a pointer or another value interpreted as such; to access a value being referenced by something else. Attempting to dereference a null pointer often results in a crash.
Assigning void pointer in C and C++:
Assigning any pointer type to a void pointer without using a cast is allowed in both C++ and ANSI C.
In ANSI C, we can also assign a void pointer to a nonvoid pointer without using a cast to non-void
pointer type. This is not allowed in C++. For example,
void *ptr1;
char *ptr2;
ptr2 = ptr1;
are all valid statements in ANSI C but not in C++. A void pointer cannot be directly assigned to other
type pointers in C++. We need to use a cast operator as shown below:
ptr2 = (char *)ptr1;
Difference between Union and Structure:
Who is preferred when?
The size of a structure type is equal to the sum of the sizes of individual member types.
However, the size of a union is equal to the size of its largest member
element.
In simple words, we can say
that unions are memory-efficient alternatives of structures particularly in situations where it is not
required to access the different member elements simultaneously.
Union members share common memory space
for their exclusive usage.
I don’t understand:
All members of a structure can be manipulated
simultaneously.
The members of a union can be manipulated
only one at a time.
C++ structures and unions:
Structures in C++ behave just like a class. Almost everything that can be achieved with a class can also be done with structures.
Unions retain their core functionality in C++
with slight add-ons like declaration of
anonymous unions.
enums in C++ :
3 differences:
2 out of those 3
An enumerated data type is another user-defined type which provides a way for attaching names to
numbers, thereby increasing comprehensibility of the code.
The enum keyword (from C) automatically enumerates a list of words by assigning them values 0,1,2, and so on. This facility provides an
alternative means for creating symbolic constants.
Syntax:
enum shape{circle, triangle = 3,square};
[circle will be 0 by default, square will however be now 4 as subsequent initialized enumerators are larger by one than their predecessors.]
The enumerated data types differ slightly in C++ when compared with those in ANSI C. In C++,
the tag name shape become new type names. By using these tag names,
we can declare new variables. Examples:
shape ellipse; // ellipse is of type shape.
ANSI C defines the types of eums to be ints. In C++, each enumerated data type retains its own
separate type. This means that C++ does not permit an int value to be automatically converted to an
enum value.
Examples:
colour background = blue; // allowed
colour background = 7; // Error in C++
colour background = (colour) 7; // OK
However, an enumerated value can be used in place of an int value.
int c = red; // valid, colour type promoted to int
3rd difference:
C++ also permits the creation of anonymous enums (i.e., enums without tag names).
Example:
enum{off, on};
Here, off is 0 and on is 1. These constants may be referenced in the same manner as regular
constants. Examples:
int switch_1 = off;
int switch_2 = on;
Applications:
and scopes in C and C++?
In practice, enumeration is used to define symbolic constants for a switch statement.
ANSI C permits an enum to be defined within a structure or a class, but the enum is globally visible.
In C++, an enum defined within a class (or structure) is local to that class (or structure) only.
Storage class:
Keyword
auto
extern
static
Register
Array in C and C++:
When initializing a character array in ANSI C, the compiler will allow us to declare the
array size as the exact length of the string constant. For instance,
char string[3] = “xyz”;
is valid in ANSI C.
It assumes that the programmer intends to leave out the null character \0 in the definition.
But in C++, the size should be one larger than the number of characters in the string.
char string[4] = “xyz”; // O.K. for C++
Pointer:
Additional feature in C++:
C++ adds the concept of constant pointer and pointer to a constant. char * const ptr1 = “GOOD”; // constant pointer
We cannot modify the address that ptr1 is initialized to.
int const * ptr2 = &m; // pointer to a constant
ptr2 is declared as pointer to a constant. It can point to any variable of correct type, but the contents
of what it points to cannot be changed.
We can declare both the pointer and the variable as constants: const char * const cp = “xyz”; This statment declares cp as a constant pointer to the string which has been declared a constant. In this case, neither the address assigned to the pointer cp nor the contents it points to can be changed.
Create Symbollic Constants:
There are two ways of creating symbolic constants in C++:
• Using the qualifier const, and
• Defining a set of integer constants using enum keyword.
const in C and C++
regarding type constants:
and naming conventions:
. In C++, we can use const in a constant expression, such as const int size = 10; char name[size]; This would be illegal in C. const allows us to create typed constants instead of having to use #define to create constants that have no type information.
As with long and short, if we use the const modifier alone, it defaults to int. For example, const size = 10; means const int size = 10; The named constants are just like variables except that their values cannot be changed
Const
Intialising and scope
in c and c++
C++ requires a const to be initialized. ANSI C does not require an initializer; if none is given, it
initializes the const to 0.
The scoping of const value differs. A const in C++ defaults to the internal linkage and therefore, it is local to the file where it is declared. In ANSI C, const values are global in nature. They are visible outside the file in which they are declared. However, they can be made local by declaring them as static. To give a const value an external linkage so that it can be referenced from another file, we must explicitly define it as an extern in C + + . Example: extern const total = 100;