https://softwareengineering.stackexchange.com/questions/252023/why-does-c-use-the-asterisk-for-pointers Stack Exchange Network Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. Visit Stack Exchange [ ] Loading... 1. + Tour Start here for a quick overview of the site + Help Center Detailed answers to any questions you might have + Meta Discuss the workings and policies of this site + About Us Learn more about Stack Overflow the company, and our products. 2. 3. current community + Software Engineering help chat + Software Engineering Meta your communities Sign up or log in to customize your list. more stack exchange communities company blog 4. 5. Log in 6. Sign up Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up. Sign up to join this community [ano] Anybody can ask a question [ano] Anybody can answer [an] The best answers are voted up and rise to the top Software Engineering 1. Home 2. 1. Public 2. Questions 3. Tags 4. Users 5. Companies 6. Unanswered 3. Teams Stack Overflow for Teams - Start collaborating and sharing organizational knowledge. [teams-illo-free-si] Create a free Team Why Teams? 4. Teams 5. Create free Team Teams Q&A for work Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams Why does C use the asterisk for pointers? [closed] Ask Question Asked 9 years, 2 months ago Modified 7 years, 4 months ago Viewed 23k times 39 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 8 years ago. Improve this question I'm just now learning about C. I find it odd that the creators chose the asterisk (*) as the symbol for pointers rather than a symbol that actually looks like a pointer (->). Considering how confusing dereferencing and function pointers can be, is there a historical, or even practical, reason for using the asterisk? * c * history * syntax Share Improve this question Follow edited Oct 3, 2014 at 5:27 Noob Saibot asked Aug 1, 2014 at 22:03 Noob Saibot's user avatar Noob SaibotNoob Saibot 55711 gold badge44 silver badges77 bronze badges 4 * 12 Note that -> is being used in the C language as a dereference operator - when accessing fields in a struct: struct_pointer-> field, which is short for (*struct_pointer).field. - amon Aug 1, 2014 at 22:19 * @amon: It only applies to structs for dereferencing, which seemed odd to me. It's a pointer symbol, right? Why not (<-) for dereferencing? Am i really the only one that thinks this way? - Noob Saibot Aug 1, 2014 at 22:25 * 3 Given the two excellent answers in this question, including one with an answer directly from the language designer, it's hard to really justify the close as "opinion-based". I've therefore nominated for reopening. - Jules Jun 3, 2016 at 7:18 * IMHO Pascal style is better. ^ is used and can be thought of a rotated arrow and read as "point to", same meaning as -> but shorter. ^integer means "pointer to integer" for type declaration, and var^ means "the memory var points to" for dereferencing. The symbol position is more logical than C when reading from left to right, which always put after type and before variable name. Pascal also uses @ for taking address, which is better than &, because @var is "the address at which var is located" - phuclv Apr 14, 2018 at 15:27 Add a comment | 2 Answers 2 Sorted by: Reset to default [Highest score (default) ] 78 Why does C use the asterisk for pointers? Simply - because B did. Because memory is a linear array, it is possible to interpret the value in a cell as an index in this array, and BCPL supplies an operator for this purpose. In the original language it was spelled rv, and later !, while B uses the unary *. Thus, if p is a cell containing the index of (or address of), or pointer to) another cell, *p refers to the contents of the pointed-to cell, either as a value in an expression or as the target of an assignment. ^From The Development of the C Language Thats it. At this point, the question is as uninteresting as "why does python 3 use . to call a method? Why not ->?" Well... because Python 2 uses . to call a method. Rarely does a language exist from nothing. It has influences and is based on something that came before. --------------------------------------------------------------------- So, why didn't B use ! for derefrencing a pointer like its predecessor BCPL did? Well, BCPL was a bit wordy. Instead of && or || BCPL used logand and logor. This was because most keyboards din't have [?] or [?] keys and not equal was actually the word NEQV (see The BCPL Reference Manual). B appears to have been partially inspired to tighten up the syntax rather than have long words for all these logical operators that programmers did fairly frequently. And thus ! for dereference became * so that ! could be used for logical negation. Note there's a difference between the unary * operator and the binary * operator (multiplication). --------------------------------------------------------------------- Well, what about other options, like ->? The -> was taken for syntactic sugar around field derefrences struct_pointer->field which is (*struct_pointer).field Other options like <- could create ambiguous parsings. For example: foo <- bar Is that to be read as: (foo) <- (bar) or (foo) < (-bar) Making a unary operator that is composed of a binary operator and another unary operator is quite likely to have problems as the second unary operator may be a prefix for another expression. Furthermore, it is again important to try to keep the things being typed frequently to a minimum. I would hate to have to write: int main(int argc, char->-> argv, char->-> envp) This also becomes difficult to read. Other characters might have been possible (the @ wasn't used until Objective C appropriated it). Though again, this goes to the core of 'C uses * because B did'. Why didn't B use @? Well, B didn't use all the characters. There was no bpp program (compare cpp) and other characters were available in B (such as # which was later used by cpp). If I may hazard a guess as to why - its because of where the keys are. From a manual on B: To facilitate manipulation of addresses when it seems advisable, B provides two unary address operators, * and &. & is the address operator so &x is the address of x, assuming it has one. * is the indirection operator; *x means "use the content of x as an address." Note that & is shift-7 and * is shift-8. Their proximity to each other may have been a hint to the programmer as to what they do... but that's only a guess. One would have to ask Ken Thompson about why that choice was made. --------------------------------------------------------------------- So, there you have it. C is that way because B was. B is that way because it wanted to change from how BCPL was. Share Improve this answer Follow edited May 23, 2017 at 12:40 Community's user avatar CommunityBot 1 answered Aug 2, 2014 at 1:37 user40980user40980 9 * 2 ...awesome! This is a great answer, @MichaelT! You showed me both historical and practical reasons, and even stuff I didn't quite understand but could look into. Thank you. +1 - Noob Saibot Aug 2, 2014 at 2:09 * @NoobSaibot The choice of character isn't as much of a deal as the fact that the operator is a prepend rather than postpend operator. This necessitates lots of extra parens (though the -> syntactic sugar helps) which can lead to silly yet vexing bugs even for an experienced C++ programmer. - Trixie Wolf Aug 2, 2014 at 3:48 * 1 You might also mention that C found a use for almost every Ascii punctuation character. There weren't many spares. I guess @ would have been another possibility. - david.pfx Aug 2, 2014 at 15:00 * 1 @david.pfx I've expanded on that - though it wasn't C that made that choice... it was B. And, well, I made a guess as to why (keyboard proximity of & and *). B also didn't use # so there were a few more spares around then... there's also $. - user40980 Aug 2, 2014 at 17:18 * 1 @david.pfx The B tutorial that I found was written by B. W. Kernighan. Unfortunately, Dennis Ritchie is no longer able to be asked (he passed away in October of '11) while Kernighan is apparently still a professor in the CS Department at Princeton. Ken Thompson (the other creator of B) works at Google... There may also have been issue with some keyboards (indicated by the trigraphs for C for what we would think of as common keys) suggesting that not all of them were available (I'm not sure if '@' was). - user40980 Aug 3, 2014 at 1:30 | Show 4 more comments 108 I was asked by a student if & and * were chosen because they were next to each other on the keyboard (something I had never noticed before). Much googling led me to B and BCPL documentation, and this thread. However, I couldn't find much at all. It seemed like there were lots of reasons for * in B, but I couldn't find anything for &. So following @MichaelT's suggestion, I asked Ken Thompson: From: Ken Thompson < [email protected] > near on the keyboard: no. c copied from b so & and * are same there. b got * from earlier languages - some assembly, bcpl and i think pl/1. i think that i used & because the name (ampersand) sounds like "address." b was designed to be run with a teletype model 33 teletype. (5 bit baud-o code) so the use of symbols was restricted. Share Improve this answer Follow answered Feb 15, 2015 at 3:43 chmullig's user avatar chmulligchmullig 97122 gold badges77 silver badges99 bronze badges 1 * 36 +1 for contacting Ken Thompson and reporting back here. - stakx Feb 15, 2015 at 9:55 Add a comment | Not the answer you're looking for? Browse other questions tagged * c * history * syntax or ask your own question. * The Overflow Blog * From prototype to production: Vector databases in generative AI applications * Meetings are the worst. Let's reduce their blast radius * Featured on Meta * Alpha test for short survey in banner ad slots starting on week of September... * What should be next for community events? Linked 18 Why pointer symbol and multiplication sign are same in C/C++? 100 Why do programming languages, especially C, use curly braces and not square ones? 1 Why C doesn't have better notation for pointers? Related 19 When should pointers be checked for NULL in C? 10 What is the historical reason why Python uses the double underscore for Class Private members 12 Is it bad to refer to access array elements via pointer arithmetic instead of the [] operator? 59 Why do many functions that return structures in C, actually return pointers to structures? 12 Why would you ever use `malloc(0)`? 1 Passing arrays as global variables instead of pointers in C Hot Network Questions * Best practice for redundant conditions in if-elif-else statements * Does nature jump? * Mutual funds question: "You need to spend money to generate income that's sustainable, because if you don't, then you end up eroding your capital," * Is it legal to sign a "contract" like that of Colleen Stan to voluntarily bond oneself into slavery? * Why are these SATA bus ports different? * Why is famas the default counter-terrorist auto-buy rifle even with plenty of money? * Diagonal Binary Sequence * Early 1980s short story (in Asimov's, probably) - Young woman consults with "Eliza" program, and gives it anxiety * Individual Laser-Point-Defense * How can I seal a crack in a teapot in a foodsafe manner? * Schemes with open generic point * Does a seeded run in The Binding of Isaac count for the D6 unlock? * The slang term for books made of paper * Statistical fallacy from a Japanese light novel * How long do modern flush toilets last? * Old military sci fi book about a spaceship on the edge of disaster * What happened to my car * Simultaneity and The Uncertainty Principle * How to stop Steam trying to read from a non-existent drive? * How do professors advise PhD students that are smarter than them? * Why do we have sump pits? * Horror short story about a man looking into another world of always happy people. He eventually enters, and they rush him and eat him * How can I defer answering a sensitive / untimely question from my boss visiting local headquarters, when visiting a bar? * Can a graph problem remain NP-hard when restricted to cycle graphs? more hot questions * lang-c Software Engineering * Tour * Help * Chat * Contact * Feedback Company * Stack Overflow * Teams * Advertising * Collectives * Talent * About * Press * Legal * Privacy Policy * Terms of Service * Cookie Settings * Cookie Policy Stack Exchange Network * Technology * Culture & recreation * Life & arts * Science * Professional * Business * API * Data * Blog * Facebook * Twitter * LinkedIn * Instagram Site design / logo (c) 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev 2023.10.10.43669 Your privacy By clicking "Accept all cookies", you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. Accept all cookies Necessary cookies only Customize settings