My intention: To loop through -- without using unions -- 30 members of a structure, all of type character array, storing in each the result of a call to itoa. In the following code, I name the structure members a-z,A-D. In the calling function, I initialize a string of those characters, called 'letters', then I try to loop through the structure members by referring to them with my increment variable as the index into letters. Then, I try to dump the contents of each member of the structure. **edit: I realize that the members wouldn't contain anything, given what code you can see. The problem seems to be with referring to struct members like this.
struct listArrays {
char a[10];
char b[10];
char c[10];
char d[10];
char e[10];
char f[10];
char g[10];
char h[10];
char i[10];
char j[10];
char k[10];
char l[10];
char m[10];
char n[10];
char o[10];
char p[10];
char q[10];
char r[10];
char s[10];
char t[10];
char u[10];
char v[10];
char w[10];
char x[10];
char y[10];
char z[10];
char A[10];
char B[10];
char C[10];
char D[10];
};
struct listArrays Ternaries;
int testTernary(){
char letters[30] = "ABCDabcdefghijklmnopqrstuvwxyz";
int i;
for(i = 0; i < 30; ++i){
dumpArray((Ternaries.(letters[i])), 10);
}
return 0;
}
The error I get is "expected identifier before '(' token."
Problems I have ruled out:
-The dumpArray function works fine.
-Looping through the letters works fine, outside the context of the
referral to struct members
Identifiers (names) only exist in the program text. Once compiled and linked, they no longer exist. So you can't "index" the struct for the letter. What you can do is:
struct listArrays {
char letters[30][10];
};
Now you can access these "letters":
dumpArray((Ternaries.letters[i]), 10);
Related
I've created this struct:
typedef struct {
char* id;
char* name;
int birthYear;
int finishedCourses;
double avarage;
int coursesNow;
int courses[MAX_COURSES_YEAR];
}Student;
and now I am trying to set an array of courses.
this is what I wrote:
s1.courses[] = {5,4,3,2};
and the error is:
student.c:15:13: error: expected expression before ‘]’ token
s1.courses[]={5,4,3,2};
int courses[]={5,4,3,2};
memcpy (s1.courses, courses, sizeof(courses));
Other way is to do so:
typedef struct {
char* id;
char* name;
int birthYear;
int finishedCourses;
double avarage;
int coursesNow;
int courses[];
}Student;
int courses[]={5,4,3,2};
Student *s = malloc(sizeof(Student)+sizeof(courses));
memcpy (s->courses, courses, sizeof(courses));
In this second case the advantage is that you alloc at runtime the very dimension for courses, you do not use padding space or statically fixed space for the field.
There are also other ways to do it.
Initialization of an array via an initializer list is only permissible upon declaration. So you can do:
int arr[] = {1,2,3,4,5};
But you cannot do:
int arr[5];
arr = {1,2,3,4,5};
You have to use a loop or memcpy:
int tmp[] = {1,2,3,4,5};
// method 1
for(int i = 0; i < sizeof(tmp) / sizeof(*tmp); i++) {
arr[i] = tmp[i];
}
// method 2
memcpy(arr, tmp, sizeof(tmp));
You can also take advantage of compound literals and dispense with tmp array:
memcpy(arr, (int []) {1,2,3,4,5}, sizeof((int []) {1,2,3,4,5}));
I am trying to pass a string array and indexes from where to start end searching in the array, I am unable to solve it from the last two days. I am sending to the pthread_create a struct data thread_data, here i am able to send the int and long data, but not the string array, can someone help me, how to pass these.
struct data{
int tid;
unsigned long start;
unsigned long end;
char * word;
char * str;
};
struct data thread_data[NUM_THREADS];
void *searchString(void *passeddata)
{
struct data *t_data;
int tid1;
char * str[3];
t_data=(struct data *) passeddata;
tid1=t_data->tid;
str=t_data->str;
.....
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
...
char work[]={"First Line","Second line","Third line"};
...
while(fgets(arr[index],120, fp)!=NULL){
index=index+1;
thread_data[index].tid=index;
thread_data[index].str=work;
...
rc=pthread_create(&threads[index],NULL,searchString,(void *)&thread_data[index]);
...
}
pthread_exit(NULL);
}
To hold the multiple strings you need 2D array.
const char *work[]={"First Line","Second line","Third line", "Fourth Line"};
You need to use pointer to pointer in struct data to hold the above array.
struct data{
.....
const char **str;
size_t lenOfStr;
};
And pass the length of array explicitly to thread function from main function.
Your sample code may look like below.
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
struct data{
int tid;
unsigned long start;
unsigned long end;
char * word;
const char **str;
size_t lenOfStr;
};
struct data thread_data[3];
void *searchString(void *passeddata)
{
struct data *t_data;
int tid1;
const char **str = NULL;
t_data=(struct data *) passeddata;
tid1=t_data->tid;
str=t_data->str;
int i = 0;
for (i = 0;i<t_data->lenOfStr;i++)
printf("%s\n", str[i]);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
const char *work[]={"First Line","Second line","Third line", "Fourth Line"};
int index = 0;
pthread_t threadid=0;
thread_data[index].tid=index;
thread_data[index].str=work;
thread_data[index].lenOfStr = sizeof(work)/sizeof(*work); // Calculate the size of work here
int rc=pthread_create(&threadid,NULL,searchString,&thread_data[index]);
pthread_exit(NULL);
}
I don't love this, but I have a struct with nearly 45 members inside; all are characters or character arrays. That said, I am going to need to optimize the way I initialize each struct. Ordinarily, I would pass the entire object into my init_struct() function, but I feel like that is not the best way to do this.
How would I create and use a pointer to the struct to accomplish this?
Old Method would look something like this:
void init_struct(struct general){
...initialize members...
}
int main(){
struct general[10];
for(int i = 0 ; i < 10 ; ++i){
init_struct(general[i];
}
}
Since this struct is so large, as I said nearly 45 members inside it, I think a point to the struct would go a long way in optimizing this process. How would I accomplish that?
Just in case you need, here is the typedef for my struct
typedef struct
{
//Basically, everything we want to read from HUDL should be here...
int play_num;
char down;
char dist[3];
char ydln[4];
char gnls[3];
char hash[3];
char home[20];
char away[20];
char odk[2];
char qtr[2];
char series[3];
char result[20];
char penalty[20];
char act_cb[20]; //How do they act post-snap
char act_dl[20];
char act_lb[20];
char act_ol[20];
char act_qb[20];
char act_rb[20];
char act_saf[20];
char aln_cb[20]; //How do they align pre-snap
char aln_dl[20];
char aln_lb[20];
char aln_ol[20];
char aln_qb[20];
char aln_rb[20];
char aln_saf[20];
char aln_wr[20];
char blitz[20];
char box_cnt[3];
char saf_count[20];
char coverage[20];
char cvr_basic[20];
char def_front[20];
char mtn_def[20];
char num_rush[3];
char off_form[20];
char form_var[20];
char motion[20];
char off_pro[20];
char off_play[20];
char play_var[20];
char personnel[20];
char play_type[20];
char time[2];
char score_diff[4];
char field_zone[2];
char dd_type[2];
char form_strength[2];
} HUDL; // MAXIMUM of 63 Members
There's a couple of things wrong on your code.
First of, your function definition is wrong because you omit the parameter name. Your function definition should look like this:
void init_struct(struct general mygeneralstruct){}
Alternatively, you could use an alias for your struct using typedef, like so:
typedef struct {
int a;
} general;
In which case, your function declaration could look like this:
void init_struct(general mygeneralstruct){}
You have the same problem when you declare your array of structures. You omit the name of your variable. Instead of
struct general[10];
it should be
struct general mygeneralstruct[10]
or
general mygeneralstruct[10](typedef)
Finally, you can't change your array of structures by passing each structure's value to the function. You need to pass each structure's address instead.
Your function declaration should then be(using typedef):
void init_struct(general* mygeneralstruct){}
and the code in the loop:
init_struct(&mygeneralstruct[i]);
To pass a pointer to your array element, you just prefix the parameter with &, make sure you declare the function correctly:
void init_struct(HUDL* pGeneral){
if ( pGeneral != NULL ) {
//This will ensure the entire structure contains '0'
memset(pGeneral, 0, sizeof(HUDL));
...initialize members...
}
}
int main(){
HUDL general[10];
for( int i=0; i<(sizeof(general) / sizeof(general[0])); i++ ) {
init_struct(&general[i]);
}
}
I'm not sure why you haven't used the typedef 'HUDL' makes life a lost easier and code easier to read.
A slightly cleaner and better approach would be to have a constructor and destructor function to allocate memory dynamically to structure and free it after use.
static void HUDL_destroy(HUDL* ptr)
{
if(ptr)
{
//...any other clean up that needs to be done goes here..
free(ptr);
}
}
static HUDL* HUDL_create()
{
HUDL* ptr = malloc(sizeof(HUDL));
if(!ptr)
return NULL;
//do initialization bits...
init_struct(ptr);
return ptr;
}
int main()
{
//allocate and initialise structure
HUDL *general = HUDL_create();
//do stuff...
//free structure after use
HUDL_destroy(general);
}
You might need an array of pointers in your case. So modify your main() accordingly.
int main()
{
//we need an array of structure pointers
HUDL* general[SIZE];
//allocate and initialize structure
for(int i=0; i<SIZE; i++)
general[i] = HUDL_create();
//do stuff...
//free structure after use
for(i=0; i<SIZE; i++)
HUDL_destroy( general[i] );
}
This is my code in which if student marks is greater than 85,scholarship status will be changed to sanctioned, but after updating it is not printing
#include<stdio.h>
#include<string.h>
struct scholor
{
char name[25];
int sem;
int marks;
char status;
};
void sanction(int m, char *s)
{
if(m>85)
{
char p[15]="sanctioned";
char *r;
r=p;
while(*r!='\0')
{
*s=*r;
s++;
r++;
}
*s='\0';
}
}
int main()
{
struct scholor s1;
scanf("%s%d%d%s",&s1.name,&s1.sem,&s1.marks,&s1.status);
sanction(s1.marks,&s1.status);
printf("%s",s1.status);
}
status is a single char but you are storing a string into it, effectively doing out of bounds access (undefined behaviour). Change it to an array and then you'll be able to copy.
struct scholor
{
char name[25];
int sem;
int marks;
char status[128];
};
and adjust the calls and passing (since status is an array now -- its name gets converted into a pointer t its first element when passed to functions):
scanf("%s%d%d%s",s1.name,&s1.sem,&s1.marks,s1.status);
sanction(s1.marks,s1.status);
printf("%s",s1.status);
Other suggestions:
1. Use a standard prototype for main such as: int main(void)
2. You could usr strcpy to copy the string as opposed to doing it yourself.
Your struct should have status as a character array not a character .Moreover when you scanf an array dont write & before because the name itself points to the assdress of the first element.Your corrected program is :
struct scholor
{
char name[25];
int sem;
int marks;
char status[16];
};
void sanction(int m, char *s)
{
if(m>85)
{
char p[15]="sanctioned";
char *r;
r=p;
while(*r!='\0')
{
*s=*r;
s++;
r++;
}
*s='\0';
}
}
int main()
{
struct scholor s1;
scanf("%s%d%d%s",s1.name,&s1.sem,&s1.marks,s1.status);
sanction(s1.marks,s1.status);
printf("%s",s1.status);
}
OK so I have erased everything trying to make this into a struct, because i messed it up bad.
I need this code of arrays to become a struct.
FILE *pFile;
int choice = 0;
char buf[40];
int id[sizeof(buf)];
char name[sizeof(buf)][20];
char state[sizeof(buf)][5];
char dis_code[sizeof(buf)];
float balance[sizeof(buf)];
char due_date[sizeof(buf)][40];
This is what I got do so far but when i try to use it it goes nuts. I still don't know how to load the file into it.
struct fileinfo
{
int id[10];
char name[20];
char state[5];
char dis_code[5];
float balance[10];
char due_date[40];
} info[sizeof(buf)];
Am i missing something or do i have the right idea. The problem is when i run this the same why i would the regular arrays I run into errors.
I'm not sure if this is your only issue, but you have changed the type of several of the fields.
int id[sizeof(buf)]; // id[i] is an int
char dis_code[sizeof(buf)]; // dis_code[i] is a char
float balance[sizeof(buf)]; // balance[i] is a float
struct fileinfo
{
int id[10]; // info[i].id is an _array of 10 ints_
char dis_code[5]; // info[i].dis_code is an _array of 5 chars_
float balance[10]; // info[i].balance is an _array of 10 floats_
} info[sizeof(buf)];
An array of a type and single instance of that type will behave quite differently.
I would suggest making the fields of the struct the same type as your original array elements, i.e.:
struct fileinfo
{
int id;
char dis_code;
float balance;
}
Since you declared char buf[40] ==> sizeof(buf) = 40. You are changing the 2 dimensional array into a single dimensional. The structure should be like
struct fileinfo
{
int id[40];
char name[40][20];
char state[40][5];
char dis_code[40];
float balance[40];
char due_date[40][40];
}