Members Bookmarks Fresher Jobs Funny Photos B.Tech Projects New Member FAQ  



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

Paid Surveys


website counter



C Question on structure.


Posted Date: 06 May 2008    Resource Type: Articles/Knowledge Sharing    Category: Jobs & Interviews

Posted By: umang garg       Member Level: Gold
Rating:     Points: 3



57. I heard that structures could be assigned to variables and passed to and from functions, but K&R I says not.

A: What K&R I said was that the restrictions on struct operations would be lifted in a forthcoming version of the compiler, and in fact struct assignment and passing were fully functional in
Ritchie's compilers even as K&R I was being published. Although a few early C compilers lacked struct assignment, all modern compilers support it, and it is part of the ANSI C standard, so there should be no reluctance to use it.

References: K&R I Sec. 6.2 p. 121; K&R II Sec. 6.2 p. 129; H&S Sec. 5.6.2 p. 103; ANSI Secs. 3.1.2.5, 3.2.2.1, 3.3.16.

58. How does struct passing and returning work?

A: When structures are passed as arguments to functions, the entire struct is typically pushed on the stack, using as many words as are required. (Pointers to structures are often chosen precisely to avoid this overhead.)

Structures are typically returned from functions in a location pointed to by an extra, compiler-supplied "hidden" argument to the function. Older compilers often used a special, static location for structure returns, although this made struct-valued functions nonreentrant, which ANSI C disallows.

Reference: ANSI Sec. 2.2.3 p. 13.

59. The following program works correctly, but it dumps core after it finishes. Why?

struct list
{
char *item;
struct list *next;
}

/* Here is the main program. */

main(argc, argv)
...

A: A missing semicolon causes the compiler to believe that main return a struct list. (The connection is hard to see because of the intervening comment.) Since struct-valued functions are usually implemented by adding a hidden return pointer, the generated code for main() actually expects three arguments, although only two were passed (in this case, by the C start-up code). See also question 101.

Reference: CT&P Sec. 2.3 pp. 21-2.

60. Why can't you compare structs?

A: There is no reasonable way for a compiler to implement struct comparison, which is consistent with C's low-level flavor. A byte-by-byte comparison could be invalidated by random bits present in unused "holes" in the structure (such padding is used to keep the alignment of later fields correct). A field-by-field comparison would require unacceptable amounts of repetitive, in-line code for large structures.

If you want to compare two structures, you must write your own function to do so. C++ would let you arrange for the == operator to map to your function.

References: K&R II Sec. 6.2 p. 129; H&S Sec. 5.6.2 p. 103; ANSI Rationale Sec. 3.3.9 p. 47.

61. I came across some code that declared a structure like this:
struct name
{
int namelen;
char name[1];
};

and then did some tricky allocation to make the name array act like it had several elements. Is this legal and/or portable?

A: This technique is popular, although Dennis Ritchie has called it "unwarranted chumminess with the compiler." The ANSI C standard allows it only implicitly. It seems to be portable to all known implementations. (Compilers which check array bounds carefully might issue warnings.)

62. How can I determine the byte offset of a field within a structure?

A: ANSI C defines the offsetof macro, which should be used if available; see . If you don't have it, a suggested implementation is

#define offsetof(type, mem) ((size_t) \
((char *)&((type *) 0)->mem - (char *)((type *) 0)))

This implementation is not 100% portable; some compilers may legitimately refuse to accept it.

See the next question for a usage hint.

Reference: ANSI Sec. 4.1.5.

63. How can I access structure fields by name at run time?

A: Build a table of names and offsets, using the offsetof() macro. The offset of field b in struct a is

offsetb = offsetof(struct a, b)

If structp is a pointer to an instance of this structure, and b is an int field with offset as computed above, b's value can be set indirectly with

*(int *)((char *)structp + offsetb) = value;





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Impressive Questions and Answers
Previous Resource: Accenture Aptitiude Questions
Return to Discussion Resource Index
Post New Resource
Category: Jobs & Interviews


Post resources and earn money!
 
Related Resources


Contact Us    Privacy Policy    Terms Of Use   

SpiderWorks Technologies Pvt Ltd. 2006 - 2007 All Rights Reserved.