What is the difference between the two expressions.int* a = 0;int* a = 10;

Questions by ykirankumarmca

Showing Answers 1 - 5 of 5 Answers

WHEN WE INITIALIZE  POINTER TYPE VARIABLE WITH O SO IT WILL TREAT IT LIKE ASSIGNING NULL POINTER SO IT WILL WORKS;

int * a= 0; // will work and initialized as a NULL pointer

BUT int *a = 10; //will not work coz it will treat like intializing int * variable   with const int value so it is not compatible so Compile Time Error Invoke.

SEE FOLLOWING CODE , ANY IDEA OR QUERY AND WANNA SHARE PLS WRITE ME AT : mohit.gonduley@gmail.com

**********************************************************

int main(int argc, char* argv[])
{
   int* a = 0; // THIS WILL ALLOW AND TREAT IT LIKE ASSIGNING NULL

   int* a1 = 10; //THIS STATEMENT WILL NOT ALLOW BECAUSE
                 //IT WILL TREAT IT LIKE ASSIGNING const int VALUE TO
                 // int * TYPE VARIABLE WHICH IS NOT POSSIBLE.

   return 0;
}

*********************************************************

int *a=0.

int *a = 10

First of all both of the statement would generate warnings, as pure integer numbers are not pointers, they need a proper casting for assigning to the pointers. In some compiler the given statements may generate compilation errors.

The proper statements would be:

int *a=(int *)0;

int *a = (int *)10;

I am giving the explanation of these statements, considering the casting has been made.

The first one indicates that the pointer a is pointing to the first byte of the memory and the second one indicates the pointer a points to the location 10 (that is byte no 10) of the data segment of the memory.

For your further information, to make the pointer initialization meaning full the first few bytes (4 bytes for tuboC ompiler) are initialized with 0. So that if we assign NULL to the pointer, then the bytes that it is pointing to will also hold 0.

Thus for the first case *a would also be 0.

  Was this answer useful?  Yes

HI SHIBAJI POUL

Hoping u are doing well...Very first tell me which C or C++ compiler you r using.

Now Dissection of ur comments :-

1) THESE ARE FEW LINES OF UR COMMENT

..............................................................

int *a=0;

int *a = 10;

First of all both of the statement would generate warnings, as pure integer numbers are not pointers, they need a proper casting for assigning to the pointers. In some compiler the given statements may generate compilation errors.

NOW MY QUERY :-

 I am agree with ur statement but only for  int *a = 10; // SHOW WARNING

 Will it show WARNING for int *a=0; statement ? atleast in my knowledge it should not show WARNING because it is equivalent to NULL. Thats reason I asked which compiler are u using , If u are using BORLAND TURBO Compiler I dnt know becasue this compiler is not belongs to  STANDARD ANSI  COMPILER LIKE VS 6.0 CL.EXE , OR LINUX gcc or g++  OR Zetalisp C compiler

........................................................................................................................

2) THESE  ARE ANOTHER  FEW LINES OF UR COMMENT

The proper statements would be:

int *a=(int *)0;

int *a = (int *)10;

I am giving the explanation of these statements, considering the casting has been made.

The first one indicates that the pointer a is pointing to the first byte of the memory and the second one indicates the pointer a points to the location 10 (that is byte no 10) of the data segment of the memory.

NOW MY QUERY

AS U wrote in ur comment

the second one indicates the pointer a points to the location 10 (that is byte no 10) of the data segment of the memory.

SO Really it will point to the location 10 of the Data Segment  Memory area od process as u firmly declare here?

-------------------------------------------------------------------------

3)ALSO IN UR COMMENT U HAVE MENTION 4 BYTES FOR POINTER IN TURBO COMPILER

SEE UR COMMENTS :

For your further information, to make the pointer initialization meaning full the first few bytes (4 bytes for tuboC ompiler) are initialized with 0. So that if we assign NULL to the pointer, then the bytes that it is pointing to will also hold 0.

Thus for the first case *a would also be 0.

NOW MY QUERY :-

Upto now I have used the Borland Turbo 1 to Turbo 3 C or c++  compiler ,it is 16 bit compiler which can run on 16 bit processor or 32 bit processor like pentium in DOS mode so how possible for for 16 bit supporting c or c++  compiler to  allocate  4 bytes. Or it is impliciately define far pointer ?........

Yeah , It is possible with MS CL comipler , gcc, g++ compiler to allocate 4 bytes to pointer  because they are made for 32 bit application development.

Please before writing any comments please try to write small code  in Borland Turbo C compiler. If any body like to check whether his/her compiler support 16 bit programming or 32 bit programming.

just write following program and RUN it , it will show you :

***********************************************

#include "stdio.h"

int main()
{
 int *a=0;
 printf("\n By This Compiler support :- %d bits programming\n",sizeof(a)*8);
 return 0;
}

**********************************************************

I am not here to showing or pointing fingure at any one , at any point I may be wrong , Only I like to share more uncovered knowledge so we can move toward perfection.......coz we will not be always kids.

Any wanna scold, suggets , advice or even have any query pls without any hesitation write at mohit.gonduley@gmail.com

.......................................................................................................

 

Shibaji Paul

  • Jul 28th, 2006
 

Dear Mohit,

Sorry for late reply. I was busy doing other important tasks.

You are correct,

int *a = 0;

won't generate any warning.

and for DOS it would be 2 bytes instead of 4 for pointers without any doubt, this was definitely a typing mistake.  

I would like to thank you for your valuable comments and for correcting my views.

Regards

Shibaji

  Was this answer useful?  Yes

ajin

  • Sep 4th, 2006
 

good

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions