how to declare a pointer in C syntax? [duplicate] - c

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C++ Pointer in Function
Are are these statements the same in C when declaring a pointer?
int *ptr;
int * ptr;
int* ptr;

As noted in the linked question, they are all the same (just make sure to use one consistently - especially when working with existing codebase).
The only 2 differences are:
The first format is what K&R uses. Many people default to K&R when talking about styling C code.
int* p, x; is misleading, since x is int and not int*, so be aware of that pitfall when using the 3rd syntax.

The three declarations are equivalent.
int *ptr; // Kernighan & Ritchie style
int* ptr; // Stroustrup style

Have you tried? Yes they are. I for my part always prefer the first way. But I will not start a new "formatting" war here ;-(

Yes. It's mainly a matter of preference, though in my experience most people prefer either the first or third.
Personally, I prefer the first, because of a siutation like:
int *ptr1, *ptr1;
whereas
int* ptr1, ptr2; /* ptr2 is NOT a pointer, needs another * before it to be one */
EDIT:
To be clear, one is not more correct than the other and you should choose which one makes the most sense to you. Above all, it's probably most important to use one consistently throughout your code.

Related

Why is the asterisk used before the each variable name instead of after the type? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 months ago.
Improve this question
This is not a duplicate of this question
I understand why it is better to declare variables like this:
int *var;
instead of
int* var
I understand that this avoids the ambiguous case of
int* var1, var2 where it would seem that var1 and var2 are both pointers to integers, but var2 is actually just a regular integer.
My question is why int* var1, var2 doesn't declare both variables as pointers to integers. Intuitively, I would think that int* would act like any other data type, where you would just have to use int* var1, var2 to declare both variables of type int*. In other words, why is C designed so that each variable needs an asterisk before it?
Intuitively, I would think that int* would act like any other data type
If you want to become comfortable with C, you may need to adjust your intuition! Types are at least a little bit complicated; in fact the type system is a lot like a little programming language of its own! int is a type, but "pointer" is a meta-type. You can have pointers to anything, even pointers to pointers.
In other words, why is C designed so that each [pointer] variable needs an asterisk before it?
Because, as explained in several of the answers to that other question, the scheme is that a declaration contains a "base type" and then some "declarators". In the case of arrays, pointers, and functions, the "declarator" is a little sample of whatever thing is going to have the base type. If I say
int i;
the declarator is just i, so I'm saying that i is going to be an int. No mystery there. But if I say
int a[10];
I'm saying that one element of the array I'm declaring — that is, when I later use the array by saying something like a[i] — is going to be an int. If I say
int f();
I'm saying that calling the function is going to return an int.
Finally, if I say
int *ip;
I'm saying that when I take the contents of this pointer, using the expression *ip, then what *ip is going to be is an int.
And of course I can string these together on the same line:
int i, a[10], f(), *ip;
Now, perhaps I'm just rehashing the answers at the other question, and not explaining why things were set up this way. The reason is that it makes it possible to declare arbitrarily-complicated derived types. We can have arrays, and pointers, and functions, and arrays of arrays, and pointers to arrays, and functions returning pointers, and pointers to functions, and arrays of pointers to functions, and functions returning pointers to functions, and types even more numerous and complicated than those, and they can all be declared (in a general and open-ended way) using the same sort of concise, punctuation-rich, unwordy syntax that C is famous for
I don't think it would have been possible to design a type system that could have expressed the concept of "array of pointers to functions" on the left, leaving you to just put the variable name on the right. Or, if you could design such a system, it would have been complicated and confusing and cumbersome to use, even more complicated and confusing than C's present scheme seems to you, with its odd combination of type names on the left, and declarators — with their mixture of names, asterisks, and other punctuation — on the right.
See also Question 1.21 in the C FAQ list.
Or, perhaps I lied. I just said, "I don't think it would have been possible to design a type system that could have expressed [complicated things] on the left, leaving you to just put the variable name on the right." But, in fact, C's typedef facility lets you do precisely that, if you want to build up a complex type in stages.
For example, if you say
typedef int *pointer_to_int;
you have arranged that the identifier pointer_to_int is not an actual variable of type int *, but rather, a synonym for the type int *. Having done that, you can declare several integer pointers at once, without having to re-type those pesky asterisks:
pointer_to_int p1, p2, p3; /* just like int *p1, *p2, *p3; */
And it works for more complicated types, too. I can say
typedef int *(*pointer_to_function_returning_pointer_to_int)();
and then do
pointer_to_function_returning_pointer_to_int x1, x2, x3;
at which point x1, x2, and x3 are all pointers to functions returning pointers to int, just as if I'd said
int *(*x1)(), *(*x2)(), *(*x3)();
In fact, the CS50 course introduces a "handy" typedef
typedef char *string;
which makes the identifier string be an alias for the type "pointer to char", which allegedly makes it easier for beginners to declare lots of strings. Unfortunately, char * isn't really C's One True String type, so the string typedef probably causes as much confusion as it attempts to resolve. (Part of the problem is that pointers do not necessarily make good "opaque" types. If you're dealing with a pointer, you often need to know that you're dealing with a pointer, so hiding it behind a typedef doesn't help.)

How is a blank(s) treated in C? [duplicate]

This question already has answers here:
Placement of the asterisk in pointer declarations
(14 answers)
Difference between int* p and int *p declaration [duplicate]
(3 answers)
difference between int* i and int *i
(9 answers)
Closed 4 years ago.
What is the most proper way to place asterisk? Why?
1) type* var;
2) type *var;
It does not matter as far as you are declaring only one pointer. It is usually writen like in the second example (in the code I usually read/write) but for the compiler it's the same.
The trouble can come out if you are declaring more than one pointer. For example, this is not declaring two pointer, instead it declares one pointer and one var of type type.
type* var1, var2;
You need to do instead:
type* var1, *var2;
I prefer to use the * by the var always.
Pointer is the type, and I think it makes most sense to group the type information:
int* foo;
This can lead to confusion if several variables are defined on the same line:
int* foo, bar; // foo will be int*, bar will be int
The solution to this is, never declare several variables on the same line. Something that Code Complete advocates, anyway.
Both work. I'd argue that #1 is clearer in general, but misleading in C and can lead to errors, e.g.:
type* var1, var2;
// This actually means:
type *var1;
type var2;
So I'd say that #2 is more idiomatic in C and therefore recommended, especially if you are not the only programmer working on the code (unless, of course, you all agree on a style).
As others have pointed out, both work fine, and the wrong one actually has the advantage of syntactic economy.
The way I look at it, a declaration consists of type followed by the variable.
<type> <variable>;
int ii;
int* pii;
So, if you take away the variable, what remains is the type. Which is int and int* above. The compiler treats int* as a type internally (which is pointer to an int).
C, unfortunately, does not support this syntax seamlessly. When declaring multiple variables of the same type, you should be able to do this:
<type> <variable>, <variable>, <variable>;
which you cannot with a pointer type:
int* ii1, ii2, ii3;
declares ii1 of type int* and the rest of type int. To avoid this, I make it a habit of declaring only one variable per line.
I've heard it argued that technically the * is a modifier to the variable,
and this is evidenced by the need to use * multiple times in multi variable
declarations
eg. int *p, *q, *r;
However I like to think of it as a modifier to the base type because that
is what appears in prototypes.
eg. void func(int*);
.PMCD.
PS. I know I haven't helped your problem :)
The second mode is correct. The other mode are not so clear for a novice programmer.
The form int* variable is usually discouraged
That would rather be a coder's preference.
For me, I declare pointers by:
int * var1, var2;
Wherein var1 is a pointer, and var2 is not. If you want to declare multiple pointers in a line:
int * var1, * var2;
And of course, using the other ways are valid.
Declaration semantics follow expression semantics (specifically operator precedence) and I find more complex declarations easier to read using the second style.
You can think of
int *foo;
declaring the type of the expression *foo (ie indirection operator applied to foo) to be int instead of foo being declared to be of type int *.
Whatever convention you choose, just try to be consistent.
There is no single "most proper place".
Usually it is the place the rest of the code uses.
If you're writing your own code from the beginning and can choose your own convention: choose your own convention and stick with it.
If you choose something other than type * var you will meet 'awkward' situations:
/* write these with another style */
int * var;
int const * var;
int * restrict var;
PS. It shouldn't matter, but I usually use type *var;

Pointer syntax in C [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In C, what is the correct syntax for declaring pointers?
good way to write “pointer to something” in C/C++
I am currently reading a book on C. At the moment I read about pointers.
Basically, I think that I have understood the concept. Anyway one thing puzzles me:
Sometimes the author uses
void *foo;
to create a new pointer, but sometimes it's
void* foo;
Is there a difference? Does it matter? Doesn't it? When to use what? ...?
You can have as many or as few spaces either side of the * as you like. It means exactly the same thing either way:
void*foo;
void *foo;
void* foo;
void * foo;
void * foo;
void * foo;
void * foo;
All are exactly the same thing. If you work on a project, there is usually some "coding style standard" that explains which is preferred in that project. But the compiler won't do any different based on if/where there are spaces.
They are equivalent but the former can be preferable when declaring multiple variables of non-void type in a single line
int *foo, *bar;
clearly declares two pointers, while
int* foo, bar;
declares one pointer and one int
Both are equivalent and no difference whatsoever.
int *foo, i;
But you should know the difference in declarations like above. Here only foo is of pointer type whereas i is of type int (not int*).
The two syntaxes are equivalent. The only difference is a matter of style.
There is no difference to compiler, as you probably already know.
As of the matter of style, some argue that void* ptr is better because the type name as a whole is isolated. The problem is, that's not how the language works:
int* ptr, otherptr; /* otherptr is not a pointer here */
Even if you decide against declaring multiple variables at once, the whole idea of having an isolated type name breaks for arrays and function pointers. That's why I prefer the other style:
int *ptr, *otherptr, dontDeclareTooManyThings[N];
Is there a difference?
Of course there is. (If there wasn't, you wouldn't be asking this.) The star is on one side of the whitespace in the first case, and on the other side in the other.
Does it matter?
It does. Not in terms of program logic, however - the two are parsed exactly the same way. However, it's a matter of personal preference. I generally recommend the second way, i. e. void *ptr, since the pointer qualifier (the asterisk) modifies the variable, and not the type.
When to use what?
Just explained.
Both variants are valid.
They represent different styles.
consider
char* foo, bar;
char *foo, bar;
in both cases foo is a pointer, bar is not.
also consider (readability)
char* foo() {
// return something
}

Pointer C - declaration [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In C, what is the correct syntax for declaring pointers?
I am fighting with the c language. Pointers are new to me, and I think I am getting closer and closer to understanding them.
I have though one questions.
What is the difference between:
int k = 4;
int* pcp = &k;
and
int k = 4;
int *pcp = &k;
I cant seem to find any difference between these declarations of the pointer, is it just syntactical sugar - or is there any difference?
Thanks
There is no difference in those declarations, but there is a difference between the following two declarations:
int* p, p2; // declares a pointer to int and a regular int
and:
int *p, *p2; // declares two pointers to int
that might be hidden by your example.
So I prefer the second declaration.
you can try it out yourself. just type both, printf it and see what happens :P
if i'm not terribly mistaken though, it's the same ;)

C - initialization of pointers, asterisk position [duplicate]

This question already has answers here:
Placement of the asterisk in pointer declarations
(14 answers)
Difference between int* p and int *p declaration [duplicate]
(3 answers)
difference between int* i and int *i
(9 answers)
Closed 4 years ago.
What is the most proper way to place asterisk? Why?
1) type* var;
2) type *var;
It does not matter as far as you are declaring only one pointer. It is usually writen like in the second example (in the code I usually read/write) but for the compiler it's the same.
The trouble can come out if you are declaring more than one pointer. For example, this is not declaring two pointer, instead it declares one pointer and one var of type type.
type* var1, var2;
You need to do instead:
type* var1, *var2;
I prefer to use the * by the var always.
Pointer is the type, and I think it makes most sense to group the type information:
int* foo;
This can lead to confusion if several variables are defined on the same line:
int* foo, bar; // foo will be int*, bar will be int
The solution to this is, never declare several variables on the same line. Something that Code Complete advocates, anyway.
Both work. I'd argue that #1 is clearer in general, but misleading in C and can lead to errors, e.g.:
type* var1, var2;
// This actually means:
type *var1;
type var2;
So I'd say that #2 is more idiomatic in C and therefore recommended, especially if you are not the only programmer working on the code (unless, of course, you all agree on a style).
As others have pointed out, both work fine, and the wrong one actually has the advantage of syntactic economy.
The way I look at it, a declaration consists of type followed by the variable.
<type> <variable>;
int ii;
int* pii;
So, if you take away the variable, what remains is the type. Which is int and int* above. The compiler treats int* as a type internally (which is pointer to an int).
C, unfortunately, does not support this syntax seamlessly. When declaring multiple variables of the same type, you should be able to do this:
<type> <variable>, <variable>, <variable>;
which you cannot with a pointer type:
int* ii1, ii2, ii3;
declares ii1 of type int* and the rest of type int. To avoid this, I make it a habit of declaring only one variable per line.
I've heard it argued that technically the * is a modifier to the variable,
and this is evidenced by the need to use * multiple times in multi variable
declarations
eg. int *p, *q, *r;
However I like to think of it as a modifier to the base type because that
is what appears in prototypes.
eg. void func(int*);
.PMCD.
PS. I know I haven't helped your problem :)
The second mode is correct. The other mode are not so clear for a novice programmer.
The form int* variable is usually discouraged
That would rather be a coder's preference.
For me, I declare pointers by:
int * var1, var2;
Wherein var1 is a pointer, and var2 is not. If you want to declare multiple pointers in a line:
int * var1, * var2;
And of course, using the other ways are valid.
Declaration semantics follow expression semantics (specifically operator precedence) and I find more complex declarations easier to read using the second style.
You can think of
int *foo;
declaring the type of the expression *foo (ie indirection operator applied to foo) to be int instead of foo being declared to be of type int *.
Whatever convention you choose, just try to be consistent.
There is no single "most proper place".
Usually it is the place the rest of the code uses.
If you're writing your own code from the beginning and can choose your own convention: choose your own convention and stick with it.
If you choose something other than type * var you will meet 'awkward' situations:
/* write these with another style */
int * var;
int const * var;
int * restrict var;
PS. It shouldn't matter, but I usually use type *var;

Resources