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;
}

Tuesday, November 25, 2008

Frequently Asked Questions

0 comments
C FAQ.....
X86 Assembly Language FAQ
Memory Management Referenced FAQ 
JTAG FAQ
UNIX Socket FAQ

Friday, November 14, 2008

#define Vs typedef

0 comments
#define Vs typedef

1. A typedef declaration does not create a new type, it provides an alias to an existing type. of course, typedef is like #define, the difference is typedefs are interpreted by the compiler, and it can cope with textual substitutions that are beyond the capabilities of the preprocessor whereas #defines are handled by preprocessor.

2. typedef does typechecking.
In case of #define it will replace the macro with macro definition and then checks the type during the compilaton.

#define CHARP char*
typedef char* CHAR_P;
CHARP ch1,ch2;
CHAR_P ch3,ch4;
ch1 = "Character1"; //legal
ch2 = "Character2"; //illegal
ch2 = 's'; //legal
ch3 = "Character3"; //legal
ch4 = "Character4"; //legal

3. It is difficult to use #define to define a structure ,union or a class.










C Structures Vs C++ Structures

0 comments
C Structures Vs C++ Structures

1.structures in C don't have member functions

2.The defination of the structure in C is limited to within the module and cannot be initialized outside its scope.
Where as in C++ you can initialize the objects anywhere within the boundaries of the project.

3. In C++ structure label is typedeffed by default
Ex:
struct Mystruct
{
int i;
char c;
};
Mystruct Ms1; //illegal in C but legal in C++
struct Mystruct Ms2; //legal in both C and C++
typedef struct Urstruct
{
int I;
char C;
}US;
Urstruct Us1; //illegal in C but legal in C++
struct Urstruct Us2; //legal in both C and C++
US Us3; //legal in both C and C++
struct US Us4; //illegal in both C and C++

4. Size of empty structure is 0 in C but 1 in C++.

Friday, November 7, 2008

new vs malloc

0 comments
new vs malloc

new
1.new allocates memory and intializes it by calling constructor
2.new returns the pointer to the type which it has just allocated memory,no need to cast
for example: new int ---- This returns a integer pointer
3.new makes sure to allocate memory successfully before calling constructor so no need check whether the call is successfull or not
4.new is an operator.
malloc
1.malloc does only memory allocation,not intialization
2.malloc returns a void pointer,you have to cast it.
3.If malloc fails to allocate memory it returns a NULL pointer.so u must check the returned pointer.
4.malloc is a function

Enumerations(C enum Vs C++ enum)

0 comments
Difference b/w enum in C ans C++(C enums Vs C++ enums):
C Implementation Example
enum num
{
first ,
second ,
};
enum num urnum= first; //legal
enum num mynum = second; //legal
num urnum= first; //illegal
num mynum = second; //illegal
typedef enum colors
{
red,green,yellow
}COL;
colors mycolors = red; //illegal
colors urcolors = green; //illegal
colors ourcolors = yellow; //illegal
COL hiscolors = green; //legal
COL hercolors = yellow; //legal
enum COL hiscolors = green; //illegal
enum COL hercolors = yellow; //illegal
enum colors hiscolors = green; //legal
enum colors hercolors = yellow; //legal
hiscolors = 4; //legal
i = hiscolors; //legal

C++ Implementation Example
enum num
{
first ,
second ,
};
enum num urnum= first; //legal
enum num mynum = second; //legal
num urnum= first; //legal
num mynum = second; //legal
typedef enum colors
{
red,green,yellow
}COL;
colors mycolors = red; //Legal
colors urcolors = green; //Legal
colors ourcolors = yellow; //legal
COL hiscolors = green; //legal
COL hercolors = yellow; //legal
enum COL hiscolors = green; //illegal
enum COL hercolors = yellow; //illegal
enum colors hiscolors = green; //legal
enum colors hercolors = yellow; //legal
mycolors = 4; //illegal
int i = mycolors; //legal

Hit Counter


Statistics

Followers

 

Programmer's Guide. Copyright 2008 All Rights Reserved