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 ;)
Related
This question already has answers here:
What is the difference between const int*, const int * const, and int const *?
(23 answers)
Closed 3 years ago.
I've been learning C, and there's something about type declarations that still isn't clear to me, and I haven't found a great answer yet. From what I'm seeing, the exact ordering of the type and modifiers doesn't seem all that important from a practical, pragmatic standpoint. For example, these lines below are all essentially equivalent (right?):
const int * np;
int const * np;
int * const np;
My question is, are these actually completely equivalent in the eyes of (say) the compiler, and/or are there some differences in them that could become important in some case(s)?
In reading through other's code, I find that there is a lot of variation in how people prefer to do these declarations, and I'm just trying to be sure I'm not missing something important.
The first two are the same, but the third is not.
In the first two cases, np is a pointer to a const int. So you can change np but not *np.
In the third case, np is a const pointer to int. So np cannot be changed but *np can.
This question already has answers here:
difference between int* i and int *i
(9 answers)
Why is the asterisk before the variable name, rather than after the type?
(12 answers)
Closed 3 years ago.
I have been trying to learn C as a programming language, and have been trying to solve sample problems on site like LeetCode in C programs. When I was reading over some of the skeleton code that was provided as a function header for a problem on LeetCode that I want to solve in C, the function header had asterisks post fixing some of the types, specifically like this:
int* twoSum(int* nums, int numSize, int target, int* returnSize) {
/* Code goes here */
}
After doing a fair bit of reading, I learned that prefixing a variable with an asterisk when declaring a variable reserves the variable as a pointer, but I have not been able to find anything about what it means when the type specifier itself is post fixed with an asterisk.
The spaces there don't matter.
int* nums is identical to int *nums. So are int * nums and int*nums.
All four of these declare nums as pointer to int.
It's a matter of style preference (though I wouldn't use that last one), with no effect on the generated code.
This question already has answers here:
What is the difference between the dot (.) operator and -> in C++? [duplicate]
(14 answers)
Closed 8 years ago.
i am trying to understand what is the difference when using structs and typedefs to access some components
what is the difference between using the dot operator when dealing with structs
using the example below
so far i have tried this code
typedef struct _game{
int something;
char something_else;
} g;
if i use
g.something or g->something
what is the difference?
i have used both of them and they both return results but i still dont understand the difference
can somebody please explain this?
I'm assuming this is C. When asking language questions tag the language. There are many languages that look the same and can give you subtly different answers. C++ is a different language than C, btw.
In this statement,
typedef struct _game { int something; } g;
g is a type, not a variable. As such, g.something makes no sense. typedef means "type define". Instead, you would have
g my_g_instance;
g *my_g_ptr = &my_g_instance;
my_g_instance.something = 2;
my_g_ptr->something = 5;
The difference between . and -> is whether the variable to the left of the operator is a pointer or not.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to understand complicated function declarations?
Spiral rule and ‘declaration follows usage’ for parsing C expressions
There is a section with the same title, "Complicated Declarations", in K&R's The C Programming Language book as you might have already read. I am just reading the book and trying to better myself in C language. After reading the section mentioned, I think I couldn't get the logic behind the syntax of C declaration statements. 1, 2, 3 and 4 are from that section 5 and 6 are from other pages.
int (*daytab)[13]
daytab: pointer to array[13] of int
void (*comp)()
comp: pointer to function returning void
char (*(*x())[])()
x: function returning pointer to array[] of pointer to function returning char
char (*(*x[3])())[5]
x: array[3] of pointer to function returning pointer to array[5] of
char
typedef int (*PFI)(char *, char *)
creates the type PFI, for ``pointer to function (of two char *
arguments) returning int. How does the syntax works here?
Finally, my questions are:
Can you explain your ways of thinking and reading complicated
declarations possibly by using examples above?
Are the things like
1,3,4 practically usable and needed? If so, can you write some code examples?
I saw The ``Clockwise/Spiral Rule'' on HackerNews in the past week or so. It is a good way to think about C declarations, especially function pointers.
Look at the identifier and the symbol next to it to the right:
If it's a [ the identifier is for an array.
If it is a ( the identifier is for a function
If it is a ) look to the left and you will find a *: the identifier is a pointer
If there is nothing to the right or to the left, the identifier is a "plain old" object.
Elaboration:
int (*daytab)[13]
daytab is a pointer
void (*comp)()
comp is a pointer
char (*(*x())[])()
x is a function
char (*(*x[3])())[5]
x is an array
typedef int (*PFI)(char *, char *)
PFI is a pointer
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.