int *p vs int* p Pointer Declarations | C Programming Tutorial

  Рет қаралды 134,655

Portfolio Courses

Portfolio Courses

Жыл бұрын

A discussion about the different ways to declare pointer variables in C, including how one approach could lead to confusion and bugs if we try to declare multiple pointer variables with a single statement. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!

Пікірлер: 560
@PortfolioCourses
@PortfolioCourses
The comments suggesting declaring one variable per line as the better/ultimate solution are spot on, I've made this video covering the advantages of this style:
@TheBunzinator
@TheBunzinator
I've always considered this to be a failure of the language definition. An int is clearly a different data type than a pointer to an int, so the * should be part of the type, not the variable name. Therefore, I always use int* i;, and never do multiple variable decls on the same line.
@danielsass4134
@danielsass4134
I have always preferred int* p, because when I first learned C, I would get confused between declaring a pointer to an integer and dereferencing a pointer. It was easier in my head to remember that int* is a pointer to an integer and *p is dereferencing a pointer. Nevertheless, I understand your point-of-view and neither notation bothers me anymore.
@paulathomas4944
@paulathomas4944
Whitespace is ignored by the C compiler as anyone who's taken the time to read K&R knows only too well. So "int
@K9Megahertz
@K9Megahertz
C does not care about whitespace. Put as many spaces or as few as you want or none at all.
@lellyparker
@lellyparker
Declaring multiple variable in a single statement is less clear and readable than simply using a single statement to declare each variable. I personally would not recommend doing that.
@finmat95
@finmat95
The code must be readable and clear. int* ftw.
@D-V-O-R-A-K
@D-V-O-R-A-K
Seems like another faulty standard behaviour
@Schmelon
@Schmelon
p2 is the clearest because it explains the type. the reasoning for p1 is bullshit
@sitting_nut
@sitting_nut
int *p1; should be read as "dereferenced p1 is an int" .then confusion goes away . and also * remains just the dereference operator and not a separate spacial pointer declaration use case .
@karlostj4683
@karlostj4683
The compiler (gcc) only applies the '*' to the variable name immediately following it in the declaration. The solution to this is: Only declare one variable at a time - anything else is sloppy, which programmers should avoid like the plague.
@bewingbewing
@bewingbewing
As old (or, excuse me, mature) as C is, there's still no end of the mayhem you can create! Or, as my friend says: C is happy to let you put it into reverse at 60 MPH. Thanks for this clear and concise video.
@cccmmm1234
@cccmmm1234
I've been using C for 40 years. I pretty much always use int *p.
@UsernameUsername0000
@UsernameUsername0000
I’m a C++ programmer and it’s not a regular thing for me to need to define more than two raw pointers or references on the same line, so I always stick to int* and int&.
@adruzgatsby
@adruzgatsby
I'm seeing a lot of comments in favor of "int* p" and I use to be in that camp because I thought that p was a variable of type pointer to an integer. The videos in this channel are great, but this particular video doesn't fully explain the why "int *p" is actually the "correct" way to write a pointer and why it's written that way. I, like most of the folks here in the comment section came from a higher level language first while you have to understand that C was created in a time when many people were still writing assembly and C was considered a high level language! Modern languages today have sophisticated type systems while C's "trust me bro" type system with all it's casting magic (a feature, not a bug) really just specifies width. You are expecting more from the type system than you really should.
@MrGeorge1896
@MrGeorge1896
Just a small remark on the use of printf("%d ", sizeof(some_type)): In most environments the type of sizeof() is not int so the %d conversion specifier raises an error or at least a warning. The correct conversion specifier is %zu. z for the type of sizeof() which is defined as size_t and u as we are dealing with an unsigned value.
@karimshariff7379
@karimshariff7379 Жыл бұрын
Thanks for this. As a beginner I would be tempted to use int* p1, p2 thinking that both p1 and p2 would be declared as pointers. Really appreciate this video. Oh, I have also done void main so I am watching that video next.
@marccygnus
@marccygnus
Ive been doing embedded work for more than two decades. Some of it has been in safety critical systems. I've always used "int * p1" (space on either side of *). And I never declare multiple variables on one line. (C++ too.) I personally find the other two annoying to read.
@aurinator
@aurinator
Thanks for this quick video, I remember wondering this exact thing while learning.
@ProtossOP
@ProtossOP
I’ve rationalised it like this when I was learning:
Convert A For Loop To A While Loop | C Programming Tutorial
3:12
Portfolio Courses
Рет қаралды 8 М.
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
Mama vs Son vs Daddy 😭🤣
00:13
DADDYSON SHOW
Рет қаралды 48 МЛН
Best Toilet Gadgets and #Hacks you must try!!💩💩
00:49
Poly Holy Yow
Рет қаралды 22 МЛН
КАК ДУМАЕТЕ КТО ВЫЙГРАЕТ😂
00:29
МЯТНАЯ ФАНТА
Рет қаралды 10 МЛН
Function Pointers | C Programming Tutorial
18:31
Portfolio Courses
Рет қаралды 58 М.
2 Years Of Learning C | Prime Reacts
22:24
ThePrimeTime
Рет қаралды 264 М.
Essentials: Pointer Power! - Computerphile
20:00
Computerphile
Рет қаралды 463 М.
array vs &array Pointers Difference Explained | C Programming Tutorial
17:38
Void Pointers | C Programming Tutorial
12:44
Portfolio Courses
Рет қаралды 9 М.
WHY IS THE STACK SO FAST?
13:46
Core Dumped
Рет қаралды 144 М.
what even is a "reference"?
5:44
Low Level Learning
Рет қаралды 125 М.
Comparing C to machine language
10:02
Ben Eater
Рет қаралды 5 МЛН
31 nooby C++ habits you need to ditch
16:18
mCoding
Рет қаралды 756 М.
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Mama vs Son vs Daddy 😭🤣
00:13
DADDYSON SHOW
Рет қаралды 48 МЛН