question

Discussion in 'C Programming' started by chump1708@yahoo.com, Dec 15, 2005.

  1. Guest

    can anyone tell me what does the following mean??

    1.
    void* abc()
    {
    }

    is void a generic pointer??? and if yes what does it mean....


    2.
    also
    what does the function

    char ** abc()
    {
    }
    return.....
    i.e. does it return a 2d array of pointers????
    , Dec 15, 2005
    #1
    1. Advertising

  2. santosh Guest

    wrote:
    > can anyone tell me what does the following mean??
    >
    > 1.
    > void* abc()
    > {
    > }

    It means that function abc() returns a pointer of type void.

    > is void a generic pointer??? and if yes what does it mean....

    Yes.
    Assuming you know the type of object it points to, you can use it
    via a cast to access the object.

    > 2.
    > also
    > what does the function
    >
    > char ** abc()
    > {
    > }
    > return.....

    It returns a pointer to a pointer of type char.

    > i.e. does it return a 2d array of pointers????

    It could, among other things...
    santosh, Dec 15, 2005
    #2
    1. Advertising

  3. Guest

    well....regarding question 1, I guess we need not typecast it....
    regarding question 2, It returns a pointer to a pointer of type char -
    what do u exactly mean???


    do you mean
    that

    ptr1 -> some location

    ptr2-> address of ptr1

    Is this what u mean??
    , Dec 15, 2005
    #3
  4. santosh Guest

    wrote:
    > well....regarding question 1, I guess we need not typecast it....
    > regarding question 2, It returns a pointer to a pointer of type char -
    > what do u exactly mean???
    >
    >
    > do you mean
    > that
    >
    > ptr1 -> some location
    >
    > ptr2-> address of ptr1
    >
    > Is this what u mean??


    A Note: please give attribution to your posts.

    Firstly, you *must* typecast a void pointer or the compiler will report
    an error.
    Secondly, yes:
    If ptr1 is a pointer of type char and if ptr2 is a pointer of type char
    initialised
    with the address of ptr1, then ptr2 is a pointer to a pointer of type
    char.
    santosh, Dec 15, 2005
    #4
  5. Chris Dollin Guest

    santosh wrote:

    > wrote:
    >> can anyone tell me what does the following mean??
    >>
    >> 1.
    >> void* abc()
    >> {
    >> }

    > It means that function abc() returns a pointer of type void.
    >
    >> is void a generic pointer??? and if yes what does it mean....

    > Yes.


    No. /pointer to/ void is a generic pointer type.

    > Assuming you know the type of object it points to, you can use it
    > via a cast to access the object.


    Or an assignment. You don't /need/ a cast, although it might
    be the most appropriate thing to use.

    --
    Chris "Spice" Dollin
    oxygen is a highly reactive waste-product of plant life.
    Chris Dollin, Dec 15, 2005
    #5
  6. Chuck F. Guest

    wrote:
    > can anyone tell me what does the following mean??
    >
    > 1.
    > void* abc()
    > {
    > }
    >
    > is void a generic pointer??? and if yes what does it mean....
    >

    No, but void* is a generic pointer. Any pointer can be cast
    (explicitly or implicitly) to void* and back again.

    >
    > 2.
    > also
    > what does the function
    >
    > char ** abc()
    > {
    > }
    > return.....
    > i.e. does it return a 2d array of pointers????


    No. It returns a pointer to a pointer to char.

    --
    Some useful references about C:
    <http://www.ungerhu.com/jxh/clc.welcome.txt>
    <http://www.eskimo.com/~scs/C-faq/top.html>
    <http://benpfaff.org/writings/clc/off-topic.html>
    <http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
    <http://www.dinkumware.com/refxc.html> (C-library}
    <http://gcc.gnu.org/onlinedocs/> (GNU docs)
    Chuck F., Dec 15, 2005
    #6
  7. santosh wrote:
    > wrote:
    > > can anyone tell me what does the following mean??
    > >
    > > 1.
    > > void* abc()
    > > {
    > > }

    > It means that function abc() returns a pointer of type void.


    It is better said that abc returns a pointer to void.

    > > is void a generic pointer??? and if yes what does it mean....

    > Yes.
    > Assuming you know the type of object it points to, you can use it
    > via a cast to access the object.


    A *pointer to void* is a generic object pointer in at least one sense
    of the word, *void* is something completely different.

    > > 2.
    > > also
    > > what does the function
    > >
    > > char ** abc()
    > > {
    > > }
    > > return.....

    > It returns a pointer to a pointer of type char.
    >
    > > i.e. does it return a 2d array of pointers????

    > It could, among other things...


    Well, not, it couldn't. You don't return arrays at all in C. This
    function will return a pointer to pointer to char every time. The
    pointer it returns could be used to access an array but it isn't the
    array that is being returned.

    Robert Gamble
    Robert Gamble, Dec 15, 2005
    #7
  8. santosh Guest

    Robert Gamble wrote:
    > santosh wrote:
    > > wrote:

    <snip>
    > > > i.e. does it return a 2d array of pointers????

    > > It could, among other things...

    >
    > Well, not, it couldn't. You don't return arrays at all in C. This
    > function will return a pointer to pointer to char every time. The
    > pointer it returns could be used to access an array but it isn't the
    > array that is being returned.


    You're right. I meant to mean the same thing, but looking back, i can
    see, it's both misleading and ridiculous.

    I'll check my wording carefully before posting further.
    thanks
    santosh, Dec 15, 2005
    #8
  9. Alok Singhal Guest

    On Thu, 15 Dec 2005 06:06:17 -0800, santosh wrote:
    > Firstly, you *must* typecast a void pointer or the compiler will report
    > an error.


    No, one does not need to cast a void pointer to assign it to
    another pointer, or the other way around:

    6.5.16.1 Simple assignment
    Constraints
    One of the following shall hold:

    one operand is a pointer to an object or incomplete type and the other
    is a pointer to a qualified or unqualified version of void, and the type
    pointed to by the left has all the qualifiers of the type pointed to by
    the right;

    Also look at the example in section 6.5.3.4 (The sizeof operator) in the
    standard:

    extern void *alloc(size_t);
    double *dp = alloc(sizeof *dp);

    The above should be an error according to you.

    This is legal:

    void *vp;
    double d, *dp;
    vp = &d;
    dp = vp;

    -Alok
    Alok Singhal, Dec 15, 2005
    #9
  10. pete Guest

    santosh wrote:

    > Firstly, you *must* typecast a void pointer
    > or the compiler will report an error.


    If your compiler gives an error
    from not casting the return value of malloc,
    that means that you neglected to #include <stdlib.h>.

    http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html

    Errata for The C Programming Language, Second Edition

    142(ยง6.5, toward the end):
    The remark about casting the return value of malloc
    ("the proper method is to declare ... then explicitly coerce")
    needs to be rewritten. The example is correct and works,
    but the advice is debatable in the context of the
    1988-1989 ANSI/ISO standards. It's not necessary
    (given that coercion of void * to ALMOSTANYTYPE * is automatic),
    and possibly harmful if malloc, or a proxy for it,
    fails to be declared as returning void *.
    The explicit cast can cover up an unintended error.
    On the other hand, pre-ANSI, the cast was necessary,
    and it is in C++ also.

    --
    pete
    pete, Dec 15, 2005
    #10
  11. wrote:

    as others have commented, please leave some context in your posts
    I have inserted part of the post you replied to
    ***
    > 2.
    > also
    > what does the function


    > char ** abc()
    > {
    > }
    > return.....


    It returns a pointer to a pointer of type char.
    ***

    <snip>

    > regarding question 2, It returns a pointer to a pointer of type char -
    > what do u exactly mean???


    "you" is not spelt "u"

    > do you mean
    > that
    >
    > ptr1 -> some location
    >
    > ptr2-> address of ptr1
    >
    > Is this what u mean??


    consider

    void f (void)
    {
    char c = 'a';
    char *ptr1 = &c;
    char **ptr2 = &ptr1;
    }

    ptr1 is a ptr to char
    ptr2 is a ptr to a ptr to char

    try a good book about C for more details


    --
    Nick Keighley
    Nick Keighley, Dec 15, 2005
    #11
  12. "Robert Gamble" <> writes:
    > santosh wrote:
    >> wrote:
    >> > can anyone tell me what does the following mean??
    >> >
    >> > 1.
    >> > void* abc()
    >> > {
    >> > }

    >> It means that function abc() returns a pointer of type void.

    >
    > It is better said that abc returns a pointer to void.
    >
    >> > is void a generic pointer??? and if yes what does it mean....

    >> Yes.
    >> Assuming you know the type of object it points to, you can use it
    >> via a cast to access the object.

    >
    > A *pointer to void* is a generic object pointer in at least one sense
    > of the word, *void* is something completely different.


    Given that we're talking about pointers, I suggest that '*' wasn't the
    best character to use for emphasis.

    A <pointer to void> (type void*) is a generic object pointer in at
    least one sense of the word, <void> is something completely different.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 15, 2005
    #12
  13. "Chuck F. " <> writes:
    > wrote:
    >> can anyone tell me what does the following mean??
    >> 1.
    >> void* abc()
    >> {
    >> }
    >> is void a generic pointer??? and if yes what does it mean....
    >>

    > No, but void* is a generic pointer. Any pointer can be cast
    > (explicitly or implicitly) to void* and back again.


    Correction: any pointer can be *converted* (explicitly or implicitly)
    to void* and back again. A cast is an explicit conversion; there's no
    such thing as an implicit cast.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 15, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sean
    Replies:
    1
    Views:
    591
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    646
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    943
    Scott Allen
    Oct 6, 2005
  4. Philip Meyer
    Replies:
    0
    Views:
    417
    Philip Meyer
    Nov 30, 2003
  5. Bit Byte
    Replies:
    1
    Views:
    836
    Teemu Keiski
    Jan 28, 2007
Loading...

Share This Page