A simple question

This is a discussion on A simple question within the C Programming forums, part of the General Programming Boards category; buf = (char*)GlobalAlloc(GPTR, len + 1); what does (char*) mean? does that mean return a char pointer?...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    85

    A simple question

    buf = (char*)GlobalAlloc(GPTR, len + 1);
    what does (char*) mean? does that mean return a char pointer?

  2. #2
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    It is called type-casting , it means that you want an object of a particular type to be interpreted as some other type without getting warnings or errors from the compiler.
    The one who says it cannot be done should never interrupt the one who is doing it.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    85
    Thanks
    but type-casting syntax of C is different from C++ ?

  4. #4
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >but type-casting syntax of C is different from C++ ?
    No, the syntax is the same. Anything between ( and ) prefixing a variable or a constant is 'Type Casting'
    Last edited by shaik786; 07-07-2002 at 03:48 AM.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    85
    is that mean by using (), we can change any type variable to any type variable ?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,855
    >is that mean by using (), we can change any type variable to any type variable?
    More or less. C offers a lot of freedom in casting, but don't expect your casts to always work as you expect. In my opinion, casts are usually a crutch for bad design and I try to only use them to clear Lint warnings when it's safe, such as:

    (void)puts ( "The void cast removes an annoying warning );

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    London, United Kingdom
    Posts
    225
    >> is that mean by using (), we can change any type variable to any type variable?

    By using casts, we can "coerce the compiler into thinking that a variable or constant is a different type from the one that you declared it to be or, as in the case of an expression, a different type than the one implied by its context. For that, you use a cast, which has this format :

    int *iptr = (int*) &table;

    These are the best uses of the cast - to suppress compiler warnings about thing that you do intentionally. Using the cast to override the compiler's limited type-checking facilities is a bad practice." - Al-Stevens Teaches C (BPB Publications)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21