I declared an array of struct and initialized it at compile time.
Now, for unit testing purposes, I would like to initialize it from a function which I can call from main() and from my unit tests.
For some reason, probably involving a 16 hour coding marathons & exhaustion, I can't figure it out.
So assuming you have
struct foo {
int a;
int b;
};
struct foo foo_array[5] = {
{ 0, 0 }, { 1, 1 }, { 2, 2 }
};
int main() {
memcpy(foo_array, some_stuff, sizeof(foo_array)); // should work
...
OR you could:
int main() {
int i;
for ( i = 0; i < sizeof(foo_array)/sizeof(struct foo); i++ ) {
init(&foo_array[i]);
}
}
but without looking at your code it's hard to say what's causing the trouble... i am SURE it's probably something very trivial you are overlooking because you are tired and have been at it for 16 hours.
typedef struct {
int ia;
char * pc;
} St_t;
void stInit(St_t * pst) {
if (!pst)
return;
pst->ia = 1;
pst->pc = strdup("foo");
/* Assuming this function 'knows' the array has two elements,
we simply increment 'pst' to reference the next element. */
++ pst;
pst->ia = 2;
pst->pc = strdup("bar");
}
void foo(void) {
/* Declare 'st' and set it to zero(s)/NULL(s). */
St_t st[2] = {{0}, {0}};
/* Initialise 'st' during run-time from a function. */
stInit(st);
...
}
see this one:
struct Student
{
int rollNo;
float cgpa;
};
int main()
{
const int totalStudents=10;
Student studentsArray[totalStudents];
for(int currentIndex=0; currentIndex< totalStudents; currentIndex++)
{
printf("Enter Roll No for student # %d\n" , currentIndex+1);
scanf("%d\n", &studentsArray[currentIndex].rollNo);
printf("Enter CGPA for student # %d\n", currentIndex+1);
scanf("%d\n", &studentsArray[currentIndex].cgpa);
}
}
Related
I'm doing dining-philosopher problem in C for assignment. And got stuck very begining of my code.
I decided each philosopher to be structure, and forks to be int array.
But I can't use global variable in this assignment.
So, I have to include shared variable in philosopher structure to pass them for arguments of thread routine.
Here is my problem - how to include int array in structure if I can't know proper size of them when initializing?
My plan is just include pointer variable in structure then allocate array's address using &.
But It doesn't work :
#include <stdlib.h>
/* inside structure*/
typedef struct s_share {
int **forks;
} t_share;
/* outside structure */
typedef struct s_philo {
t_share *share;
} t_philo;
int main(void)
{
t_philo *philo;
int *forks;
int i;
i = 0;
/* malloc structure arrary philo, size = 10 */
philo = (t_philo *)malloc(sizeof(t_philo) * 10);
/* malloc int arrary forks, size = 100 */
forks = (int *)malloc(sizeof(int) * 100);
while (i < 10)
{
philo[i].share->forks = &forks; //error
i++;
}
}
Output : segmentation fault
I tested share->forks size like this :
printf("size of forks : %ld\n", sizeof(philo->share->forks));
Output was 8.
It's enough size to store int * pointer.
Through this I know It's not the memory allocation problem.
Then what is problem? Can someone check this for me?
Edit :
When I try to malloc directly philo->share->forks, I got same error.
typedef struct s_share {
int *forks;
} t_share;
typedef struct s_philo {
t_share *share;
} t_philo;
int main(void)
{
t_philo *philo;
int *forks;
int i;
i = 0;
philo = (t_philo *)malloc(sizeof(t_philo) * 10);
while (i < 10)
{
philo[i].share->forks = (int *)malloc(sizeof(int) * 100); //error
i++;
}
}
I thought it's because when philo initialized, sizeof operator calculated forks's memroy to be 8 - which required for pointer.
Is there something wrong?
Edit 2 :
To clear my question,
It's easy to solve this problem, if I write size of array in structure definition.
typedef struct s_share {
int forks[100];
} t_share;
typedef struct s_philo {
t_share *share;
} t_philo;
but according to my assignmet I have to get philosopher's number from cmd. So I can't do that.
Above is simple version of my origin code
Sorry, Edit 2 is wrong :
typedef struct s_share {
int forks[100];
} t_share;
typedef struct s_philo {
t_share *share;
} t_philo;
int main(void)
{
t_philo *philo;
t_share *share;
int *forks;
int i;
i = 0;
philo = (t_philo *)malloc(sizeof(t_philo) * 10);
while (i < 10)
{
philo[i].share->forks[i] = 1;
i++;
}
}
Output
zsh: segmentation fault ./a.out
I still got segfault when I write array size in struct definition.
I used calloc to initialize all member in my struct but same error occurs :
typedef struct s_share {
int **forks;
} t_share;
typedef struct s_philo {
t_share *share;
} t_philo;
int main(void)
{
t_philo *philo;
t_share *share;
int *forks;
int i;
i = 0;
philo = (t_philo *)calloc(10, sizeof(t_philo));
forks = (int *)calloc(100, sizeof(int));
while (i < 10)
{
philo[i].share->forks = &forks; //error
i++;
}
}
Edit 4:
I finally found error. It's because I didn't malloc 'share' struct in philo struct
typedef struct s_share {
int **forks;
} t_share;
typedef struct s_philo {
t_share *share;
} t_philo;
int main(void)
{
t_philo *philo;
int *forks;
int i;
i = 0;
philo = (t_philo *)malloc(sizeof(t_philo) * 10);
forks = (int *)malloc(sizeof(int) * 100);
while (i < 10)
{
philo[i].share = (t_share *)malloc(sizeof(t_share)); //here
philo[i].share.forks = &forks;
i++;
}
}
That one line -allocating struct share- solved problem.
Or, I can modify philo struct definition like this :
typedef struct s_philo {
t_share share; //not pointer, just struct
} t_philo;
In this way, I can automatically malloc struct share.
I got confused in this point. Thanks for helping!
this line
philo[i].share->forks
Is dereferencing the pointer 'share' which is not set. You called malloc and did not set any values, so the data inside your allocated buffer is 'garbage' data.
// add begin
t_share* new_share = (t_share*)malloc(sizeof(t_share));
philo[i].share = new_share;
// add end
// don't use &forks
philo[i].share->forks = forks; //error
i++;
// need forks++
forks++;
I have difficulty applying the pass by reference and pass by value separation in structs.How can I swap the elements of the fixed size struct array as below.
struct try{
int num;
char name[10];
};
int main(){
struct try book[3];
void swapper(/********/);// <-what should be the argument of this function
}
void swapper(/********/){//swap second and third element of struct array
/*how the swap may be done?
temp=book[2];
book[2]=book[3];
temp=book[3];*/
}
There are a lot of ways to do what you're asking. One approach:
#include <stdio.h>
struct try {
int num;
char name[10];
};
void
swapper(struct try *a, int b, int c)
{
struct try tmp = a[b];
a[b] = a[c];
a[c] = tmp;
}
void
display(const struct try *t, size_t count)
{
while( count-- ){
printf("%d: %s\n", t->num, t->name);
t += 1;
}
}
int
main(void) {
struct try book[] = {
{ 1, "foo"},
{ 2, "bar"},
{ 3, "baz"}
};
display(book, sizeof book / sizeof *book);
swapper(book, 1, 2);
display(book, sizeof book / sizeof *book);
return 0;
}
Well I am wanting to change the way my structures are written, currently I use array and I need to limit its use, but I wanted a way to create a dynamic array that is the size of the reading done, without always having to edit the array value.
Current Code:
struct sr_flag {
int value_flag;
};
struct er_time {
int value_time;
};
struct se_option {
struct sr_flag flag[50];
struct er_time time[50];
};
struct read_funcs
struct se_option *option;
void (*option_func) (void);
...
}
struct read_funcs func_;
struct read_funcs *func;
int sr_flags(int i, int fg, int val) {
if(i < 0)
return 0;
return func->option[i].flag[fg].value_flag = val;
}
void option_func(void) {
struct se_option fnc;
fnc.option = malloc(500 * sizeof(*(fnc.option)));
}
void read_fnc() {
func = &func_;
func->option = NULL;
func->option_func = option_func;
}
I look for a way to remove the array amount [50] instead each time the sr_flags function is executed the limit is raised
Example: sr_flags function executed 1x array would be [1] if executed 2x would be [2]
I also think about doing the same with the option_func function
I tried using the following more unsuccessfully
struct se_option {
struct sr_flag *flag;
struct er_time time[50];
};
int sr_flags(int i, int fg, int val) {
if(i < 0)
return 0;
func->option[i].flag = malloc(1 * sizeof(*(func->option[i].flag)));
return func->option[i].flag[fg].value_flag = val;
}
int main () {
for(int i < 0; i < 10; i++)
sr_flags(i, 1, 30);
return 0;
}
I'm not 100% certain on what it is you want but I think you just want to call realloc and increase the size by the amount you provide. And that's very easy to do, as for the values you want with the arrays I'm not sure so I just used a placeholder value.
#include <stdio.h>
#include <stdlib.h>
struct sr_flag {
int value_flag;
};
struct er_time {
int value_time;
};
struct se_option {
struct sr_flag* flag;
struct er_time* time;
};
void allocateflags(struct se_option* options, int size, int val){
options->flag = realloc(options->flag, size*sizeof(struct sr_flag));
struct sr_flag* flag = options->flag+size-1;
flag->value_flag = val;
}
void allocatetime(struct se_option* options,int size, int val){
options->time = realloc(options->time, size*sizeof(struct er_time));
struct er_time* time = options->time+size-1;
time->value_time = val;
}
void displayflagvalues(struct se_option* options,int size){
for(int index = 0; index < size ; ++index){
printf("flag: %i\n",options->flag[index].value_flag);
}
}
void displaytimevalues(struct se_option* options, int size){
for(int index = 0; index < size ; ++index){
printf("time: %i\n",options->time[index].value_time);
}
}
int main(){
struct se_option options = {0};
for(int index = 0; index < 10; ++index){
allocateflags(&options, index,index);
allocatetime(&options, index,index);
}
displayflagvalues(&options, 10);
displaytimevalues(&options,10);
return 0;
}
The code creates an se_option structure wheren sr_flag and er_time pointers are null. Then there's two functions one allocateflags and the other allocatetime, both of which call realloc with the size you provide. When you call realloc, all previous memory is copied over to the new array. Also free is called automatically by realloc.
This step
struct sr_flag* flag = options->flag+size-1;
flag->value_flag = val;
struct er_time* time = options->time+size-1;
time->value_time = val;
Is slightly redundant but it was just to show the newest array can hold the value. If you understand pointer arithmetic, all its doing is incrementing the pointer to the last position then subtracting 1 struct size and setting that value. Basically setting the value of the final array in the pointer.
Is there a way to loop through structs and assign a value to their members during the process?
I'm not really sure if I composed the question correctly, so I'll try showing it in code, that is of course invalid, but hopefully serves as better example:
struct example {
int x;
/* ... */
};
struct example s1;
struct example s2;
int *structs[] = {
s1.x,
s2.x
};
int main(void) {
for (int i = 0; i < 2; i++) {
*structs[i] = i;
}
return 0;
}
Basically, I need to automate the process of assigning values to multiple structures, but I don't know how. Is this even possible in C?
If you fix a bunch of trivial syntax errors, you can come up with:
struct example
{
int x;
/* ... */
};
struct example s1;
struct example s2;
int *structs[] = { &s1.x, &s2.x };
int main(void)
{
for (int i = 0; i < 2; i++)
{
*structs[i] = i;
}
return 0;
}
Alternatively, you could use an array of pointers to structures:
struct example
{
int x;
/* ... */
};
struct example s1;
struct example s2;
struct example *examples[] = { &s1, &s2 };
enum { NUM_EXAMPLES = sizeof(examples) / sizeof(examples[0]) };
int main(void)
{
for (int i = 0; i < NUM_EXAMPLES; i++)
{
examples[i]->x = i;
// ...
}
return 0;
}
Both compile — both work.
I would like to ask question regarding pointers. I dont know what to do here, I have a method in the main that is calling a method outside the main, and I need to use pointers.Basically this is the rough draft of it: Thanks!
char *book[] = { "x", "y", "z",};
int number[] = { 1, 2, 3};
struct data{ char *bookname; int booknumber;};
struct data *list[3];
my_method(char *x, int y, int *z)
{
//creating a new struct
list[(*z)++] = (struct data*) malloc( sizeof(struct data) );
//assigning arguments
list[(*z)++]->bookname = x;
list[(*z)++]->booknumber = y;
(*z)++;
}
int main()
{
int nextValue = 0;
my_method(book[nextValue], book[nextValue], &nextValue);
int i;
for(i = 0; i < 3; i++)
{
function(book[i], number[i]);
printf("name: %c number: %d", list[i]->bookname, list[i]->booknumber);
}
}
It looks like you are passing the wrong arguments to your method. Try changing the following line:
my_method(book[nextValue], number[nextValue], &nextValue);
In addition, in your method you seem to be incrementing z four times, which I doubt is the behavior you want. You should only increment it once at the end, like the following:
//creating a new struct
list[*z] = (struct data*) malloc( sizeof(struct data) );
//assigning arguments
list[*z]->bookname = x;
list[*z]->booknumber = y;
(*z)++;
Try the following code. Simply removed unnecessary static void.
//declaration of method outside the main
void my_method(............,int *nextValue)
{
//................................//
//..............................//
//then increment the pointer
(*nextValue)++;
}//my_method
int main()
{
int nextValue = 0;
//Removed static void
my_method(............, &nextValue);
//........................................//
}//main