FoodForFutureGeeks

Showing posts with label GeneralProgramming. Show all posts
Showing posts with label GeneralProgramming. Show all posts

Sunday, 15 April 2012

Introduction to pointers

What is a pointer?

A pointer is a special type of variable that is used to hold the address of another variable or a pointer,pointer can be used to manipulate the value stored at that address.
Often it may be required to manipulate the value stored at a particular address, this is not possible by using normal variables, so we need pointers.(Read article on Pass by value&Pass by reference in general programming.)

Declaring a Pointer: 

Pointer variables are declared by prefixing variable name with * symbol.
ex: int *ipointer;
float *fpointer; etc


Assigning Value to a pointer/pointing it to a variable:

a pointer can be assigned the address of a variable as follows:
int *ipointer;
int k=15;
ipointer=&k;
the & operator is used to obtain the address of variable k.

Dereferencing a pointer/obtaining the value pointed by pointer:

this can be done by prefixing the pointer with *.
ex:
float *fpointer;
float  f=10.00;
fpointer=&f;
//use * to obtain the value pointed by pointer
printf("value pointed by fpointer:%f",*fpointer);

Important Note:

 * is used in two places when dealing with pointers
1. int* ip ; // here it means u are declaring a pointer to an integer.
2. int k=*ip; or printf(“%d”,*ip);  here it means dereferencing or fetching the value stored at the address pointed by pointer.

Sample Program:

#include<stdio.h>
void main()
{
//declaring a pointer
int *pointer;
int k=10;
//storing the address of variable k in a pointer
pointer=&k;
//the symbol & gives the address of variable k
printf("Getting the value From pointer\n");
//here * is used to dereference or obtain the value pointed by pointer
printf("K:%d",*pointer);
}

Two dimensional pointers:

two dimensional pointers are pointers to pointers or they can be considered as an array of pointers

ex1:pointer to a pointer 

char* str="hi im learning pointers";
char** strp=&str;
printf("%s",*strp);

here strp acts as a pointer to str which is pointing to the starting address of string "hi im learning pointers"

ex2 (complicated only for C++ ):

This concept is very useful when an array has to be populated using pass by reference
#include<iostream>
#include<conio.h>
void populatearray(int** mainarray,int* count)
{
    int a[]={1,2,3,4,5};
    //create a single dimension array for storing the values
    int* myarray=new int[5];
    //assign the values to be stored to the array
    for(int i=0;i<5;i++)
    {
        myarray[i]=a[i];
    }
    //store the count in the adress pointed by formal parameter
    *count=5;
    //store the array in the adress pointed by formal parameter
    *mainarray=myarray;
}
void main()
{   //the main array where values have to be stored
    int* arraymain=0;
    //count of elements
    int maincount=0;
    //pass the adess of pointer to array, adress of counter
    populatearray(&arraymain,&maincount);
    //check whether pass by reference has worked
    printf("The array Elements:\n");
    for(int i=0;i<maincount;i++)
    {
        printf("\n%d",arraymain[i]);
    }
    getch();
}

Thursday, 5 April 2012

Functions Pass By Value & Pass By Reference

We can pass arguments to a function in two ways:

Pass by Value:

Only the value of the variable is passed on to a function, this value is stored in a formal parameter of function, any manipulations done by the function will not be reflected back to the actual variable.

Pass by Reference:

Here the address of the variable is passed, this will be received by the formal argument which is a pointer, the formal argument acts as a reference to the original variable, so any changes made using the function will be reflected back to the actual parameter.

Example program:
Pass by Value


#include<stdio.h>
//function declaration
void add(int var1,int var2,int result);

//main function
void main()
{
 int result=0,var1=10,var2=20;
 printf("before passing by value:\nresult:%d",result);
 //pass the variable result by value
 add(var1,var2,result);
 printf("\nafter passing by value:\nresult:%d",result);

}
//function definition for add function
void add(int var1,int var2,int result)
{
result=var1+var2;
}

output:

before passing by value:
result:0
after passing by value:
result:0


we see that even though in the add function we are storing the sum of var1,var2 in result its not reflected in main function as the variables are passed by value.

Pass by Reference:


#include<stdio.h>
//function declaration
void add(int var1,int var2,int* result);

//main function
void main()
{
 int result=0,var1=10,var2=20;
 printf("before passing by Reference:\nresult:%d",result);

 //pass the variable result by Reference
 add(var1,var2,&result);
 printf("\nafter passing by Reference:\nresult:%d",result);

}
//function definition for add function
void add(int var1,int var2,int* result)
{
 *result=var1+var2;
}



before passing by Reference:
result:0
after passing by Reference:
result:30

we see that since the result was passed by reference, the changes done in the add function are reflected back to main function.

Figure showing pass by value&pass by reference



Monday, 2 April 2012

Programming approaches in C,C++

Basically there are two programming approaches in C, C++:
  1. Top Down Approach.
  2. Bottom Up Approach.

Top Down Approach:

This approach is known as top down as the main function is defined at the top, other user defined functions are defined below it.
ex:
//top down approach
#include
//function declaration
int add(int,int);
int sub(int,int);
//main function definition
void main()
{
int sum=0,difference=0,var1=0,var2=0;
printf("enter two variables to calculate the sum:\n");
scanf("%d%d",&var1,&var2);
//call to add function, main function is the calling function
sum=add(var1,var2);
//call to sub function
difference=sub(var1,var2);
printf("sum is:%d",sum);
printf("difference is:%d",difference);
}
//user defined functions are defined below main
int add(int value1,int value2)
{
return value1+value2;
}
int sub(int value1,int value2)
{
return value1-value2;
}

so when a function is called, control goes from top to bottom and returns to top again, ie from main function to the user defined function and back to main function again.


Bottom up approach:

This approach is known as bottom up as the main function is at the bottom and user defined function definitions are at the top.
ex:
//bottom up approach
#include
//user defined functions are defined above main
int add(int value1,int value2)
{
return value1+value2;
}
int sub(int value1,int value2)
{
return value1-value2;
}
//main function definition
void main()
{
int sum=0,difference=0,var1=0,var2=0;
printf("enter two variables to calculate the sum:\n");
scanf("%d%d",&var1,&var2);
//call to add function, main function is the calling function
sum=add(var1,var2);
//call to sub function
difference=sub(var1,var2);
printf("sum is:%d",sum);
printf("difference is:%d",difference);
}

so when a function is called, the control goes from bottom to top and returns to bottom again ie from main function to the user defined function and back to main function again.

Saturday, 31 March 2012

U Think U Cant Program

Many people(when they begin) think they cant write a computer Program, its some rocket science too complicated

Can I?

Myths:

  • I dont have knowledge of computers.
  • coding is too technical
  • Im not the right person to do it.

Break the Ice:

  • No one was/is born with the knowledge of computers.
  • Programming doesnt need extensive knowledge about computers.
  • If u are able to communicate with people(in any spoken or written language), i don't see why u should'nt be able to do the same with computers.

Hope you start programming:

The Peak Is'nt Far