Basic Question - char *str V/S int * 
Author Message
 Basic Question - char *str V/S int *

Hi,

We can declare

char *str = "Hello World";

but the following needs to be cast.

int *a = (int *)10;

Why we can't declare int *a = 10;

Thanks in advance.

Regards
Nagaraj



Mon, 22 Aug 2005 15:15:16 GMT  
 Basic Question - char *str V/S int *

Quote:
> Hi,
> We can declare
> char *str = "Hello World";

This puts "Hello World" somewhere in memory and makes str point to
the first 'H'.

Quote:
> but the following needs to be cast.
> int *a = (int *)10;
> Why we can't declare int *a = 10;

Because "10" is not a valid address. It's an integer.

You cannot mix types "T" and "pointer to T". Note that "Hello World" is
an *array* of chars, not a char itself.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"The truth is out there, man! Way out there!"
   - Professor Ashfield



Mon, 22 Aug 2005 15:24:02 GMT  
 Basic Question - char *str V/S int *


Quote:
> Hi,

> We can declare

> char *str = "Hello World";

> but the following needs to be cast.

> int *a = (int *)10;

You're right that it needs to be cast; it needs to be cast out
of the code. Conversion from int to any pointer type is not defined
by the Standard. It may be defined by your implementation, but it
need not be the same way as in any other implementation. It very
rarely needs to be done at all [outside embedded systems].

Quote:
> Why we can't declare int *a = 10;

Because 10's type is not a pointer type.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html



Mon, 22 Aug 2005 16:29:16 GMT  
 Basic Question - char *str V/S int *

Quote:

> > char *str = "Hello World";

> This puts "Hello World" somewhere in memory and makes str point to
> the first 'H'.

Where's the second 'H'? ;p


Mon, 22 Aug 2005 18:22:47 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. char **str vs. char *str[]

2. char *str vs. char str[]

3. char *str vs. char s

4. DS==SS vs DS!=SS in a DLL

5. _T(str) vs (CString)str?

6. A char pointer (char *) vs. char array question

7. int to char / char to int

8. char to int, int to char...

9. conversion int to char, char to int ?????

10. char *fnpars(const char *fn, int p, int v)

11. char* and char str[2]

12. main() vs int main() vs int main(void)

 

 
Powered by phpBB® Forum Software