Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Tuesday, 11 October 2016

A brief understanding of pointers in C

The computer’s memory is a sequential collection of storage cells. Each cell, commonly known as a byte, has a number called address associated with it. Typically, the addresses are numbered consecutively, starting from zero. The last address depends on the memory size. A computer system having 64k memory will have its last address as 65,535.
Whenever we declare a variable in our programs, the system allocates somewhere in the memory, an appropriate location to hold the value of the variable. This location will have its own address number. Consider the following example:
The above statement int var creates a location in the memory to hold integer value. That location will have an address for example assume it is 5000. The statement var = 200 stores the value 200 at the location whose address is 5000. So, in our example, var is the variable name, 200 is the value stored in var and 5000 is the address of the memory location containing the variable var.
So, we can access the value 200 either by using the variable name var or by using the address of the memory location which is 5000. We can access using the second method by storing the address in another variable. This concept of storing memory address in a variable and accessing the value available at that address is known as a pointer variable. Since the pointer is also variable, it will also have a memory address just like any other variable.

Pointer Constants

The memory addresses within a computer are referred to as pointer constants.

Pointer Values

We cannot assign the memory addresses directly to a variable. We can only get the address of a variable by using the address operator (&). The value thus obtained is known as pointer value.

Pointer Variables

Once we obtain the pointer value, we can store it in a variable. Such variable which stores the memory address is known as a pointer variable.

Accessing the Address of a Variable

The actual location of a variable in memory is system dependent. A programmer cannot know the address of a variable immediately. We can retrieve the address of a variable by using the address of(&) operator. Let’s look at the following example:
In the above example, let the variable a is stored at memory address 5000. This can be retrieved by using the address of operator as &a. So the value stored in variable p is 5000 which is the memory address of variable a. So, both the variable a, and p point to the same memory location.

Declaring and Initializing Pointer Variables

Declaration

In C, every variable must be declared for its type. Since pointer variables contain addresses that belong to a specific data type, they must be declared as pointers before we use them. The syntax for declaring a pointer is as shown below:
This tells the compiler three things about the variable pointer-name. They are:

  1. The asterisk (*) tells that the variable pointer-name is a pointer variable.
  2. pointer-name needs a memory location.
  3. pointer-name points to a variable of type datatype. 
Consider the following example for declaring pointers:

Initialization

After declaring a pointer variable we can store the memory address into it. This process is known as initialization. All uninitialized pointers will have some unknown values that will be interpreted as memory addresses. Since compilers do not detect these errors, the programs with uninitialized pointers will produce erroneous results.
Once the pointer variable has been declared, we can use the assignment operator to initialize the variable. Consider the following example:
In the above example, we are assigning the address of variable var to the pointer variable p by using the assignment operator. We can also combine the declaration and initialization into a single line as shown below:

Accessing a Variable Using Pointer

After declaring and initializing a pointer variable, we can access the value of the variable pointed by the pointer variable using the * operator. The * operator is also known as indirection operator ordereferencing operator. Consider the following example:
In the above example, we are printing the value of the variable var by using the pointer variable palong with the * operator.

Thursday, 13 September 2012

Usage Of Unions with typedef

Can you predict the output of this program ?
main()
{
        typedef union {
           int     a;
           char    b[10];
           float   c;
        } Union
        Union   x, y = { 100 }; // here y = 100 bcoz its property of union
        x.a = 50;s
trcpy (x.b, "hello");
        x.c = 21.50;
        printf ("Union x : %d %s %f\n", x.a, x.b, x.c);
        printf ("Union Y : %d %s %f\n", y.a, y.b, y.c);
}


Solution:

This is the  problem  about  Unions.  Unions are  similar to
structures  but it  differs  in  some  ways.  Unions  can be
assigned  only  with one  field at any time.  In this  case,
unions x and y can be  assigned  with any of the one field a
or b or c at one time.  During  initialisation  of unions it
takes  the value  (whatever  assigned  ) only for the  first
field.  So, The statement y = {100}  intialises  the union y
with field a = 100.
In this  example,  all fields of union x are  assigned  with
some  values.  But at any time only one of the  union  field
can  be  assigned.  So,  for  the  union  x the  field  c is
assigned as 21.50.
Thus, The output will be 
        Union x : 22 22 21.50
        Union Y : 100 22 22
        ( 22 refers unpredictable results ) 

Reference :


http://www.c-puzzles.thiyagaraaj.com/level-two.html


What should you required to learn machine learning

  To learn machine learning, you will need to acquire a combination of technical skills and domain knowledge. Here are some of the things yo...