Casting between int and pointer to char 
Author Message
 Casting between int and pointer to char

Im working on a project that Im trying to keep as portable as possible. The
following code works but is giving me a "assingment makes pointer from int
without a cast" error.

<code snippet>
      char *s;
      int i;

      i = (int) *s++;
</code snippet>

I dont understand why the cast I have used doesnt work. Shouldnt the value of
*s++ be evaluated then cast to int and then assinged to the variable i?

Thanks in advance.
Ryan

--
Who wants to remember that escape-x-alt-control-left shift-b puts you into
super-edit-debug-compile mode?
(Discussion in comp.os.linux.misc on the intuitiveness of commands, especially
Emacs.)
Ryan Mallon: Registered Linux user #282325



Mon, 09 May 2005 05:56:14 GMT  
 Casting between int and pointer to char

Quote:

>  Im working on a project that Im trying to keep as portable as possible. The
>  following code works but is giving me a "assingment makes pointer from int
>  without a cast" error.

>  <code snippet>
>        char *s;
>        int i;

>        i = (int) *s++;
>  </code snippet>

First: Post real, complete code, and post the real compiler diagnostic.
This snippet doesn't seem to warrant the diagnostic you mentioned.
Second: Only use casts if there is a real reason. There is no reason
here because the integral promotions and the assignment operator take
care of the appropriate conversions.

Gergo
--
You can create your own opportunities this week.  Blackmail a senior executive.



Mon, 09 May 2005 06:12:37 GMT  
 Casting between int and pointer to char

Quote:

> Im working on a project that Im trying to keep as portable as possible. The
> following code works but is giving me a "assingment makes pointer from int
> without a cast" error.

> <code snippet>
>       char *s;
>       int i;

>       i = (int) *s++;
> </code snippet>

> I dont understand why the cast I have used doesnt work. Shouldnt the value of
> *s++ be evaluated then cast to int and then assinged to the variable i?

    There is no integer-to-pointer conversion in this
code.  Also, the cast you've written is unnecessary;
the exact same effect would be obtained if you'd
omitted it.

    Methinks you've snipped away the actual offender
and presented us with an innocent bystander.

--



Mon, 09 May 2005 06:22:49 GMT  
 Casting between int and pointer to char

Are you sure this is piece of code where the compiler warning points to?
I am not getting anything though.
What you have done is this
*s is first cast to int ( which is not required) and assigned to i.after
that s is incremented to point to the next location. I am not sure if
that is what you intended.
-sridhar

--
Posted via http://dbforums.com



Mon, 09 May 2005 06:45:09 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. char pointers vs. int pointers

2. Unicode int to char casting

3. casting signed char to unsigned int

4. cast (char *) to (int *) ?

5. Casting an int to a char*?

6. int to char / char to int

7. char to int, int to char...

8. conversion int to char, char to int ?????

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

10. Pointer<->int casting

11. Casting a pointer to an int

12. Casting int to pointer and back again.

 

 
Powered by phpBB® Forum Software