Thread: "Pointers" <-- crappy name

  1. #31
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CornedBee View Post
    No, it isn't. string is an array. Which is not the same thing as a pointer.
    Okay. It was my understanding that "string" points to the starting address of the array*. But since the complier guarantees the memory is sequential, I suppose that is silly.

    Still:
    Code:
    #include <stdio.h>
    
    int main() {
    	char string[]="hello world", *ptr=string;
    	printf("%p %p\n",string,ptr);
            printf("%p %p\n",&string,&ptr);
    	return 0;
    }
    You know what happens here. So string is just the starting address of an array, whereas ptr has it's own, separate address containing the address of string? That would explain why no arthmetic with "string" is possible.

    I think I just answered my own question. Thanks for clarifying, CornedBee.

    * I'm sure I picked that up from the cboard crew too, but I won't point fingers..wha-wha
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #32
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, string is an array of allocates bytes in which the string is stored. It's not a pointer.
    This is why it can be modified, while string literals cannot (because they are not stored in a buffer on the stack like the array is).
    Just as the compiler says, the size of string is 12 bytes (if I counted correctly) and ptr is 4/8 bytes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #33
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    string is an array.

    Now, C has the distinction between "lvalue" and "rvalue". These are muddled concepts, but succinctly put, the lvalue of a variable is the variable itself (think of it as a "location value"), while the rvalue is its value. Temporaries and literals don't have lvalues (except string literals). You cannot take the address of an rvalue. You cannot use an lvalue in most other operations, such as arithmetic.
    Whenever lvalues are used where rvalues are required, you get an implicit conversion. For most things, this conversion has no practical effect; it's only a matter of arguing about the theory of the language. But for functions and arrays, it has an effect: a function is converted to a pointer to it, and an array is converted to a pointer to its first element.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #34
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Learning things again today. So I can't go "string++" because "string" is an lvalue. And yet, I can dereference (*string) and get an rvalue...it seems to me there is a slight anomaly in the syntax since "*ptr++" and "ptr++" accomplish the exact same thing.

    Quote Originally Posted by CornedBee View Post
    Temporaries and literals
    What do you mean by "temporaries"?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #35
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If I write std::string() then I have created a temporary of type std::string. If I call a function that returns a std::string, that return value is a temporary. If I pass a string literal to a function that takes a const reference to a string, that reference will reference a temporary.

    There's lots of places in C++ where temporary objects are created implicitly or explicitly. Such objects are rvalues.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #36
    Registered User
    Join Date
    May 2009
    Posts
    29

    more to learn..

    Quote Originally Posted by MK27 View Post
    There is a slight complication. A pointer contains an address (or, that is what it's value should be) but a pointer also has an address of it's own, where it's value is stored. The later is generally irrelevant, but this distinguishes the concept of "memory address" from "pointer".

    Pointer arithmetic generally does not involve hex. Eg, to move thru a string:
    Code:
    #include <stdio.h>
    
    int main() {
    	char string[]="hello world", *ptr=string;
    	int i;
    	while (ptr[0] != '\0') {
    		printf("%c",ptr[0]);
    		ptr++;
    	}
    	return 0;
    }
    Hex is just a form of notation. ++ is 1, which is 1 in dec and hex. If you wanted to skip to the end of string, you could use +=0xb, but most people would use +=11. Hex is used alot with mem address because the numbers are big and (possibly) because most programmers will recognize a hex number as a memory address (but that is not the only place it is used).
    well, I still have much to learn about the lower levels of the language, and advanced pointer operations. I still think the term "pointer" is a misnomer and misleading. But then again, I haven't gotten as far as most here, so I'll just wait till after my advanced C class, but for now, to me, pointer or array is basically synonymous with "a memory address".

    Ok, well, back to my program.. I've procrastinated enough.

  7. #37
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by MK27 View Post
    Okay. It was my understanding that "string" points to the starting address of the array*. But since the complier guarantees the memory is sequential, I suppose that is silly.

    Still:
    Code:
    #include <stdio.h>
    
    int main() {
    	char string[]="hello world", *ptr=string;
    	printf("%p %p\n",string,ptr);
            printf("%p %p\n",&string,&ptr);
    	return 0;
    }
    You know what happens here. So string is just the starting address of an array, whereas ptr has it's own, separate address containing the address of string? That would explain why no arthmetic with "string" is possible.

    I think I just answered my own question. Thanks for clarifying, CornedBee.

    * I'm sure I picked that up from the cboard crew too, but I won't point fingers..wha-wha
    Still, that's undefined behaviour. That is, the way you print the addresses. No-one said sizeof(void *) == sizeof(char *) :-). Considering %p is for void pointers.

  8. #38
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by zacs7 View Post
    Still, that's undefined behaviour. That is, the way you print the addresses. No-one said sizeof(void *) == sizeof(char *) :-). Considering %p is for void pointers.
    Even if different types of pointers are different sizes (or are different in even stranger ways), you can still convert from any pointer type to void *.

    So it's just a matter of adding an explicit cast to the pointer when you call printf(). A smart compiler might even do that for you, although of course you should never rely on that.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #39
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No-one said sizeof(void *) == sizeof(char *)
    The C++ standard does, in 3.9.2p4:
    "A cv-qualified or cv-unqualified (3.9.3) void* shall have the same representation and alignment requirements as a cv-qualified or cv-unqualified char*."
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #40
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > So it's just a matter of adding an explicit cast to the pointer when you call printf(). A smart compiler might even do that for you, although of course you should never rely on that.
    That was my point

    Quote Originally Posted by CornedBee
    The C++ standard does, in 3.9.2p4:
    "A cv-qualified or cv-unqualified (3.9.3) void* shall have the same representation and alignment requirements as a cv-qualified or cv-unqualified char*."
    Oh cool... perhaps I chose the wrong example type then... Although I did gloss over the C99 draft. But you did quote the C++ standard
    Last edited by zacs7; 05-26-2009 at 07:58 AM.

  11. #41
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by argv
    My main point really, is that most explainations of pointers really suck.
    Then you should not claim that your explanation is a "tutorial"

    Still, I maintain that, from my understanding at least, it all boils down to the fact that a "pointer" is simply an address.
    Yes, but what tutorial on pointers does not say this?

    Here are the first few sentences of section 10.1.1 of Accelerated C++ by Koenig and Moo:
    A pointer is a value that represents an address of an object. Every distinct object has a unique address, which denotes the part of the computer's memory that contains the object. If you can access an object, you can obtain its address, and vice versa. For example, if x is an object, then &x is the address of that object, and if p is the address of an object, then *p is the object itself. The & in &x is an address operator, and is distinct from the use of & to define reference types. The * is a dereference operator, which works analogously to the way * works when applied to any other iterator. If p contains the address of x, we also say that p is a pointer that points to x.
    Then Koenig and Moo go on to talk about null pointers. Since they cover iterators first, they have already covered one past the end iterators. Of course, we could always talk about pointers before generalising to iterators.

    Here is a statement from cprogramming.com's own pointer tutorial:
    In the computer, pointers are just variables that store memory addresses, usually the addresses of other variables.
    Oh, and another tutorial pretty high on the search results for "C++ programming" is cplusplus.com's tutorial, which first introduces the idea of memory and memory locations, then the concept of a reference:
    The address that locates a variable within memory is what we call a reference to that variable.
    and then:
    The variable that stores the reference to another variable (...) is what we call a pointer.
    So, it sounds that you are just complaining about sloppy reading material that is not worth bothering about in the first place. Get a good book and cross reference with good tutorials.

    Quote Originally Posted by argv
    but for now, to me, pointer or array is basically synonymous with "a memory address".
    In many contexts, an array is converted to a pointer to its first element, but that does not make an array a pointer. Perhaps the easiest way to demonstrate this is to take the sizeof an array and compare it to the sizeof a pointer. If you get the same result, change the number of elements of the array and compare again.
    Last edited by laserlight; 05-27-2009 at 08:41 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #42
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    Yes, but what tutorial on pointers does not say this?
    A pointer *is not* an address. A pointer contains an address, and it has an address of it's own. That's two addresses for every pointer. The purpose of a pointer is to point to an address. It is not itself the address. I think you do go on to explain that, which makes it even stranger why you would concurr "a pointer is an address".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #43
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    A pointer *is not* an address.
    The term "pointer" can also be used to mean "address".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #44
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    The term "pointer" can also be used to mean "address".
    Yes, and to refer to a species of weird looking dogs. But I bet you can't actually write a sentence doing what you just claimed.

    The [pointer/address] of variable x is...

    The [pointer/address] is 0xdeadbeef.

    The [pointer/address] should advance through the string one byte at a time.

    Maybe that last one will work either way, but I think then it will have two slightly different meanings. And we have been witnessing the effect this apparently has on the uninitiated, at least one of whom evidently feels that since "a pointer is an address" why call it a pointer?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #45
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    But I bet you can't actually write a sentence doing what you just claimed.
    Koenig and Moo did that in the paragraph that I quoted: "if p is the address of an object, then *p is the object itself". Obviously, we can also say: if p is a pointer to an object, then *p is the object itself.

    This website's tutorial is more explicit about defining terms:
    A note about terms: the word pointer can refer either to a memory address itself, or to a variable that stores a memory address. Usually, the distinction isn't really that important: if you pass a pointer variable into a function, you're passing the value stored in the pointer--the memory address.
    This usage can also be seen as something of a simplification in terms, e.g., we may talk about an int variable n being 2 when it is more accurate to talk about the value of n being 2, so it may be more convenient to use "pointer" and "address" interchangeably when the distinction is not significant.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Look at my crappy program. Look at it!
    By Hirumaru in forum C Programming
    Replies: 18
    Last Post: 03-12-2009, 01:17 AM
  2. <( ' '<) Simple Programming Question?
    By strigen in forum C Programming
    Replies: 1
    Last Post: 03-05-2009, 03:17 PM
  3. x = x < y < 2; WHY IS THIS ALWAYS TRUE!?
    By sh4k3 in forum C Programming
    Replies: 5
    Last Post: 06-08-2007, 01:00 AM
  4. > > > Urgent Help < < <
    By CodeCypher in forum C Programming
    Replies: 2
    Last Post: 01-31-2006, 02:06 PM