Jump to content

Talk:struct (C programming language)

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Article or redirect?

[edit]

From history, this page has been in three rather different states:

Each is reasonable (although the last perhaps less so), but there seems to have been no discussion of the matter. Worse, the Structs page had gotten set as redirecting to Object composition, making the singular and plural of "struct" refer to very different things.

The evolution seems to be that what started out as being about C (programming language) specifically got geared towards C++, and then towards abstract object orientation. Later someone reacted to this and changed the redirect to the more common (but still abstract) interpretation of Record (computer science). 90.230.192.94 (talk) 17:17, 8 May 2009 (UTC)[reply]

[edit]

This article should observe that struct names are a namespace separate from the main namespace of types and variables, so that one can declare

typedef struct ListNode { int val; struct ListNode *next; } ListNode;

That is, it is not necessary to use different names (ListNode_ and ListNode) for the two C views of what is essentially the same data type.
All the best, --Jorge Stolfi (talk) 21:57, 15 May 2009 (UTC)[reply]

Type punning

[edit]
"For example, common Internet protocols rely on the fact that C compilers insert padding between struct fields in predictable ways [...]"

Is this an example of type punning? --Abdull (talk) 22:52, 2 January 2010 (UTC)[reply]

I don't think so. I think it was intended for uses such as those shown in the example, and not, as Type punning characterized it—a subversion. See #Stuctural padding below. — CpiralCpiral 23:16, 1 October 2011 (UTC)[reply]

Call by reference or call by value?

[edit]

In C, when calling a subroutine or receiving a return value, is a struct passed/returned by value or by reference? Does the ISO/IEC C standard discuss this topic explicitly? Thanks, --Abdull (talk) 09:18, 2 May 2010 (UTC)[reply]

I haven't looked at the standard you mention, but it has always been part of C that a struct is passed and returned by value. If a struct contains data occupying 1000 bytes, the compiler pushes 1000 bytes onto the stack. C programmers often pass a pointer to a struct, which is what a "pass by reference" language would do. I suppose this could be mentioned somewhere (it is implied at C (programming language)#Characteristics), but it's probably excessive detail for an article. Johnuniq (talk) 02:03, 3 May 2010 (UTC)[reply]

Structural padding

[edit]

Assuming "padding" is strictly related to data structure alignment, I move we place the information about padding deeper into the article, and change the concept from "padding" to alignment. e.g. "The C compiler aligns the members of a struct..." A lead section might better focus on the unique aspects. There are two things that are not unique about the coded padding example.

  • Padding is not unique to structs if alignment is used throughout the executable. In case the compiler does align data types, member data types, and instructions and cooked data for the executable, then the compiler routine for byte padding a struct need not be called (assuming there is not some other addressing algorithm, unrelated to word size).
  • The internal alignment of data members is not unique to the C struct. There is an analogous dynamic that also "works as expected" concerning subclassing in C++. That said it is very interesting that even if a C compiler is run "unaligned" it will always align (don't say "pad"?) struct members inside the struct, guaranteeing this manipulation by a sub-struct of quantity N-last elements manipulating its containing struct of N elements.

Salient internals for our lead section are the size (as mentioned), and the method of chaining the members of a C struct to one another. — CpiralCpiral 21:31, 1 October 2011 (UTC)[reply]

Declare/define

[edit]

"The memory is already given and zeroed by just declaring a variable of that type regardless of member initialization" is nonsense. Declarations do not allocate memory. Object definitions allocate memory. Furthermore, that memory is not "zeroed" by a definition (let alone a declaration!). It is necessary to initialize at least one member of the struct if one wishes the default static initializer rule to cut in. If the initializer is another struct, a bitwise copy is made, and thus any members (note: members, not fields!) that were 0 in the initializing struct will be 0 in the initialized struct.

I didn't bother correcting the article, because it's a waste of time. There always seems to be someone ready and waiting to uncorrect it again.

BinaryDigit (talk) 17:00, 14 March 2012 (UTC)[reply]

I have been watching this page for a while (but never really examined its contents), and I don't recall seeing any good edits undone. What you say makes a lot of sense, so please go ahead and edit. Johnuniq (talk) 03:10, 15 March 2012 (UTC)[reply]
I have gone ahead and removed the incorrect statement about zeroing. The example doesn't actually demonstate the supposed behaviour it described anyway, so I've left that as-is. It seems like someone had just "learned" and misinterpreted this "fact" and made a drive-by addition to an otherwise correct section. — Preceding unsigned comment added by 82.9.176.129 (talk) 23:28, 7 November 2014 (UTC)[reply]

Too "{{technical}}"

[edit]

While it doesn't contain too much detail for such a technical subject. The article's tone is manual-like (something for Wikiversity, not Wikipedia]] and is clearly too technical for most readers to understand. It demonstrates some concepts directly through code use (such as C compilers inserting "padding between struct fields in predictable ways"). I am only able to understand this article due to my backround in C, not all readers are programmers.
Sowlos 10:00, 21 February 2013 (UTC)[reply]

In what situation would a non-programmer would need to know what a C struct is? There's no doubt a summary at the top could help though. Kupiakos (talk) 20:48, 5 July 2013 (UTC)[reply]
As a Java and Python programmer who has never worked with C, I find this article difficult to understand. I think the biggest reason is that the explanation is almost entirely in code (which is difficult to understand if you aren't familiar with C syntax), and it lacks a high level description of what a struct is for and more importantly why anybody would want to use one. I think it would also be helpful to draw parallels between it and similar concepts in other languages. I get the impression that this is maybe like Java's Enum type, but the article doesn't say what languages use structs, aside from C, and what equivalencies, if any, exist in other languages. -Thunderforge (talk) 20:05, 14 October 2013 (UTC)[reply]
That doesn't really matter does it? Wikipedia is for a general audience. If an article is not useful in such a context, it doesn't belong here. If it is useful, it should at least be a stated goal to make it understandable by a general audience, even if it currently isn't. — Preceding unsigned comment added by 82.9.176.129 (talk) 23:31, 7 November 2014 (UTC)[reply]

Not too

[edit]

Yes, the article is atypical and maybe too much intended for someone with experience in programming BUT it is not an article about a beginner topic! this article has immense value for one who wants to lookup special attributes of the "struct" in the C programming language. I think the advice or request for rewrite should say something else: "The article needs a longer introduction of dictionary character, which in turn should direct the reader to the general article on //records//." --d-axel (talk) 23:07, 20 June 2016 (UTC)[reply]

Incorrect statement

[edit]

The following statement in the article is incorrect:

"The memory is already given and zeroed by just declaring a variable of that type regardless of member initialization"

Asigning to a new struct by value from another struct will create a copy with the same layout, but if the rvalue is only partially initialized, the lvalue will also be partially initialized. None of the uninitialized fields will be magically "zeroed". The exception to this is structs with static storage duration -- but in the example, the structs have automatic storage duration. — Preceding unsigned comment added by 82.9.176.129 (talk) 23:21, 7 November 2014 (UTC)[reply]

Yes, it was wrong two years ago (when I mentioned it before), and you're right - it's still wrong. I'm almost tempted to write it properly, but I tried once before to correct Wikipedia's C stuff, and it was broken again within a day. BinaryDigit (talk) 07:17, 3 February 2015 (UTC)[reply]

To be clear, that sentence was removed just afterward by that editor. Dhtwiki (talk) 10:02, 4 February 2015 (UTC)[reply]

confusing variable

[edit]

In the examples, this uses the variable "point" but also talks about pointers. It can get confusing to know which one is being referred. Perhaps changing the arbitrary name of the variable to some like "location" might make the example clearer. — Preceding unsigned comment added by 199.116.175.88 (talk) 12:53, 7 May 2015 (UTC)[reply]


Also, there is a line of code in the article {ie. typedef struct point point; } that has two words 'point point'. This is sort of like the life story of many documents --- introducing confusing things without explaining it. To the newcomer of the language, they will probably have no idea what that means. It is also off-putting for newcomers when confusing details or code like that is just placed there, without some kind of explanation - such as this style follows some kind of convention. KorgBoy (talk) 23:33, 3 June 2018 (UTC)[reply]

What directly corresponds data type the Assembly Language has?

[edit]

The C struct directly corresponds to the Assembly Language data type of the same use, and both reference a contiguous block of physical memory

— 2nd paragraph of the introduction to struct (C programming language)

I can't see a directly corresponds Assembly Language data type of the same use. Is it just because I apparently less familiar with the Assembly Language then I think I am? Is it because the directly corresponds Assembly Language data type is purely a contiguous block of physical memory? I think there should be some clarification. 188.120.152.177 (talk) 14:11, 27 November 2015 (UTC)[reply]

Yes, the statement needs clarifying. The author might have had in mind the nomenclature of a particular assembly language or a macro name; but, otherwise, the statement appears erroneous. Struct is definitely the sort of complex data structure that complilers, not assemblers, deal with. So, there shouldn't be a one-to-one correspondence. Dhtwiki (talk) 22:44, 27 November 2015 (UTC)[reply]
Various assemblers supported, and presumably still support, structs; Google 'asm struct' for confirmation. I suspect that C was first and assemblers added support to make it easier to call assembly modules from C. I think the assembler would be told to assume that some index register was pointing to a block of memory holding a struct, and the program could then use symbols (labels) as offsets within the struct. However, that is esoteric history and not much use to explain what a struct is. Johnuniq (talk) 23:45, 27 November 2015 (UTC)[reply]