3 ms·
What is broken about it? I am happy to make corrections.
by denniskubes 14y ago
What is broken about it? I am happy to make corrections.
- jorgem 14y agoSome thoughts: >> A pointer is a variable that holds, literally points to, a memory address What is a memory address? How does a pointer literally point to a memory address?
- jcoder 14y agoIt's the guide to pointers, not the guide to memory addresses—I think readers are expected to google unfamiliar terms.
- jorgem 14y agoI think if I understand memory addresses and assembly language, then I understand pointers. If the guide doesn't explain why pointers are useful and efficient on real hardware, then it's missing the point. Which was my point.
- phao 14y agoC assumes some sort of object space addressable to the byte (each object is composed of one or more bytes -- no such a thing as a 0 bytes object in C). It's the memory pretty much. Afaik, what is an address is not defined as part of the language. I guess people should use the knowledge they got from their "how computers work" classes =D. A pointer holds an address. I think that's as deep as you can get with this as clarifying goes. You can though, say, to help learners that the address of an object is a value which can be used to indirectly access the object. You could say that it's a value that you can keep with you, to access the object later, indirectly. And, as any language that I am aware of. Things are usually defined abstractly (in terms of what you want to be true about them). C is not different. You won't find concrete definitions (things may look concretely defined, but they're not). As far as we're concerned, addresses are these things that - the address-of operator returns - is help by pointers - can be indirected - ... It could be represented as a character string for all we know.
- phao 14y agoMinor points: - & is called the "address of" operator, and saying "The & is the reference operator and is used to reference a memory address" sounds just strange. - In C, a reference is just a way to indirecty access an object, which means that a pointer is a reference. The C language doesn't define references like C++ does (as another name for an object). In C, references are much informal (the conceptual definition is used; as in a means to access something else). I am going to be honest with you, I read the beginning. Found it strange, then looked at the topics. Saw you had a "pointers and arrays" section. Read it (it's the part where people get confused more). You seem to make the same mistakes. I didn't read the rest. These are not minor problems, IMO. Here is a quote from the pointers and arrays section: "Imagine an array variable like a pointer that cannot be changed that holds the memory address of the first element of the array it points to. Even though the array variable holds a memory address, you cannot assign a pointer variable to an array variable, even if the pointer variable actually points to the same or a different array. You also cannot assign one array variable to another." Arrays are not pointers. And they don't hold memory addresses. An array is a continuous block of memory holding as many objects as you specified, all of the same type. You should explain "value context" and "object context" and tell people that when an array name is used in a value context, the value you get is a pointer to its first element. This is not due to "arrays are pointers" or "arrays hold pointers". Arrays are not pointers; and they only hold pointers if the element type is of a pointer type (however, in this case, the pointer(s) is(are) its element(s)). You should explain what it means "arrays values are pointers to their first elements", and focus on "value". If you take the sizeof operator or the address of operator, they operate on object context (not on value context), then you will "reveal" the "true nature" of the array. To really be able to explain pointers, you should explain other things first. Which is very hard to do in a small blog post. I suggest you make a series of posts; and try to make then correct, w/o getting bogged down with definitions of C terminology (which idk how to avoid). Terminology such as lvalue, rvalue, name, value context, object context, value of something, object, identifier (in C, identifiers and names are 2 different terms), and some others. Out of curiosity... Can I ask you why you're posting about C?
- denniskubes 14y agoYou have some valid points. I clarified the arrays section. I am just attempting to show some simple examples. Wasn't trying to do anything in depth. Not the easiest things with pointers of course.
- mauchter 14y ago> On lines 15-16 we assign our void pointer back to our castptr int pointer. Notice the explicit cast needed. The explicit cast is not needed in C.
- RegEx 14y agoCorrect. This is also why you do not need to cast the result of malloc(). An assignment of a pointer to void to another pointer initiates an implicit conversion.
- eswangren 14y agoAnd it is actually somewhat dangerous to cast the return value of malloc. Aside from being redundant it can hide an error on compilers which implement an older version of the standard (not uncommon. Anything pre-C99). If you forget to include stdlib.h the cast hides the error and malloc will be assume to be a function which returns int. makes for interesting runtime errors. Never cast the return value of malloc in C, and don't write redundant code. C is not C++.
- phao 14y agoHonestly, why don't you re-iterate the known tutorials about these things? Make a blog post saying "here are these old C tutorials, which are amazing; and some people seem to have forgotten these things" and post them. Lots of comp.lang.c posts are interest Chris Torek's stuff are interesting. If you go to the ##c channel, at irc.freenode.com, they have a wiki link on the topic. That wiki has links to many tutorials. Re-iterate these things. And, please, recommend people using something other than C =)
- wyclif 14y agoIt would be super cool if you would just point to what you think is the better stuff with a link.
- phao 14y agoSorry. I don't know why I didn't do it. http://www.iso-9899.info/ http://www.iso-9899.info/ This is that wiki page I mentioned. It has links to all sorts of places. All articles I was talking about, I found in there. It has an articles page: http://www.iso-9899.info/wiki/C_gotchas http://www.iso-9899.info/wiki/C_gotchas And it has some recommendations on many things, including other artigles. http://www.iso-9899.info/wiki/Usenet http://www.iso-9899.info/wiki/Usenet Check out the "Additional materials part": http://www.iso-9899.info/wiki/Main_Page#Additional_materials http://www.iso-9899.info/wiki/Main_Page#Additional_materials Anyway, wander a little bit in that page. The books recommendations are great, even though they recommend Deitel & Deitel C book, which is not bad, but it's sort of shallow.
- wyclif 14y agoThanks, I appreciate that.
- phao 14y agoThis Chris Torek guy has a bunch of articles. Series called "C for smarties". It's __very__ good. http://web.torek.net/torek/c/ http://web.torek.net/torek/c/ The link is in there in the wiki somewhere, but I think it's worth placing it separately. The wiki mentions some names in the Usenet page. Google these names.
- denniskubes 14y agoThanks to everybody for the suggestions. I made some changes to denote addressof and better explain arrays. Also stated that the explicit cast wasn't needed.
- greenyoda 14y agoA couple more things: - There's nothing special about uninitialized pointers in C. Any uninitialized variable, whether it's a pointer, int, double, etc., contains unpredictable bytes left over from whatever was previously in that memory location, and should not be referenced. Not sure why this belongs in an article that's specifically about pointers. - You should proofread the whole thing and fix the typos that a spelling checker won't pick up, e.g., "garage" should be "garbage".
- acqq 14y agoExactly, as soon as I saw "pointers can be null or uninitialized" my thought was "this is a really confused soul who wants to teach others." I made my own compilers for living but I find the text very confusing. I'm glad I didn't have to learn anything from this text. I still believe people should learn C from this book: http://en.wikipedia.org/wiki/The_C_Programming_Language http://en.wikipedia.org/wiki/The_C_Programming_Language It's not so big and it's really good written.