Thursday, December 18, 2008

Memory Allocation for a two dimensional array dynamically

0 comments
#include
#include

int allocate(float ***p, int m,int n)
{
int i;
*p=(float**)malloc(m*sizeof(float*));
for (i = 0; i < m ; i++)
{
(*p)[i]=(float*)malloc(n*sizeof(float));
}
if (*p==NULL)
{
return 0;
}
else
{
return 1;
}
}

int main()
{
int Row,Col,i,j;
float **Array;
printf("Enter the number od rows and volomns respectively\n");
scanf("%d %d",&Row,&Col);

if (allocate(&Array,Row,Col))
{
printf("array created. start filling it");
}
else
{
printf("memory not allocated. exiting program");
sleep(5);
exit(1);
}

for (i = 0; i < Row ; i++)
{
for (j = 0; j < Col ; j++)
{
printf("\na[%d][%d]=",i,j);
 scanf("%2f",&Array[i][j]);
}

}

for (i = 0; i < Row ; i++)
{
for (j = 0; j < Col ; j++)
{
printf("\nArray[%d][%d]=%f",i,j,Array[i][j]);
}

}

 getch();
 return 0;
}

Thursday, December 4, 2008

Dynamic Casting

0 comments
dynamic_cast

dynamic_cast is used to do downcasting, means to assign a base class pointer to it's derived class pointer.
Ex:
class base
{
};
class derived :: public base
{
}
base* b = new derived; //this is upcasting and allowed
but....
derived* d = b; //this is downcasting and not allowed directly
so here dynamic casting is used
derived* d = dynamic_cast(b);

A function with no arguments in the prototype

0 comments
A function with no arguments in the prototype

Consider we have a function with the following prototype
void fun();
How many arguments it will take in C and C++?

Ans:In C it can take any no of arguments, but in C++ it takes no argument.

Ex:
int main()
{
int i,j,k;
i = 1;
j = 2;
k = 3;
foo(); //valid in both C and C++
foo(i); //Valid in C but invalid in C++
foo(i ,j); //Valid in C but invalid in C++
foo(i,j,k); //Valid in C but invalid in C++
}
void foo()
{
cout<<"In function";
}

Anonymous Unions

0 comments
Anonymous Union

A Union with out a name is called Anonymous,means members of that union cannot be accessed directly.
Ex:
struct
{
char code;
union
{
char ch;
int num;
};
char *name;
} S={'j',97,"zyxwvu"};

int main()
{
int rslt;
rintf("structure members\n");
printf("code %c\nch is %c\nnum %d\nname %s\n",S.code,S.ch,S.num,S.name);
rslt = ({
int a = 5;
int b;
b = a + 3;
});
printf("rslt is %d\n",rslt);
getch();
return 0;
}

Hit Counter


Statistics

Followers

 

Programmer's Guide. Copyright 2008 All Rights Reserved