This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I want to create an array of structures:
typedef struct
{
int id;
int department;
}employee;
int main(int argc, char *argv[])
{
int i;
employee job[3]; ////////////////error
for (i=0;i<3;i++)
{
//do something
}
return 0;
}
the issue is that when I want to compile I got the following error:
syntax error before ';' token and points to the line that I marked
Any help?
Thanks
That code, as it stands alone, is fine, as per the following transcript:
pax> cat qq.c
typedef struct {
int id;
int department;
} employee;
int main (int argc, char *argv[]) {
int i;
employee job[3]; ////////////////error
for (i=0;i<3;i++) {
//do something
}
return 0;
}
pax> gcc -o qq qq.c
pax>
. Some things you may want to check are:
Has anything #defined something like employee or job?
Are you sure employee is spelt identically in both places?
Are you sure you have all those semicolons you claim to have?
Are there any "funny" characters in your code (ones that seem invisible but still stuff up your input stream to the compiler)?
A good first start would be to comment out the errant line and see if you get the error on the following line. That should narrow it down to either that line (no error) or a previous line (still have error).
You can see the entire file in hex mode (looking for funny characters) if you do something like:
od -xcb myfile.c
(under Linux - Windows I'm not sure though, if you have gvim, you can convert it to hex with a menu item).
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to convert assembly to machine code. I have a MINGW compiler where if i type ./convert.exe mov %a then it should output 0x01 0xc0. I am thinking of using a struct listing each assembly code with its corresponding machine value. At the moment i keep getting errors like "request for member opcode in something not a structure". Any help would be appreciated.
#include <stdio.h>
#include <string.h>
struct _Instruction {
char mnemonic[10];
unsigned char opcode;} typedef Instruction;
Instruction instruction_list[] = {
{"mov", 0x01},
{"add", 0x04},
{"sub", 0x05},
{"mul",0x06},
{"div", 0x07},
{"and",0x08},
{"or",0x09},
{"xor",0x0a},
{"cmp",0x0b},
{"",-1},
};
Instruction get_inst(char mnemonic[]);
int main2(int argc, char *argv[])
{
char* instruction = argv[1];
Instruction get_inst = get_Instruction(instruction);
printf("%s ; %s",instruction_list.mnemonic,instruction_list.opcode);
return 0;
}
Instruction get_inst(char mnemonic[])
{
int i;
for(i=0; instruction_list[i].opcode != -1; i++)
{
if(!strcmp(instruction_list[i].mnemonic, mnemonic))
{
return instruction_list[i];
}
}
return instruction_list[i];
}
For one thing, your structure is declared incorrectly. You should format it like this instead:
typedef struct _Instruction {
....
} Instruction;
I'm not sure why that's not triggering syntax errors, but it's certainly not helping.
Also, you have both a variable and a function named get_inst. You call a non-existent function named get_Instruction(). You probably meant to name your function get_Instruction().
Also, the .opcode member of your structure is a single char. Your printf statement uses the "%s" format specifier to print it. This expects a string, which will cause printf to continue reading past the end of the .opcode member, displaying unpredictable garbage and accessing memory it shouldn't be touching.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I want to extract the words from a file (and later, from console input), count their appearances and store them in my Word structure:
typedef struct cell{
char *info; /* word itself */
int nr; /* number of appearances of the word *
}*Word;
This structure will be allocated dynamically for as many words are contained in the file. Consider this function:
void Word_Allocation (Word* a) /* The function that allocates space for one structure */
My questions are:
How do I correctly open a file and read it line by line?
How do I correctly store words and number of appearances in my structure?
As for file io, this is the basics.
As for the algorithm, since you are not using C++, so map is not available which is trivial for this problem. A straightforward solution in C might be:
Allocated an array of cell and read in words
sort the array on char *info.
count
Your allocator function should return a Word* and receive a size to allocate for the word itself. Something like this, perhaps:
Word * Word_Allocation (size_t size) {
Word *w = malloc(sizeof(*w));
if (w) w->info = malloc(size);
if (!w->info)
{
free(w);
w = NULL;
}
return w;
}
You can read a word at a time with:
#define STR(x) #x
enum {MAX_BUF = 100};
char buf[MAX_BUF];
fscanf(infile, "%" STR(MAX_BUF) "s", buf);
And then strlen(buf)+1 is the size to pass to Word_Allocation. Or you can pass buf and have Word_Allocation call strlen and copy the data over.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have spent ages trying to figure out what is the problem with the following program:
typedef struct user {
char host[40];
int order;
} user;
typedef struct data {
user userdat;
} data;
int read_user (char *datname, user *userdat) {
...
fscanf (datin, "%s", &userdat->host);
fscanf (datin, "%d", &userdat->order);
//1
printf ("%d\n", userdat->order);
...
}
void init_data (data *dat) {
init_userdat (&dat->userdat);
}
void init_userdat (user *userdat) {
*userdat->host = "127.0.0.1";
userdat->order = 0;
}
int user_print (int i, data *dat) {
//2
printf ("%d\n", dat->userdat.order);
}
int main(int argc, char *argv[]) {
...
data dat;
init_data (&dat);
read_user (datname, &dat->userdat);
user_print (&dat);
}
The program is very simplified to highlight the relevant sections. What happens is that the first print statement (//1) outputs the value correctly, while the second (//2) does not - it outputs something that looks like a possible memory location.
I have tried numerous combinations of accessing the stored variable, but I just can't crack it. Any help would be appreciated.
Edit1: Fixed up a couple of non essential errors in code (not relating to pointers or structs)
Final Edit: Thank you all for your help. The issue that Arun Saha pointed out was indeed in the original code and is now fixed. However, the problem of printing two different strings persisted. Your assurance that the code should otherwise compile and work led me to discover the true culprit - I was not properly initializing another part of the otherwise complex struct and this resulted in overwriting of the user.order variable.
The following line does not do what it appears to do :-)
*userdat->host = "127.0.0.1";
userdata->host is a pointer to the first character in the host[40] array. The above statement would copy only one character from the source string ("127.0.0.1"). To copy the entire string, use the standard library function stncpy()
strncpy( & userdata->host[ 0 ], "127.0.0.1", 40 );
In your main function, when you invoke read_user (datname, &dat->userdat);, I feel there should be a compilation issue. This should be actually read_user (datname, &dat.userdat); as dat is not a pointer, but an object itself.
With this change and Arun's previous recommendation, I have tried your program and it works.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have a menu and sub-menu. I have created a structure where I have store menu and submenu names in an array. Trying to print them, but I think I am wrong somewhere.
#include <stdio.h>
#include <stdlib.h>
struct calculator
{
int x_loc;
int y_loc;
char main_menu[15];
char sub_menu[15];
char icon_title[100];
};
char *main_menu_names[3] = {"Link_1", "Link_2", "Link_3"};
char *sub_menu_names[3] = {"Sub_Link_1", "Sub_Link_2", "Sub_Link_3"};
struct calculator info = {219, 120, main_menu_names, sub_menu_names, "Title"};
int main()
{
printf("X location: %d\n",info.x_loc);
printf("Y location: %d\n",info.y_loc);
printf("Menu Name: %s\n",info.main_menu);
printf("sub menu: %s\n",info.sub_menu);
printf("icon_title: %s\n",info.icon_title);
system("PAUSE");
return 0;
}
Your code doesn't make any sense.
You can't initialize struct calculator's char main_menu[15] field with menu_names, which is undefined. I don't believe you could compile the above.
DEFINITION IS SINGLE ARRAY
char main_menu[15];
char sub_menu[15];
But u have equated to two dimensional array
char *main_menu_names[3] = {"Link_1", "Link_2", "Link_3"};
char *sub_menu_names[3] = {"Sub_Link_1", "Sub_Link_2", "Sub_Link_3"};
in
struct calculator info = {219, 120, menu_names, sub_menu_names, "Title"};
It should be like
struct calculator info = {219, 120, menu_names[0][0], sub_menu_names[0][0], "Title"};
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
[EDIT:]
I have removed a lot of code now. And I have reduced it to a more straight forward question.
Is it OK, to pass this variable...
char Record_Info[file_length + 1];
to another function, like this:
listdir(tempdesc, 0, sockid, Record_Info);
which uses this header:
int listdir(char *dirname, int lvl, int sockid, char Record_Info[])
And the listdir() can call its-self many times?
.
* Orig Question *
I was wondering if another set of eyes can look at this please. It was working, but I added some more code (probably more sprintf, near the bottom) and now a Segmentation fault occurs.
This program creates/sends the web page, correctly, but crashes when it returns from the send_recordings_list() function back to the parsing_request() function. I don't think its necessary to understand how its formatting the data from the file, but I included that bit anyway.
For the most part, I'm using static variables, and when I attempt to free() any thing, it causes another seg fault, or gcc lib fault. Also I am unsure if passing the variables between the functions() might be causing the issue?
Also worth mentioning, a different function (not shown in here) sends out a web page which is about 4MB, I can call this one many times and it does not crash. I have placed a few // ****** at some lines which might be of interest?
Oh yes, I have not done much standard error handling yet... (I'll do it later), so assume its a spherical chicken in a vacuum.
.
So it starts at parsing_request()
goes to send_recordings_list()
goes to listdir() . (this calls its self many times)
back to send_recordings_list()
. (seg fault here)
back to parsing_request()
.
I'm hoping some will go 'ahh i see what you did fool......'
Embedded Linux 2.6.27, GCC 4.2.4, 32MB Ram
void parsing_request(int sockid, char *buff)
{
char *res_line=malloc(MAXLINE), path[MAXLINE], *line;
// Cut code
if (strcmp(line, "/recordings_body.htm") == 0)
{
send_recordings_list (sockid); // ****** IT GOES HERE 1st ******
return;
}
free (res_line);
return;
}
int send_recordings_list(int sockid)
{
int hnd;
int hnd2;
char tempdesc[MAX_PATH_LENGTH];
// Copy all of the data <1 MiB (Slow reading)
int file_length = lseek (hnd2, 0, SEEK_END);
char Record_Info[file_length + 1]; // ******
lseek (hnd2, 0, SEEK_SET);
read (hnd2, Record_Info, file_length);
close (hnd2);
// Cut out code
del_file_cnt = 0;
sprintf (tempdesc, "%s/Recordings", TOPPY_DIR);
listdir(tempdesc, 0, sockid, Record_Info); // ***** Major 2nd call here
return 0;
}
int listdir(char *dirname, int lvl, int sockid, char Record_Info[])
{
int i;
DIR* d_fh;
struct dirent* entry;
char longest_name[4096];
char tempdesc[4096], morespace[128];
int row_col;
char chan_name[128], alt_name[128], desc[500], category[128], rec_time[14], start_time[14], end_time[14];
char trim_file_name[128], trim_file_name2[128], trim_alt_name[128], file_links[1535];
char logo[128];
int length, u_score, dot;
char *looky_pos1, *looky_pos2;
int is_match;
struct tm tm_rec, tm_start, tm_end;
time_t tim_rec, tim_start, tim_end;
// Cut out code
return 0;
}
Sorry, this is a hopeless mess. Learn how to use Linux tools like valgrind and gdb. Check carefully that the memory you request is eventually freed once and after its use is done. Check that you don't pass pointers to local variables out of functions. Check that your strings are long enough to accomodate the data expected (or check as part as your as of now nonexistent error handling).
One debugging strategy that is suprisingly effective is the Teddy Bear Consultant: Get a teddy bear and explain your problem to it step by step. Only if that doesn't help is a real human warranted. [It works because sorting out your understanding to explain it forces you to really think it through.]
After looking quickly at your code, you have two options:
Learn how to handle errors, how to manage resources and how to avoid global variables
Use a programming language which does it for you
Both will take a long time but using the second approach, you will be much more productive.
I suggest to try to implement the above with Python just to get a feeling.
Answering your most recent post, asking if you can pass the character value into the function... I don't understand what you're trying to do. In the function prototype you have the final parameter as a char variable_name[], are you trying to pass a array in? If so, why not use a pointer?
so the prototype would like like:
int listdir(char *dirname, int lvl, int sockid, char* Record_Info);
Passing by reference always tends to give you a little bit more control, whether you want it or not. Are you trying to pass in a specific element of an array?