Passing Structures to Functions
Here we can discuss passing structures and their members to functions .
Passing Structure Members to Functions
When you pass a member of a structure to a function , you are actually passing the value of that member to the function . Therefore, you are passing a simple variable (unless, of course, that element is compound, such as an array ). For example, consider this structure:
struct fred
{
char x;
int y;
float z;
char s[10];
} mike;
Here are... Read Full Story
Structures
A structure is a collection of variables referenced under one name, providing a convenient means of keeping related information together. A structure declaration forms a template that may be used to create structure objects (that is, instances of a structure). The variables that make up the structure are called members. (Structure members are also commonly referred to as elements or fields).
Generally, all of the members of a structure are logically related. For example, the... Read Full Story
Structure Assignments
The information contained in one structure may be assigned to another structure of the same type using a single assignment statement. That is, you do not need to assign the value of each member separately. The following program illustrates structure assignments:
#include
int main(void)
{
struct {
int a;
int b;
} x, y;
x.a = 10;
y = x; /* assign one structure to another */
printf(”%d”, y.a);
return 0;
}
After the assignment, y.a will contain the value 10... Read Full Story
I thought the following program was a perfect C program. But on compiling, I found a silly mistake. Can you find it out (without compiling the program :-) ? #include void OS_Solaris_print () { printf ( "Solaris - Sun Microsystems \n " ); } void OS_Windows_print () { printf ( "Windows - Microsoft \n " ); } void OS_HP-UX_print () { printf ( "HP-UX - Hewlett Packard \n " ); } int main () { int num ; printf ( "Enter the number (1-3): \n " ); scanf ( " %d " , & num ); switch ( num ) { case 1... Read Full Story
Function Arguments
If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function . They behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit. As shown in the following function , the parameter declarations occur after the function name:
/* Return 1 if c is part of string s; 0 otherwise. */
int is_in(char *s, char c... Read Full Story
Returning Pointers
Although functions that return pointers are handled just like any other type of function , a few important concepts need to be discussed.
Pointers to variables are neither integers nor unsigned integers. They are the memory addresses of a certain type of data. The reason for this distinction is because pointer arithmetic is relative to the base type. For example, if an integer pointer is incremented, it will contain a value that is 4 greater than its previous value... Read Full Story
Pointers to Functions
A particularly confusing yet powerful feature of C++ is the function pointer. Even though a function is not a variable , it still has a physical location in memory that can be assigned to a pointer . This address is the entry point of the function and it is the address used when the function is called. Once a pointer points to a function, the function can be called through that pointer . Function pointers also allow functions to be passed as arguments to other... Read Full Story
Initializing Pointers
After a local pointer is declared but before it has been assigned a value, it contains an unknown value. (Global pointers are automatically initialized to null.) Should you try to use the pointer before giving it a valid value, you will probably crash your program—and possibly your computer’s operating system as well—a very nasty type of error!
There is an important convention that most C / C++ programmers follow when working with pointers: A pointer that does not... Read Full Story
Problems with Pointers
Nothing will get you into more trouble than a wild pointer! Pointers are a mixed blessing. They give you tremendous power and are necessary for many programs. At the same time, when a pointer accidentally contains a wrong value, it can be the most difficult bug to find. An erroneous pointer is difficult to find because the pointer itself is not the problem. The problem is that each time you perform an operation using the bad pointer, you are reading or writing to some... Read Full Story
Array
An array is a collection of variables of the same type that are referred to through a common name. A specific element in an array is accessed by an index. In C / C++ , all arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. Arrays may have from one to several dimensions. The most common array is the null-terminated string, which is simply an array of characters terminated by a null. Arrays and... Read Full Story