question

C

chump1708

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????
 
S

santosh

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...
 
C

chump1708

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??
 
S

santosh

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.
 
C

Chris Dollin

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

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.
 
C

Chuck F.

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)
 
R

Robert Gamble

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

It is better said that abc returns a pointer to void.
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.
It returns a pointer to a pointer of type char.

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
 
S

santosh

Robert said:
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
 
A

Alok Singhal

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
 
P

pete

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.
 
N

Nick Keighley

(e-mail address removed) 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.
***

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
 
K

Keith Thompson

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


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.
 
K

Keith Thompson

Chuck F. said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top