Pointers

B

ByteSurfer

I am realy confussed bout pointers.. Can you guys please tell me what
is the difference or is there any kinds of pointers ?

char** a;
char *a;
*char a;

etc if there is any that i don't know or i am wrong.

Thanks

Regards,
Billy
 
B

bjrnove

*char a; IS NOT VALID AT ALL.

Look at this example:

char chr = 'a'; /* Just another char */
char* pchr = &chr; /* A pointer to the char above */
char** ppchr = &pchr; /* A pointer to the pointer to the char */
char*** pppchr = &ppchr; /* A pointer to a pointer to a pointer to a
char */

This can go on for a while (your code will probably crash after added
tons of *, but there doesn't have to be a limit. It's all compiler and
machine specific.)

Now you can use this pointers. Adding a * in front of a pointer (not
when declaring it, but ever after) will give you the stuff inside a
pointer. You could therfor do something like this:

printf("The char chr is: %c\n", chr);
printf("The char pointed to by pchr is: %c\n", *pchr);
printf("The char pointed to by ppchr is: %c\n", **ppchr);
printf("The char pointed to by pppchr is: %c\n", ***pppchr);

Now I know this may sound a bit confusing, but when you understand
pointers it wil be much clearer. I can recomend you to read this
chapter in a online book on c:
http://publications.gbdirect.co.uk/c_book/chapter5/
It will basicly try to teach you everything about pointers.

One last thing. Usaly you never need more than a pointer and in some
cases a pointer to a pointer. It's actualy pretty weird to see a
pointer to a pointer to a pointer.
 
B

ByteSurfer

some times is am kinda confused.
they like to code

char* a;
char *a;

is that both are of the same ??

and some more is it :

*const char a;
-> a pointer of a constant address to a character

const char* a; or const char *a;
-> a constant value of a char.

then how bout *const char* a;

will the spaces char* and char * the same ??
 
B

bjrnove

some times is am kinda confused.
they like to code

char* a;
char *a;

is that both are of the same ?? Yes.


and some more is it :

*const char a;
-> a pointer of a constant address to a character
No. Its an error. You want:
char const * a;
const char* a; or const char *a;
-> a constant value of a char.
It's a pointer to a char. NOT a char.
char a; is a char.
then how bout *const char* a;
Not valid.
char* const* a; is valid thow.
will the spaces char* and char * the same ??
Yes.
char * a;
is the same as
char* a;

Spaces doesn't mather as long as you have one space it's a space. The
rest is ignored by the compiler.
 
J

Jens.Toerring

ByteSurfer said:
some times is am kinda confused.
they like to code
char* a;
char *a;
is that both are of the same ??

Yes, it doesn't matter if you have white space before or after the '*'.
There's also the third and fourth possibility:

char * a;
char*a;

That shows it doesn't matter at all where and how much white space
you put before ot after the asterisk.

Some people prefer 'char*' to 'char *' because they feel that it
makes it more obvious that the variable to be defined is a pointer,
while others don't like it because they feel that it makes it easier
to overlook that in

char* a, b;

'b' isn't a pointer but a simple char variable despite the '*' after
the 'char'.
and some more is it :
*const char a;
-> a pointer of a constant address to a character

No, that's a syntax error. You can't have a '*' at the start when
defining a variable.
const char* a; or const char *a;
-> a constant value of a char.

No, that's a pointer to an array of chars with the attached promise
that you won't change the contents of what the pointer points to.
This is quite useful when you use pointers that point to literal
strings, i.e.

const char *my_hello = "Hello, boys and girls!";

because the compiler may be able to gently remind you you're doing
something stupid when you don't keep your promise, or when you use
this for function arguments to indicate that the function is not
going to change the elements of the array it receives. An example is

char *strcpy( char *dest, const char *src );

This makes it immediately clear that strcpy() will only change the
destination string, but will leave the source string alone. And
again it doesn't matter if you have white space before or after
the '*' or before and after the asterisk or none at all.
then how bout *const char* a;
will the spaces char* and char * the same ??

That's also a syntax error.
Regards, Jens
 
R

Randy Yates

while others don't like it because they feel that it makes it easier
to overlook that in

char* a, b;

'b' isn't a pointer but a simple char variable despite the '*' after
the 'char'.

This is a flaw in the C standard, in my opinion. Logically the "*" is
a modifier of the type. To associate it with the identifier rather
than the type is to confuse its function.
 
R

Randy Howard

This is a flaw in the C standard, in my opinion. Logically the "*" is
a modifier of the type. To associate it with the identifier rather
than the type is to confuse its function.

No, it's a flaw in the way people use it.

If they did it correctly, I.e.

char *a, b;

or even better

char *a;
char b;

then it would be obvious. Multiple variables on one line isn't
necessarily lazy, but it does lead to a lot of strange bugs,
particularly for those not already fluent in the language.
 
K

Keith Thompson

Randy Yates said:
This is a flaw in the C standard, in my opinion. Logically the "*" is
a modifier of the type. To associate it with the identifier rather
than the type is to confuse its function.

The principle is that declaration mimics use (more or less). Specifically,

char *a;

means that "*a" is of type char (from which you can infer that a is of
type pointer-to-char). Similarly,

char arr[10];

means that arr[whatever] is of type char (from which you can infer
that arr is of type array-of-char).

Yes, it can be confusing, and if you could only apply a single "*" or
"[...]" to a type name it would be silly, but you need to understand
the principle (or have a copy of the cdecl program) to be able to
decipher things like

void (*signal(int sig, void (*func)(int)))(int);

It's often helpful to avoid putting too many things on one line,
either by declaring only one variable per line or by using
intermediate typedefs.
 
M

Mark McIntyre

some times is am kinda confused.
they like to code

char* a;
char *a;

is that both are of the same ??

yes. C doesn't care where you put the whitespace.

char * a;
is also the same.
and some more is it :

*const char a;

synax error. The * must come AFTER a type.
const char* a; or const char *a;
-> a constant value of a char.

pointer to a char that is const
then how bout *const char* a;

syntax error.
will the spaces char* and char * the same ??

whitespace is ignored.

Read expressions right to left

const char* const x;

"x is a const pointer to a char that is const. "

(I hope....)
 
B

ByteSurfer

char chr = 'a'; /* Just another char */
char* pchr = &chr; /* A pointer to the char above */

using only one pointer like as you stated above than using a pointer to
a pointer to a char ... what will be the benifit and what is the use ??

char** ppchr = &pchr; /* A pointer to the pointer to the char */
char*** pppchr = &ppchr; /* A pointer to a pointer to a pointer to a
char */

some more when i am doing a function and then i thought eg.
start(*const char *a); will be valid ..
 
T

Thomas Stegen

Randy said:
No, it's a flaw in the way people use it.

If they did it correctly, I.e.

char *a, b;

That is no more correct than

char* a, b
then it would be obvious. Multiple variables on one line isn't
necessarily lazy, but it does lead to a lot of strange bugs,
particularly for those not already fluent in the language.

Hence it is a problem in the language.

It is the whole declaration mimics use. And it is not really
a very smart thing always.

If, as you say, it is "incorrect" why allow it in the language in the
first place?
 
R

Randy Yates

ByteSurfer said:
using only one pointer like as you stated above than using a pointer to
a pointer to a char ... what will be the benifit and what is the use ??

Consider, instead of an array of characters (i.e., a "word"), an
array of character strings (i.e., a "paragraph"). Then each element
in the array is not a character but a pointer to the first of an array
of characters, and therefore the identifier (the variable name) of the
array is a pointer to a pointer.

char* word = "country";
char** wordArray = {"Now", "is", "the", "time", "for", "all", "good",
"men", "to", "come", "to", "the", "aid", "of",
"their", "country"};
 
J

Jarno A Wuolijoki

This is a flaw in the C standard, in my opinion. Logically the "*" is
a modifier of the type. To associate it with the identifier rather
than the type is to confuse its function.

If you're ever going to "fix" the flaw (by developing a new language,
bribing the committee, or something..), please don't make it like:
char* a;
but
&char a;
instead.

The odd reverse syntax of C makes sense precisely because you're
declaring that something_that_looks_like_an_expression will be of type
base_type instead of plain_identifier is of some_complex_derived_type.

(of course to get this right with function pointers, you need to have
a syntax for constructing functions on the fly, but that's just a good
thing;)
 
O

Old Wolf

Thomas said:
That is no more correct than

char* a, b

Currently, the order of items in a declaration list doesn't
matter; these are all the same:

char a, b, *c, d();
char d(), b, a, *c;
char *c, d(), a, b;
char b, *c, d(), a;

Are you really suggesting that it would have been better
if one of those lines did something different to the others?
 
J

john_bode

ByteSurfer said:
using only one pointer like as you stated above than using a pointer to
a pointer to a char ... what will be the benifit and what is the use ??

Double indirection comes into play several places. For example, if you
create a function that has to write a pointer value to one of its
parameters, you have to pass a pointer to that pointer. For example,
take the strtol() function in the standard library:

long strtol(const char *str, char **ptr, int base);

What strtol() does is take a string of digit characters pointed to by
str and convert them to the equivalent integral value (e.g., "1234" =>
1,234). It will write the address of the first character that is not a
digit to ptr. Since we're writing to a parameter, we must pass its
address (a pointer value); since that parameter is of type pointer to
char, we must pass a pointer to pointer to char.

You'll also see double and higher indirection when dealing with
multidimensional arrays.
some more when i am doing a function and then i thought eg.
start(*const char *a); will be valid ..

No. What are you trying to convey here? Here are some examples that
may help:

char *a; // a and what it points to are both writeable
const char *a; // a is writeable, what it points to is not
writeable
char * const a; // a is not writeable, what it points to is
writeable
const char * const a; // neither a nor what it points to are writeable
 
B

ByteSurfer

Randy said:
use ??

Consider, instead of an array of characters (i.e., a "word"), an
array of character strings (i.e., a "paragraph"). Then each element
in the array is not a character but a pointer to the first of an array
of characters, and therefore the identifier (the variable name) of the
array is a pointer to a pointer.

char* word = "country";
char** wordArray = {"Now", "is", "the", "time", "for", "all", "good",
"men", "to", "come", "to", "the", "aid", "of",
"their", "country"};

Is that mean when i just have one pointer to a type i will point to the
first memory location of it. when i have type ** identifier ; i will be
pointing to the memory location of it and the memory will contain
another pointer that point to a new identifier.

then it will be the same of
char * word[] = "{...............};
char ** word;

what make the difference between this two?? or the theories behind
using them is just the same .? then in char ** word; how can i know, i
can put in how many elements in it ?
 
K

Keith Thompson

Thomas Stegen said:
That is no more correct than

char* a, b

Of course it's no more or less correct; since the amount of whitespace
is insignificant, they're identical.

It's "incorrect" only if the programmer's intent was to declare both a
and b as pointer-to-char objects. If the intent was to declare a as a
pointer-to-char and b as a char, it's perfectly correct (though it
would be clearer as two separate declarations).
Hence it is a problem in the language.

It is the whole declaration mimics use. And it is not really
a very smart thing always.

Any declaration scheme is going to have problems in some cases. C's
can be confusing, but it's consistent. Like anything else, you can
use it correctly if and only if you know how. And it cannot be fixed
without either breaking tons of existing code or inventing a new
language that doesn't pretend to be compatible with C.
If, as you say, it is "incorrect" why allow it in the language in the
first place?

No language can prevent logical errors.
 
R

Randy Yates

Keith Thompson said:
[...]
No language can prevent logical errors.

Perhaps not, but it can go a long way towards avoiding the types of errors
that humans tend to make.
 

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

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,422
Latest member
LatashiaZc

Latest Threads

Top