questions on following statement

This is a discussion on questions on following statement within the C Programming forums, part of the General Programming Boards category; Hi All, I was to trying understand an source file ,I came across following line Code: void *pParam; *((char**)pParam) = ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    139

    questions on following statement

    Hi All,

    I was to trying understand an source file ,I came across following line

    Code:
    void *pParam;
    *((char**)pParam) = "SCHD_MODULE";
    Why is pParam typecasted to double char pointer?

    Thanks

  2. #2
    Epy
    Epy is online now
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    944
    I'm thinking that void is already a pointer (NULL pointer), so *pParam would be a double pointer. Casted to char** since it's a char array (string) and then dereferenced once to give a char* pointer.

    Equivalent:
    Code:
    char *pParam = NULL;
    *pParam = "SCHD_MODULE"; // read only
    Last edited by Epy; 07-29-2010 at 08:00 AM. Reason: clarification

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Epy View Post
    I'm thinking that void is already a pointer (NULL pointer),
    I'm not sure what you're trying to say here, but it's wrong. A void pointer isn't a NULL pointer, it's also not initialized to NULL. I don't know where you got NULL from, but there was nothing set to NULL in this example. It's a generic pointer, not a NULL pointer.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    I still didn't understand the need for typecasting it to double pointer....
    Last edited by sanddune008; 07-30-2010 at 12:37 AM.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    I still didn't understand the need for typecasting it to double pointer....

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Let's define out custom type:
    typedef char mytype_t,;
    Now we want to pass that variable to a function to modify it. In order to do that, we need to pass it by address, right?
    So,

    mtype_t mydata;
    foo(&mydata);

    And

    Code:
    void foo(mytype_t * data)
    {
        *data = 'H';
    }
    Easy. Right?
    Now let's redefine our type a little:

    typedef const char* mytype_t;

    And we once again pass it to a function:

    mtype_t mydata;
    foo(&mydata);

    And

    Code:
    void foo(mytype_t * data)
    {
        *data = "Hello World!"; // We probably should have malloc or something here, but this serves the example fine.
    }
    No problems! We've seen this stuff before.
    Now let's substitute mydata_t for the type it's an alias for, and we get:

    const char * mydata;
    foo(&mydata);

    And

    Code:
    void foo(char const* * data)
    {
        *data = "Hello World!"; // We probably should have malloc or something here, but this serves the example fine.
    }
    There you go. A double pointer.

    The reason for the casting is probably because the function takes an opaque pointer (ie void*). Thus it needs to convert it to its real type before doing something with it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. issue with switch statement
    By bluetxxth in forum C Programming
    Replies: 14
    Last Post: 02-24-2010, 09:01 AM
  2. Beginner question about the Return statement
    By lucidrave in forum C Programming
    Replies: 3
    Last Post: 08-07-2009, 05:19 PM
  3. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  4. Some C test questions: challenge
    By Mister C in forum C Programming
    Replies: 47
    Last Post: 09-10-2002, 06:47 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

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