I know that when defining a an enum you can enumerate through the list numerically:
typedef enum MONTH { Jan = 1, Feb, March, ... };
Can you enumerate through values in a struct the same way? I basically want to loop through the values in a struct using a for or while loop.
struct items {char *item_name, int item_value};
struct items Items_list[] =
{
"item 1", 2000,
"item 2", 3600,
....
};
Language used is C.
Edit: I may have just answered my own question since what I had in mind is an array of structs. Will leave the question up for now however.
This declaration and initializer combination are invalid. (The question changed while the original version of this answer was written.)
If you are asking "is there a way to access the first member of the structure, then the second, without knowing the structure element names", then the answer is 'no, not without careful encoding beforehand'.
The careful coding involves multiple steps. For each element, you need an encoding of the type, the offset of the member in the structure, and perhaps the size of the member (if the encoding of the type does not give that to you anyway):
typedef enum { MT_INT, MT_CHAR_PTR, ... } MemberType;
typedef struct MemberAccess
{
const char *name;
size_t offset;
MemberType type;
} MemberAccess;
static const MemberAccess members[] =
{
"item_name", offsetof(struct items, item_name), MT_CHAR_PTR },
"item_value", offsetof(struct items, item_value), MT_INT },
};
And now, with excruciating care, you can write code to either get or set the value in the Nth member of a struct item pointed at by a particular pointer. However, doing so is still far from trivial.
int get_int(const void *data, const MemberAccess *member)
{
assert(member->type == MT_INT);
return (*(const int *)((const char *)data + member->offset));
}
GCC notwithstanding, you need the cast to a character pointer; you cannot legitimately do pointer arithmetic on void *.
You might then invoke:
int value = get_int(&Items_list[1], &members[1]);
to get at the integer value of the second field of the second element of the array.
This is so excruciating to deal with that there have to be excellent reasons to go through the overhead. There can be such reasons. I know of a system with 400 configuration parameters (which is a problem in its own right, but lets pretend that's OK; they've accumulated over 20 years of development) stored in a structure with heterogeneous types for the members. The code that manipulates it is written out 400 times - ouch! - because it doesn't use a system driven off an analogue of the MemberAccess structure. The code would be a lot more compact than it currently is because there are about a dozen data types to deal with, so most of the code is repetitive. Another way of reducing the complexity of that code would be to make everything into a string, but there are issues with that transformation too.
No, you cannot iterate the elements of a struct. The best you can do is hardcode the names of the struct in the loop:
struct items *item = Items_list;
while (item < Items_list + sizeof(Items_list) / sizeof(*Items_list)) {
printf("%s %d", item->item_name, item->item_value);
++item;
}
Also note that you cannot reliably iterate an enum either, because it could be defined like this:
typedef enum MONTH { Jan = 1, Feb = 13, March = 10, ... };
And the elements are both out of order and non-continuous (i.e. there are gaps in the numbers).
One way to do this, when you have a pointer type in the inner struct, and when that pointer cannot be meaningfully NULL, is to do something like:
for (int i=0; Item_List[i].item_name != 0; i++) {
// do whatever
}
If you don't have a handy pointer type, a "sentinel" value can often be used to mark the last record.
You'll need to remember to add a null element/sentinel at the end of your structure array though, and fix your syntax.
No, there's no easy way to do this. A struct is not a numerical value, so you can't loop through its values/members. You can either use an array instead of a struct and access its memebers with a simple for loop, or write a special enumerator callback function which takes the struct as its one argument, a number as another argument, and using case or if statements, looks up each member of the structure.
Related
I would like to get some help on C-style struct. I do not have much understanding in C so I am not familiar with the basics.
Right now, I have a struct (outerStruct) that has other structs as members (innerStruct1, innerStruct2...). What I need to do is to get values of MEMBER VARIABLES OF innerStruct1, innerStruct2....
The only information I have is the pointer to outerStruct and a data structure that has all the possible variable types of MEMBER VARIABLES OF EACH innerStructs. Note that all the variable names are just variable type
+ "_val" so if I can types of member variables, I will be able to get variable names as well.
I need to get the information on the MEMBERS OF the innerStructs and parse them into a text file, s.t variable name and variable value would show up.
Can I get the member variable of the innerStruct without using variable names? Since C plays with the memory allocation, maybe this is possible? If this is not possible, what are possible information that I can get?
The container of variable names and types is quite large so if I use all values to retrieve information, it will do too many null checks, and the time will be linear to the size of the container. Instead, I would like to know if I can take advantage of the naming convention and make the code more efficient.
struct outerStruct {
innerStruct1 innerStruct1_val;
innerStruct2 innerStruct2_val;
}
struct innerStruct1 {
int int_val = 5;
char char_val = 'a';
}
struct innerStruct2 {
float float_val = 4.0;
double double_val = 5.0;
}
Expected output(print to screen):
"innerStruct1_val : int_val = 5, char_val = 'a' || innerStruct2_val : float_val = 4.0, double_val = 5.0"
TL;DR I want to get values of the inner struct from a nested struct without using the actual variable names.
(Also, the code is here to only show the lay out of the structure. Correct syntax will be used.)
In C, you have to manually walk the members of respective structures. But, even if you want to keep the structure internals "private", by only making the structure tag name public, you can provide a public function to print the function.
struct outerStruct;
void print_outerStruct(const struct outerStruct *);
Likewise for the innter structures.
Since you have to define the complete outer structure someplace for it to be useful, that place would also be the place to provide the implementation of its print function.
struct outerStruct {
struct innerStruct1 innerStruct1_val;
struct innerStruct2 innerStruct2_val;
};
void print_outerStruct (const struct outerStruct *os) {
printf("outerStruct (%p) { ", os);
printf("%s: ", "innerStruct1_val");
print_innerStruct1(&os->innerStruct1_val);
printf("%s: ", "innerStruct2_val");
print_innerStruct2(&os->innerStruct2_val);
printf(" }\n");
}
With sufficient scaffolding, you may be able to generate these print functions mechanically. For example, it might require writing a parser for your structures.
I am taking the operating system class in mit online, I completed the first assignement http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-828-operating-system-engineering-fall-2012/assignments/MIT6_828F12_assignment1.pdfbut what surprised me is how they return the data structures, they work with a data structure and they return a smaller data structure, and to use it they just cast it back. I see that it can optimize the code but is this safe ? is it good practice ?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct small
{
int n ;
};
struct big
{
int n ;
char * string ;
};
struct small* f()
{
struct big* x = malloc(sizeof(struct big));
x->n = 'X';
x->string = strdup("Nasty!");
return (struct small*) x ;
}
int main(int argc, char *argv[])
{
struct big *y = (struct big*)f();
printf("%s\n",y->string);
}
EDIT 1 : here is the link from mit, i just replicated the idea in my own code.
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-828-operating-system-engineering-fall-2012/assignments/sh.c
No structure is returned, but a pointer to a structure. A pointer contains the address of the memory where the actual object is located. (In this case it has been allocated dynamically with malloc, hence has dynamic storage duration and will live until the pogram ends or a free is called on the pointer.)
The code is legal and has defined semantics. 6.3.2.3/7 of the n1570 draft says
A pointer to an object type may be converted to a pointer to a
different object type. If the resulting pointer is not correctly
aligned for the referenced type, the behavior is undefined. [does not apply here. -ps]
Otherwise, when converted back again, the result shall compare equal
to the original pointer.
Note that the structures could be completely unrelated (the first data element does not need to be the same type). The cast could even be to a built-in type like int.
The issue may be different if the object were accessed through a pointer of the wrong type (which it isn't) because of aliasing issues.
To answer your questions:
Is it safe?
Obviously not. Just casting the type of a pointer to something else is something you should only do if you are positively sure it's the right type.
However, if you know that, everything is fine.
Is it good practice?
No, not in this shape.
You sometimes do Object Oriented Programming in C in a similar way (compare CPython's PyObject): You use one "base" object, and one "type" struct which have a structure like:
struct obj_type {
const char* name;
size_t length; // this is important so that you can copy the object later on without knowing its insides
};
struct obj_base {
obj_type* type;
};
Because it is guaranteed that in C, pointers to structs point to the address of their first element, you can use further objects that build atop of that:
struct more_complex_object {
obj_type* type;
int value;
};
...
int main() {
obj_type *more_complex_object_type = malloc(sizeof(obj_type));
more_complex_object_type->name = "A more complex object type";
more_complex_object_type->length = sizeof(more_complex_object);
more_complex_object *obj = malloc(more_complex_object_type->length);
obj->type = more_complex_object_type;
obj->value = 10;
...
//let's now use it as a simple instance of the "super" object
obj_base* just_an_object = (obj_base*)more_complex_object;
//when we copy things, we make sure to copy the full length:
obj_base* target = malloc(sizeof(more_complex_object));
memcpy(target, just_an_object, just_an_object->type->length);
//now, when handling an object, we can check its type
if(just_an_object->type == more_complex_object_type) {
more_complex_object *my_obj = (more_complex_object)just_an_object;
printf("value: %d\n", my_obj->value);
}
}
Yes it is safe, as long your compiler follows the C standard
Why doesn't GCC optimize structs?
But no, I do not consider this a good practice.
Good practices are relative, and should never be use as a rule. This construction is required if you want to simulate OO inheritance behavior with C.
I'm wondering for a solution to the below problem. Please help.
Problem:
struct s{
int a;
int b;
}st;
I want a function to initialize the values at runtime. The problem is that I want to make it generic, so I want to pass the member name as input to the function and get it initialized.
fun_init(char* mem, int val)
{
// Find offset of the member variable in struct 'st'
// Assign the value
}
One straight solution is to use string comparision on the member name. But if I happen to add some extra variables at a later time, I'll have to modify the function, which I don't want.
Hope I was able to frame the ques clearly.
Thanks
C does not provide a way to find a symbol by name. See this thread for more information.
The simplest solution here is to use an associative array.
Read this thread if you need to mix value-types. (In your example, all value types are int, so you might not need this.)
void fun_init(int *storage, int val) {
*storage = val;
}
void something_else(void) {
struct s {
int a;
int b;
} st;
fun_init(&st.a, 42);
}
However, if you need to dynamically determine the key name, you are doing something wrong. If you need to store key/value pairs, perhaps you would be interested in the hashtable.
I'm guessing you want to initialize struct from either user input or persistency.
A solution involves creating an associative array as mentioned by #Domi.
The array is filled with key/value pairs such as (const char*, unsigned).
The key is the name of struct member and the value is the offset from the start of the struct.
Each struct will need to have a function that initializes the above array. You can get an offset to a member via the offsetof macro.
This will NOT work with structs that have bit fields (sub byte named members).
I'm really new to C programming and I'm still trying to understand the concept of using pointers and using typedef structs.
I have this code snippet below that I need to use in a program:
typedef struct
{
char* firstName;
char* lastName;
int id;
float mark;
}* pStudentRecord;
I'm not exactly sure what this does - to me it seems similar as using interfaces in Objective-C, but I don't think that's the case.
And then I have this line
pStudentRecord* g_ppRecords;
I basically need to add several pStudentRecord to g_ppRecords based on a number. I understand how to create and allocate memory for an object of type pStudentRecord, but I'm not sure how to actually add multiple objects to g_ppRecords.
defines a pointer to the struct described within the curly bracers, here is a simpler example
typedef struct {
int x;
int y;
}Point,* pPoint;
int main(void) {
Point point = {4,5};
pPoint point_ptr = &point;
printf("%d - %d\n",point.x,point_ptr->x);
pPoint second_point_ptr = malloc(sizeof(Point));
second_point_ptr->x = 5;
free(second_point_ptr);
}
The first declares an unnamed struct, and a type pStudentRecord that is a pointer to it. The second declares g_ppRecords to be a pointer to a pStudentRecord. In other words, a pointer to a pointer to a struct.
It's probably easier to think of the second as an "array of pointers". As such, g_ppRecords[0] may point to a pStudentRecord and g_ppRecords[1] to another one. (Which, in turn, point to a record struct.)
In order to add to it, you will need to know how it stores the pointers, that is, how one might tell how many pointers are stored in it. There either is a size somewhere, which for size N, means at least N * sizeof(pStudentRecord*) of memory is allocated, and g_ppRecords[0] through g_ppRecords[N-1] hold the N items. Or, it's NULL terminated, which for size N, means at least (N+1) * sizeof(pStudentRecord*) of memory is allocated and g_ppRecords[0] through g_ppRecords[N-1] hold the N items, and g_ppRecords[N] holds NULL, marking the end of the string.
After this, it should be straightforward to create or add to a g_ppRecords.
A struct is a compound data type, meaning that it's a variable which contains other variables. You're familiar with Objective C, so you might think of it as being a tiny bit like a 'data only' class; that is, a class with no methods. It's a way to store related information together that you can pass around as a single unit.
Typedef is a way for you to name your own data types as synonyms for the built-in types in C. It makes code more readable and allows the compiler to catch more errors (you're effectively teaching the compiler more about your program's intent.) The classic example is
typedef int BOOL;
(There's no built-in BOOL type in older ANSI C.)
This means you can now do things like:
BOOL state = 1;
and declare functions that take BOOL parameters, then have the compiler make sure you're passing BOOLs even though they're really just ints:
void flipSwitch(BOOL isOn); /* function declaration */
...
int value = 0;
BOOL boolValue = 1;
flipSwitch(value); /* Compiler will error here */
flipSwitch(boolValue); /* But this is OK */
So your typedef above is creating a synonym for a student record struct, so you can pass around student records without having to call them struct StudentRecord every time. It makes for cleaner and more readable code. Except that there's more to it here, in your example. What I've just described is:
typedef struct {
char * firstName;
char * lastName;
int id;
float mark;
} StudentRecord;
You can now do things like:
StudentRecord aStudent = { "Angus\n", "Young\n", 1, 4.0 };
or
void writeToParents(StudentRecord student) {
...
}
But you've got a * after the typedef. That's because you want to typedef a data type which holds a pointer to a StudentRecord, not typedef the StudentRecord itself. Eh? Read on...
You need this pointer to StudentRecord because if you want to pass StudentRecords around and be able to modify their member variables, you need to pass around pointers to them, not the variables themselves. typedefs are great for this because, again, the compiler can catch subtle errors. Above we made writeToParents which just reads the contents of the StudentRecord. Say we want to change their grade; we can't set up a function with a simple StudentRecord parameter because we can't change the members directly. So, we need a pointer:
void changeGrade(StudentRecord *student, float newGrade) {
student->mark = newGrade;
}
Easy to see that you might miss the *, so instead, typedef a pointer type for StudentRecord and the compiler will help:
typedef struct { /* as above */ } *PStudentRecord;
Now:
void changeGrade(PStudentRecord student, float newGrade) {
student->mark = newGrade;
}
It's more common to declare both at the same time:
typedef struct {
/* Members */
} StudentRecord, *PStudentRecord;
This gives you both the plain struct typedef and a pointer typedef too.
What's a pointer, then? A variable which holds the address in memory of another variable. Sounds simple; it is, on the face of it, but it gets very subtle and involved very quickly. Try this tutorial
This defines the name of a pointer to the structure but not a name for the structure itself.
Try changing to:
typedef struct
{
char* firstName;
char* lastName;
int id;
float mark;
} StudentRecord;
StudentRecord foo;
StudentRecord *pfoo = &foo;
Often stacks in C are dependent upon datatype used to declare them. For example,
int arr[5]; //creates an integer array of size 5 for stack use
char arr[5]; //creates a character array of size 5 for stack use
are both limited to holding integer and character datatypes respectively and presumes that the programmer knows what data is generated during the runtime. What if I want a stack which can hold any datatype?
I initially thought of implementing it as a union, but the approach is not only difficult but also flawed. Any other suggestions?
I would use a structure like this:
struct THolder
{
int dataType; // this is a value representing the type
void *val; // this is the value
};
Then use an array of THolder to store your values.
This is really just a variant of Pablo Santa Cruz' answer, but I think it looks neater:
typedef enum { integer, real, other } type_t;
typedef struct {
type_t type;
union {
int normal_int; /* valid when type == integer */
double large_float; /* valid when type == real */
void * other; /* valid when type == other */
} content;
} stack_data_t;
You still need to use some way to explicitly set the type of data stored in each element, there is no easy way around that.
You could look into preprocessor magic relying on the compiler-dependent typeof keyword to do that automagically, but that will probably not do anything but ruin the portability.
Some people have suggested a void* member. In addition to that solution I'd like to offer an alternative (assuming your stack is a linked list of heap-allocated structures):
struct stack_node
{
struct stack_node *next;
char data[];
};
The data[] is a C99 construct. data must be the last member; this takes advantage of the fact that we can stuff arbitrary quantities after the address of the struct. If you're using non-C99 compiler you might have to do some sketchy trick like declare it as data[0].
Then you can do something like this:
struct stack_node*
allocate_stack_node(size_t extra_size)
{
return malloc(sizeof(struct stack_node) + extra_size);
}
/* In some other function... */
struct stack_node *ptr = allocate_stack_node(sizeof(int));
int *p = (int*)ptr->data;
If this looks ugly and hacky, it is... But the advantage here is that you still get the generic goodness without introducing more indirection (thus slightly quicker access times for ptr->data than if it were void* pointing to a different location from the structure.)
Update: I'd also like to point out that the code sample I give may have problems if your machine happens to have different alignment requirements for int than char. This is meant as an illustrative example; YMMV.
You could use macros and a "container" type to reduce "type" from being per-element, to whole-container. (C99 code below)
#define GENERIC_STACK(name, type, typeid, elements) \
struct name##_stack { \
unsigned int TypeID; \
type Data[elements]; \
} name = { .TypeID = typeid }
Of course, your "TypeID" would have to allow every possible agreed-upon type you expect; might be a problem if you intend to use whole structs or other user-defined types.
I realize having a uniquely named struct type for every variable is odd and probably not useful... oops.
I created an library that works for any data type:
List new_list(int,int);
creates new list eg:
List list=new_list(TYPE_INT,sizeof(int));
//This will create an list of integers
Error append(List*,void*);
appends an element to the list. *Append accpts two pointers as an argument, if you want to store pointer to the list don't pass the pointer by pointer
eg:
//using the int list from above
int a=5;
Error err;
err=append(&list,&a)
//for an list of pointers
List listptr=new_list(TYPE_CUSTOM,sizeof(int*));
int num=7;
int *ptr=#
append(&listptr,ptr);
//for list of structs
struct Foo
{
int num;
float *ptr;
};
List list=new_list(TYPE_CUSTOM,sizeof(struct Foo));
struct Foo x;
x.num=9;
x.ptr=NULL;
append(&list,&x);
Error get(List*,int);
Gets data at index specified. When called list's current poiter will point to the data.
eg:
List list=new_list(TYPE_INT,sizeof(int));
int i;
for(i=1;i<=10;i++)
append(&list,&i);
//This will print the element at index 2
get(&list,2);
printf("%d",*(int*)list.current);
Error pop(List*,int);
Pops and element from the specified index
eg:
List list=new_list(TYPE_INT,sizeof(int));
int i;
for(i=1;i<=10;i++)
append(&list,&i);
//element in the index 2 will be deleted,
//the current pointer will point to a location that has a copy of the data
pop(&list,2);
printf("%d",*(int*)list.current);
//To use the list as stack, pop at index list.len-1
pop(&list,list.len-1);
//To use the list as queue, pop at index 0
pop(&list,0);
Error merge(List ,List);
Merges two list of same type. If types are different will return a error message in the Error object it returns;
eg:
//Merge two elements of type int
//List 2 will come after list 1
Error err;
err=merge(&list1,&list2);
Iterator get_iterator(List*);
Get an iterator to an list. when initialized will have a pointer to the first element of the list.
eg:
Iterator ite=get_iterator(&list);
Error next(Iterator*);
Get the next element of the list.
eg:
//How to iterate an list of integers
Iterator itr;
for(itr=get_iterator(&list); ite.content!=NULL; next(ite))
printf("%d",*(int*)ite.content);
https://github.com/malayh/C-List