Pointers

K

Keith Thompson

sake said:
hey
this doesn't work in my enviorment.

"doesn't work" is one of the less useful problem descriptions. *How*
doesn't it work? What behavior did you expect, what did you see, and
how do they differ?
*ptr is actually the value pointed to and ptr is the pointer.
you verify this implicitly by printing the value with *ptr.

Of course.

Ian did omit the "=" for the initializer, which would cause a
compile-time error; is that what you mean by "doesn't work"? If not,
try to compile and execute this program:

#include <stdio.h>
int main(void) {
int n = 42;
int *ptr = &n;
printf( "%d %d\n", n, *ptr );
return 0;
}

The output should be a single line of text, "42 42". If you get
anything else (including nothing), then we can discuss what's going
wrong.
 
K

Keith Thompson

sake said:
Keith Thompson said:
sake said:
int n 42;
int *ptr = &n;

declares ptr as a pointer to int and initialises it with the address of n.

printf( "%d %d\n", n, *ptr );

hey
this doesn't work in my enviorment.

"doesn't work" is one of the less useful problem descriptions. *How*
doesn't it work? What behavior did you expect, what did you see, and
how do they differ? [...]
Ian did omit the "=" for the initializer, which would cause a
compile-time error; is that what you mean by "doesn't work"? If not,
try to compile and execute this program:

#include <stdio.h>
int main(void) {
int n = 42;
int *ptr = &n;
printf( "%d %d\n", n, *ptr );
return 0;
}

The output should be a single line of text, "42 42". If you get
anything else (including nothing), then we can discuss what's going
wrong.

yes, my mistake, sorry.

I transcripted the code like that

...
int *val, no=20;
*val=&no ;
...

which didn't work. next time I'll double ckeck

That barely even resembles the code that Ian posted.

If you're going to tell us that some code posted here doesn't work,
please copy-and-paste the exact code before trying it, and tell us
what error messages you got. (Your version shouldn't even compile,
or at the very least should produce a compile-time warning.)
 
A

Anand Hariharan

I think that instead of creating the (void *) type,
it might have been better to have made a rule change
so that conversions to pointer to character types,
could be done without casts
in the kinds of situations
where the (void *) type doesn't need casts now.

How would that allow for "generic functions" (a la the comparison
function pointer argument passed to qsort)?

- Anand
 
S

Shao Miller

How would that allow for "generic functions" (a la the comparison
function pointer argument passed to qsort)?

What is the problem with it? The 'compar'-pointed function would take
'char *', then inside, you'd assign them to the actual
pointer-to-element type (by initializer or assignment).
 
S

Seebs

What is the problem with it? The 'compar'-pointed function would take
'char *', then inside, you'd assign them to the actual
pointer-to-element type (by initializer or assignment).

The problem is: How would you distinguish between "generic pointer" and
"pointer which expects to be pointed at individual bytes"? As in, if I'm
looking at the prototype of a function, can I tell just by looking whether
it thinks it's getting a stream of bytes or an arbitrary pointer to space
of unknown contents?

I like (void *) mostly for documentation reasons; it provides clarity of
intent.

-s
 

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,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top