|
|
#26 | ||
|
Quote:
Quote:
So when you say that a function is taking an array as an argument, you're really saying that it's taking a pointer to an array. All array operations involve pointers, though it may not be entirely obvious at first that this is the case. |
|||
|
|
0
|
|
|
#27 | |
|
Quote:
The 'const' is part of the "contract" of the function declaration. For callers that include the declaration, it tells them (and the programmer) that the function won't modify the data pointed at. For the implementation of the function, i.e. its definition, it tells the compiler to enforce read-only access, and doesn't permit the function to modify the data pointed at. Of course, if you recast to non-const, the compiler assumes you know what you're doing. A lot of C is like that: it assumes you know what you're doing. |
||
|
|
0
|
|
|
#28 | |||
|
Quote:
---------- Quote:
The "const" is not. It means the pointer that the function receives is a "pointer to const char". That means the compiler won't let you modify the data that the pointer points to through that pointer. Why does it work? Take both parts together. The caller passes a pointer to the first array element, and the function expects a pointer. So that matches. Once you have a pointer to the first array element, you can access the other elements as well. In your case: word [0] or *word - The first element of the array word [1] or * (word + 1) - The second element of the array word [2] or * (word + 2) - The third element of the array. ---------- Quote:
You cannot declare a function in C (or C++, or Objective-C, or Objective-C++) that has a parameter of an array type. ---------- Ok. Now try to explain what a "restrict pointer" is. Try explaining why pointers that point into the same array, and pointers pointing to different arrays, are treated differently. Try explaining aliasing. Going back in history, try explaining what's the difference between a "far pointer" and a "huge pointer". Try explaining why it is ok to write "char* p = 0;" but not "char* p = 1;" and not "int i = 0; char* p = i;". Try explaining what a C++ reference is. Try explaining a pointer cast in C++. Last edited by gnasher729; Feb 5, 2013 at 04:35 PM. |
||||
|
|
0
|
|
|
#29 | ||
|
Quote:
Now, what type is foo? Const pointer to int. I've declared an array, and the pointer to the array is stored in the variable specified. I don't understand what the problem here is. Are you saying that no memory is allocated for the variable that holds the pointer itself? Okay, fine, but I didn't say that. As far as the C compiler, and thus the programmer, is concerned, you can use the variable foo in any context that calls for a pointer as an rvalue—you can assign it to another pointer, for example. Quote:
None of the things listed here change anything about the fundamental nature of pointers. Think about what the term “pointer” means for a second: It “points” to an address in memory. What is an address? A number. If you explain it any other way, you're just throwing layer upon layer of abstractions from the actual definition of what a pointer is, in an attempt to “simplify” the explanation for the novice user. Ultimately, all of these simplifications must be un-learned in order to advance—if they are understood at all in the first place—because they are fundamentally wrong. If you imply that there is anything special at all about pointers as far as the computer is concerned, you're doing a disservice to those who are trying to learn from you. |
|||
|
|
0
|
|
|
#30 |
|
Perhaps we're going too meta here, but here's a good explanation of whats going on:
http://c-faq.com/decl/spiral.anderson.html |
|
|
|
0
|
|
|
#31 | ||
|
Quote:
Quote:
It is essential to understand the array-to-pointer standard conversion, which converts an array to a pointer to its first element in most contexts. This is the thing from which all the pointer-like behavior of arrays arises, and it is the explanation for chown33's first sentence. Then it's mainly a matter of understanding how pointers work. For example, array indexing is defined in terms of pointer arithmetic, and the first step from a[2] (where "a" is an array name) to *(a+2) is the array-to-pointer conversion. Then you need to understand pointer arithmetic. Then dereferencing the pointer result. Arrays and pointers are not the same thing! An array is a region of storage containing elements of a single type one after the other, with no space in between. In contrast, a pointer is a scalar quantity whose defined values include NULL and memory addresses, the latter being the addresses of single objects, including objects within arrays. An array name is not a pointer! The C FAQ 6.2 explains this. 6.1 could have further illustrated this, but it punts to Koenig's excellent "C Traps And Pitfalls" (1989), which I'm surprised to see is available on Amazon. However, if you understand 6.2 and the fact that your linker may treat the two declarations of "a" as a sort of union, you can explain what happens when you make the mistake in 6.1, and the compiler/linker system doesn't diagnose the problem and produces an executable file, as the present day Visual C++ 2012 continues to do, and probably most if not all others. |
|||
|
|
0
|
|
|
#32 | |
|
Quote:
|
||
|
|
0
|
|
|
#33 | |
|
Quote:
|
||
|
|
0
|
|
|
#34 |
|
I hope you guys don't mind if I add a question: That awesome FAQ linked earlier said that while arrays and pointers are quite different ( I understand the differences), that anytime time an array is used in an expression, a pointer to the first element of the array is used instead? Even though arrays and pointers are separate items, that a pointer actually is used the vast majority of the time an array is used?
|
|
|
|
0
|
|
|
#35 | ||
|
Quote:
The C99 specification (section 6.6) defines an address constant as follows: Quote:
You absolutely can do arithmetic on pointers. True, pointer arithmetic works a bit differently in C than with regular numbers, but that's just shorthand that the language provides to make pointers of data types larger than a single byte easier to deal with. If you wanted to, you could cast the pointers to integers of the appropriate size, do math on them at the byte level, and cast them back to pointers and it would work just fine (assuming your architecture doesn't have any data alignment issues or anything like that.) |
|||
|
|
0
|
|
|
#36 | |
|
Quote:
It is essential to understand the array-to-pointer standard conversion, which converts an array to a pointer to its first element in most contexts. This is the thing from which all the pointer-like behavior of arrays arises, and it is the explanation for chown33's first sentence. Then it's mainly a matter of understanding how pointers work. For example, array indexing is defined in terms of pointer arithmetic, and the first step from a[2] (where "a" is an array name) to *(a+2) is the array-to-pointer conversion. Then you need to understand pointer arithmetic. Then dereferencing the pointer result. While the array-to-pointer conversion occurs in array indexing, passing arguments to functions, returning values from functions (that being a big gotcha for a local non-static array), etc, there are contexts in which it does not occur. A couple that come to mind include when an array is the operand of the address-of operator and sizeof. In C++, other examples include when binding to an array reference and when the array is the operand to typeid. In those instances, an array does not undergo the array-to-pointer conversion. There might be one or two more instances I'm not thinking of at the moment. But anytime an array occurs in other expressions, it undergoes the array-to-pointer conversion, e.g. Code:
typedef struct S { int x; } S;
void f(S*);
void g()
{
S s[10];
S* p = s+5;
s[1];
*s;
s->x;
p-s;
f(s);
}
Code:
S t[2][10]; |
||
|
|
1
|
|
|
#37 | |
|
Quote:
|
||
|
|
0
|
|
|
#38 | |
|
Quote:
6.1.2.5/17 An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. 6.2.2.1/3 Except when it is the operand of the sizeof operator or the unary & operator, or is a character string literal used to initialize an array of character type, or is a wide string literal used to initialize an array with element type compatible with wchar_t, an lvalue that has type ‘‘array of type’’ is converted to an expression that has type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. These agree with the definitions I gave for arrays and for the array-to-pointer conversion. When the irrelevant bit you quoted about "address constants" talks about their production "implicitly by the use of an expression of array type", which is the only thing it says about arrays, this implicit production is due to the array-to-pointer conversion I've been talking about. It does not support your repeated wrong assertions that arrays are constant pointers. Also, the use of the term "expression" further renders the section irrelevant to your claim about array names, because the following is a declaration, not an expression: int x[10]; These are defined terms the standard uses in precise ways. The section you quoted simply doesn't contribute to what is meant by an "array name", and FWIW, "array names" aren't even the fundamental thing anyway WRT array behavior, as I alluded to at the end of my previous message when I talked about subarrays in multidimensional arrays. |
||
|
|
0
|
|
|
#39 |
|
Everything in a computer is stored as a "number", ultimately sequences of bits of various lengths that are assigned various meanings. If you want to talk about something, talk about types. That's the fundamental concept. A type is a set of values and operations defined on those values. It's rather redundant to make a point out of those values being "numbers". What else would they be?
|
|
|
|
0
|
|
|
#40 | ||||
|
Quote:
Quote:
Quote:
But okay, fine. Let me clarify my original statement for you: Quote:
Last edited by ytk; Feb 6, 2013 at 12:03 AM. |
|||||
|
|
0
|
|
|
#41 | ||
|
Quote:
Quote:
ytk sez int foo[20]; Now, what type is foo? Const pointer to int. I've declared an array, and the pointer to the array is stored in the variable specified ----- An array name is an “address constant”... So every address constant, including an array name, is by definition a pointer. All of that is just trivially wrong, and besides not understanding much about C, you don't even seem to comprehend what you've written, as you're denying saying those things you plainly said. You're not even making an effort to learn. Judging by your lack of comments and questions, you haven't read the C FAQ recommended to you by several people, which explains why you're wrong. Nor have you asked anything about the array-to-pointer conversion, which is the key to clearing up a big part of your misunderstanding. P.S. I'm putting your past posts in italics because otherwise they get clipped when I reply, and I can't figure out how to make this board preserve more than one level of quotes. Having to go back and find those statements makes this whole thing even more tedious. |
|||
|
|
0
|
|
|
#42 | |
|
Quote:
Your revision is not an improvement. When you declare: int x[10]; The only thing that exists in memory is the array of 10 ints. (For the definition of "array", see my previous posts.) There is no pointer anywhere in it or around it. In particular, x is not a pointer. It is harmful to think of it as a pointer. The identifier x merely is the name for this array, and it is not an object in its own right. Thus, it is ABSOLUTELY WRONG to say that "declaring an array defines a memory address which is stored in the name specified, which points to the head of the array". |
||
|
|
0
|
|
|
#43 | |
|
Quote:
I'm tired of arguing with you about this, particularly when you repeatedly tell me I've said things I didn't say, or explicitly said the precise opposite of. I'm not even sure what the argument is about, because I basically agree with everything you're saying conceptually. Yes, I get it. An array is not a pointer. I've said that numerous times. Referencing an array by name returns a pointer, so you can treat the array name exactly as if it were a pointer to the start of the array. That's really all I'm trying to say here. If you think I ever said anything different, then I was simply unclear. Okay? |
||
|
|
0
|
|
|
#44 | |||
|
Quote:
And once you get to things like multiple inheritance in C++, references in C++, blocks in Objective-C, distributed objects, or more interesting hardware like an AS400, this "pointer = number" thing _really_ makes it hard to understand what's actually going on. Quote:
Code:
void f (int array [20])
{
printf ("Size of array parameter is %d\n", (int) sizeof (array));
}
int main (void)
{
int array [20];
printf ("Size of array is %d\n", (int) sizeof (array));
f (array);
}
Quote:
Last edited by gnasher729; Feb 6, 2013 at 02:41 AM. |
||||
|
|
0
|
|
|
#45 | |
|
Quote:
This isn't great advice. The quote tags generate quote notifications it's a great way to note that someone is replying to your previous message. Also the tone of the thread has become a bit personal. Dial it back a bit please or we'll have to put this one down. B
__________________
MBA (13" 1.7 GHz 128GB), UMBP (15" SD 2.8 GHz), UMB (13" 2.4 GHz), iMac (17" Yonah), 32GB iPad 3 WiFi+LTE, 64 GB iPad WiFi, 32 GB iPhone 5, Airport Extreme |
||
|
|
0
|
|
|
#46 |
|
I think i will leave this discussion.
I guess we can agree that a discussion about pointers in c quickly can become, hmm, sort of complicated. That is why we should recommend any beginning programmer to follow a well-thought curriculum in learning how to use pointers. Every-one will then go from the simple things into more complicated matters. On the way, the understanding will deepen and maybe even change. The final authority on how a c program should behave is the standard document, but in order to actually understand that you need a firm base in computer science. And any actual implementation might add further "complications". I remember one version of a c compiler where you could select between small, medium and large memory model, each handling pointers quite differently inside the program. In the small model, pointers could only point to a small memory area of 256 bytes. In this mode the compiler could not follow the full c specification, but you could do decent things within the limits regardless. |
|
|
|
0
|
|
|
#47 | |
|
Quote:
http://forums.macrumors.com/showpost...1&postcount=40 This makes it hard to reply to him, because those parts don't appear when I quote his message. When I quote the above linked message to reply, the end of it appears as follows (I'm using italics and my own pseudo-tagging because this is part of my message, and using quote tags would cause the problem I'm complaining about if someone were to reply to it): *****ytk {***** But okay, fine. Let me clarify my original statement for you: ///// part ytk put in quote tags missing here ///// That statement, while technically a more accurate description of what is going on under the hood, provides more information than is necessary for comprehension and is likely far more confusing to a novice. Happy now? *****} ytk***** Like I said, I have to go back and copy and paste to restore the missing part. This would not be a problem if the forum were configured to leave nested quote tags alone. I looked for an option to enable this feature, but like I said earlier, I didn't find it. I can understand the motivation for disabling nested quotes, but most forums don't do it, and it's fairly common to use quote tags as ytk has done rather than using "real quotes", because it sets the quoted text apart better. Moreover, unless a reply to a quoted part includes everything necessary to understand the reply, you have to go back to the original to get the proper context, which can get really tedious when people break a message into multiple quoted parts to respond inline, as they typically don't copy the little link to the antecedent message in each quoted part they separate out. For all these reasons, I think it's best not to have the forum software effectively edit replies by stripping nested quotes and require people to trim quotes themselves, despite all the problem with that policy. |
||
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 01:46 AM.







Linear Mode
