C Language

C Language

Respected Sir,

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

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

please help me sir .

Thank you sir !!!

View Answers

June 14, 2011 at 7:24 PM

hi, Srinivasan

(i)In c offsetof() macro is defined in <stddef.h>. With the help of it you can compute the offset field in structure struct s as offsetof(type,field).

 #include <stddef.h> 
size_t offsetof(type, field); 

e.g.

struct st { int a, b; }; 
offsetof(struct st, b);

(char *) casting arranges that the offset so computed is a byte offset.

(ii)Maintain the offsets field computed by the offsetof() macro.

*(int *)((char *)stp + offsetf) = value;

in the above the value of f can be set indirectly, here stp is the pointer to an instance of the srurcture and the field is an int which has the offset offsetf.









Related Tutorials/Questions & Answers:
C Language
C Language  What's the right declaration for main()? Is void main() correct? in C language ? please help me sir ! Thank You
C language
C language  i want that when i hit any key only * to be print not the hit key in c language   The given example will display aestricks on hitting any key. #include "stdio.h" #include "conio.h" #include "ctype.h" int
Advertisements
C Language
C Language  Respected sir, Why does sizeof report a larger size than I expect for a structure type, as if there were padding at the end? help me sir
C Language
C Language  Respected Sir, How can I determine the byte offset of a field within a structure? How can I access structure fields by name at run time? please help me sir . Thank you sir
c language code
c language code  Dear Sir, Could you send me the c language codings for BOOK BANK
c-language pointer functions
c-language pointer functions  what is the execution process of character pointer functions with example
hotel management programme in c language?
hotel management programme in c language?  hotel management programme in c language
about a program in c language
about a program in c language   Write a program that inputs five different integers from the keyboard, then print the sum, the product, the smallest and the largest of these numbers. Use only single selection form of if statement
logic for c language
logic for c language  01 02 03 04 05 06 20 21 22 23 24 07 19 32 33 34 25 08 18 31 36 35 26 09 17 30 29 28 27 10 16 15 14 13 12 11
Change case in C language
Change case in C language  I want to right in .dat file through structure.For this i have defined following: struct student //structure defination... If user is typing b5678 ,i want to store as B5678. (C)for input i am using
C Language - Java Beginners
C Language  1. What are extension files? Give examples. 2. What is operating system? What are the different types available? 3. What is the purpose of operating system? 4. What is the function of operating system? 5
write a program to use for loop print **********========== in c language
write a program to use for loop print **********========== in c language  write a program to use for loop print **========== in c language
C++ programming language - Java Beginners
C++ programming language  int main() { int i = 1, c = 0, sum = 1,j ; scanf("%d", &j); while (i <= j) { sum = sum + (i /3+i/5); printf
c programming language - Java Beginners
c programming language  int main() { int i = 1, c = 0, sum = 1,j ; scanf("%d", &j); while (i <= j) { sum = sum + (i /3+i/5); printf("%d
C language-qsort and bsearch function - Ajax
C language-qsort and bsearch function  Explain qsort and bsearch...() #include #include #include #include char alpha[] = {'a','b','c...); if(p) printf(" %c is in alphabet\n", *p); else printf("is not in alphabet\n
C Language - Design concepts & design patterns
C Language  What is meant by %u in printf statement in c? for example,%d means integer value,like that what is %u?  Hi Friend, %u is used to print an unsigned integer. Unsigned integers are non-negative numbers
Main function parameter in C language - Ajax
Main function parameter in C language  Please let me know how... of fgetpos(): #include #include int main () { FILE * pFile; int c...==NULL) perror ("Error opening file"); else { c = fgetc (pFile
Write a C language program to read two matrices and multiply them?
Write a C language program to read two matrices and multiply them?  Write a C language program to read two matrices and multiply them?   Here is an example of multiplying two matrices using C language. #include <
Sample code for An ASCII Encryption and Decryption code in C-language only.
Sample code for An ASCII Encryption and Decryption code in C-language only... in ASCII format and only in C (NOT EVEN C++). Thank you.Please if possible post...; int choice; char fname[20],temp[20]={"file.txt"},c; clrscr
Ask C/C++ Questions online
language, but both the languages are popular among programmers. C is a general purpose programming language that can run on most of the platforms. C is used... Bell Laboratory while C++ is a popular object oriented programming language
Objective C Introduction
programming language. Objective C is very old programming language... in early 1980's. The Objective-C language is designed to enable a easier... to the C language. Objective C takes best features from C and smalltalk. Objective C
C++
C++  How can i write this in dev c
C++
C++  How can i write this in dev c
c#
c#  how to find out the size of the BMP image in C
c++
c++  use a prgrm as an example to xplain-: a)class b)object c)message d)cope resolution operator
bsearch() function in C
bsearch() function in C Programming Language This section demonstrates the use bsearch() function in C. This searches function the given key in the array...[] = { 'a', 'b', 'c', 'd' }; int comp(const void *a, const void
c++
c++  write a programme that calculates the area and circumference of a rectangle
c#
c#  how to find out the size of the BMP image
c++
c++  i use turbo c++...i want to change the background color...what is the command for it and the header file used
c++
c++  i use turbo c++...i want to change the background color...what is the command for it and the header file used
c++
c++  i use turbo c++...i want to change the background color...what is the command for it and the header file used
C++
C++  dear sir How to create windows form application for login screen using C++? USER Name -TESTADMIN Password -testuser
c++
c++  differenciate btw.-a)local & public variables b)pre-defined & user defined functions c)fnctn prototype,defination & fnctn code.use code examples
c++
c++  differenciate btw.-a)local & public variables b)pre-defined & user defined functions c)fnctn prototype,defination & fnctn code.use code examples
C++
C++  differenciate btw.-a)local & public variables b)pre-defined & user defined functions c)fnctn prototype,defination & fnctn code.use code examples
c++
c++  write a prgrm tht accepts 3 integer values,then with a function to swap the values
C++
C++  Describe the principle advantages of deploying a linked list versus a static array when implementing a Queue or a Stack
C++
C++  Trace the bubble sort using the following integers,which represent the elements in an array. 5,7,3,8,6,7,3
C++
C++  I have been asked to write a programme that gets five values,gets the sum,average and product please help me
C#
C#  i need a code that will make program perform the basic operations upon the data Listview save, update and cancel
c++
c++  write a program that gets a key or character from the keyboard and displays it ASII code in decimal, hexadecimal and binary form.It must also give a description of the key pressed
c++
c++  write a prgrm tht calculates the surface area of a cylinder.the prgrm shld find the circumfrance and have a function that calculates the fadius and another to calculate the surface area
C++
C++  write a prgrm tht gets five values,gets the sum,average &...; #include <conio.h> void main() { clrscr(); int a,b,c,d,e,sum; float average...;>b>>c>>d>>e; sum=a+b+c+d+e; average=sum/5; product=a*b*c*d
c++
c++  .write a program that accepts 5 subjects of a student,it should have a function to calculate the average & another function to grade.grade should be as follows-<40=E,40-49=D,50-59=C,60-69=B & >70
c++
c++  Consider the following function: int funcExercise7(int list[], int size) { int sum = 0; for (int index = 0; index < size; index++) sum = sum + list[index]; return sum
c++
c++  Consider the following declarations: class xClass { public: void... private members does class xClass have? c. How many constructors does class xClass.... Write a C++ statement that prints the values of the data members of the object
C#
C# register form  i want to generate a user id automatically after filling their register form and i want to show their id to their email? please reply me soon
c++
c++  Consider the following declarations: class xClass { public: void func(); void print() const; xClass (); xClass (int, double); private: int u... private members does class xClass have? c. How many constructors does class xClass
c++
c++  Write a console based C++ program that reads student information from a text file, build an array of objects of type class StudentInfo... name with the minimum GPA 7) Display Student GPAs as Letter Grades (A, B, C, D
c++
c++  .write a prgrm tht accepts 5 subjects of a student,it shld hv a function to calculate the average & another function to grade.grade shld be as follows-<40=E,40-49=D,50-59=C,60-69=B & >70

Ads