Talk:C Programming/Pointers and arrays

I think this article is especially in need of work. I agree with the general sentiment below that the examples are not very helpful; and pointers are conceptually tricky, yet are here approached without substantial introduction to what they really are, or their actual usefulness. Meatnordrink (discuss • contribs) 14:35, 8 December 2020 (UTC)

The example that begins with  doesn't compile for me. That example also probably doesn't belong in an introductory text about pointers. It should probably be substantially rewritten or removed.

Someone wrote:

Declaring an array variable without a size is equivalent to declaring a pointer

followed by a bogus example. This statement is completely untrue. One declares an incomplete array that, unless later completed, is completed at the end of the translation unit with 1 element. It takes up the space of the element. The other declares a pointer, and takes up the space of a pointer. You really should understand this stuff before trying to teach it...

User:207.17.47.140

It's a syntax that comes from C's predecessor B. It's still an accepted syntax for function parameters. Look:

int fn(int a[]){ a++; return *a; }

To really understand all this, I think it is good to describe how C evolved out of B. Without that background, lots of stuff just doesn't make sense. C sometimes gives you a "free" unary & operator when you use an array as a pointer; I'm sure that this is a major cause of beginner confusion. It makes sense once you know that arrays in B were really pointers to arrays. The arrays lost this when the language gained structs, because prior to the C++ invention of constructors there would be no way for the pointer to get initialized to point at the data. Thus we get the weird array-as-a-pointer feature that confuses people.

I think that teaching this should involve:


 * 1) generic language-neutral concepts of arrays, pointers, and strings (no code examples)
 * 2) how B did things
 * 3) the big language change
 * 4) review
 * 5) strings now const. New C99 variable-size array stuff

All through it, the oddities of pointer arithmetic will require frequent reminders. (the whole 42[array] thing, *array, and so on)

AlbertCahalan 04:45, 29 September 2005 (UTC)

Removed the casting of void* to an object pointer type. void* was introduced specifically as a generic pointer type that does not need casting; such casts are a relic from pre-ANSI C when char* was abused as the generic type. Also, removed text that stated that such a cast is necessary. While casting may seem harmless, it can mask the serious problem of forgetting prototypes for allocation functions. In addition, it simply clutters up code. One rarely sees code like "char x = (char)'x';" even though 'x' is an int, for example. The same principle should apply to pointers. -- 67.170.85.37 04:06, 3 January 2006 (UTC)

-

Could anybody explain me that:

typedef int (*MyFunctionType)( int, void *);

I thought typedef is used like that: typedef long int* abc; abc variable1; thx


 * MyFunctionType typedef'ed to be a pointer to a function that takes two arguments (int, void *) and returns an int. Read about the syntax for declaring function pointers in C. --Spoon! 07:22, 25 July 2006 (UTC)

article style
I gotta tell you, this article is really unclear...the problem is changing the way of thinking of someone, especially if they have to wrap their minds around pointers and arrays in quick succession as the article flows. It's especially hard because my background is Java and Perl...

--168.105.118.205 11:10, 30 April 2006 (UTC)


 * I agree. What is this supposed to mean? When returning a pointer from a function, do not to return a pointer that points to a value that is local to the function or that is a pointer to a function argument. WTF? Also, the nonsense examples are distracting. MEANINGFUL examples would be more helpful, even if they're slightly longer. 128.250.37.103 10:14, 23 August 2006 (UTC)


 * It means that, since local variables are deallocated when the function returns, don't return a pointer to any of those variables, or it will be a dangling pointer. Care to rephrase it? --Spoon! 02:05, 1 September 2006 (UTC)

Just as a note, the current external link to a 'pointer cheat sheet' actually goes to a quiz, which I wouldn't describe as a cheat sheet.

Another possibly unclear one: "First one declares a structure having as elements function pointers for the various operations to that can be specified polymorphically." (In #Pointers to Functions.) I assume it means "First one declares a structure [which has elements which are] function pointers for the various operations..." but then it says "... to that can be specified polymorphically." I can't make sense of it unless I assume the "to" was there by mistake, in the sense of "various operations that are to be specified", but I don't know if this is right. 21:23, 12 January 2010 (UTC)

merge
I suggest merging our discussion about pointers into this module C Programming/Pointers and arrays, rather than talk about them here and again in a section of another module C_Programming/Complex_types. --DavidCary (talk) 15:21, 28 March 2009 (UTC)

Is the struct example within scope?
We're leading off with a discussion involving structs, which aren't introduced until C Complex Types. We could either reorganize the two sections so that structs have already been presented, or we could find a different example for this section. I might get around to changing things up before long, but I'd welcome any thoughts on the matter before I have time to Be Bold and whatnot. Ingcake (discuss • contribs) 21:54, 28 May 2015 (UTC)

Pointers and Arrays - example code has extra level of pointer dereferencing
In this example: the last two lines should be:

Please note the removal of the '*' characters from the beginning of the assignment. Those are not needed because In the right-hand form, the '*' _is_ needed.