LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-30-2009, 07:06 PM   #1
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Blog Entries: 24

Rep: Reputation: 43
Unhappy C compiling error: expected expression before ‘{’ token


Hi. I have a compiling error which doesn't seem to want to go away. I suppose I could try to do it another way, but it seems really annoying that it is not working and I'm not sure why.

The code is compiled with gcc. I'm just compiling the object code now - not yet linking it into the program. Here is the compiler error:

Code:
bash-3.00$ gcc -c menu_browser.c
menu_browser.c: In function ‘create_menu_browser_ctrl’:
menu_browser.c:10: error: expected expression before ‘{’ token
Here is the offending source file, menu_browser.c:

Code:
#include <stdlib.h>
#include <pthread.h>
#include "menu_data.h"
#include "menu_browser.h"
#include "disp_thread_modes.h"

struct Menu_Browser_Ctrl * create_menu_browser_ctrl(MenuItem_List_Bundle * menuitem_list_bundle)
{
  struct Menu_Browser_Ctrl * menu_browser_ctrl = malloc(sizeof(struct Menu_Browser_Ctrl));
  menu_browser_ctrl->mode_h = PTHREAD_MUTEX_INITIALIZER;
  menu_browser_ctrl->mode = th_m_dead;
  menu_browser_ctrl->menuitem_list_bundle = menuitem_list_bundle;
  return menu_browser_ctrl;
}
If I comment out the line with "PTHREAD_MUTEX..." in it, the source compiles fine, so I'm (wildly) guessing that this has something to do with the PTHREAD_MUTEX_INITIALIZER macro.

Here is menu_browser.h:

Code:
#ifndef _MENU_BROWSER_
#define _MENU_BROWSER_

#include <pthread.h>
#include "menu_data.h"

struct Menu_Browser_Ctrl {
  pthread_mutex_t mode_h;
  char mode;
  struct m_i_l_b * menuitem_list_bundle;
};

struct Menu_Browser_Ctrl * create_menu_browser_ctrl(MenuItem_List_Bundle * menuitem_list_bundle);

#endif
"menu_data.h" is kind of long, but it provides typedef struct
MenuItem_List_Bundle. "disp_thread_modes.h" simply provides a few constants.
 
Old 03-30-2009, 07:42 PM   #2
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375

Original Poster
Blog Entries: 24

Rep: Reputation: 43
Solved... I think

I think I figured out what was wrong. The problem is that PTHREAD_MUTEX_INITIALIZER is, well, an initializer... and I was trying to use it differently.

Here is the definition from pthread.h:

Code:
# define PTHREAD_MUTEX_INITIALIZER \
  { { 0, 0, 0, 0, 0, 0, { 0, 0 } } }
So I adjusted the code to look like so:
Code:
struct Menu_Browser_Ctrl * create_menu_browser_ctrl(MenuItem_List_Bundle * menuitem_list_bundle)
{
  struct Menu_Browser_Ctrl * menu_browser_ctrl = malloc(sizeof(struct Menu_Browser_Ctrl));
  pthread_mutex_t mode_h = PTHREAD_MUTEX_INITIALIZER;
  menu_browser_ctrl->mode_h = mode_h;
  menu_browser_ctrl->mode = th_m_dead;
  menu_browser_ctrl->menuitem_list_bundle = menuitem_list_bundle;
  return menu_browser_ctrl;
}
And this seems to compile. I wanted to just have a default initialization inside the struct itself, but that didn't seem to compile. (Is that allowed?)

Last edited by CoderMan; 03-30-2009 at 07:49 PM. Reason: Blah, stupid typos!
 
Old 03-30-2009, 08:42 PM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
As you probably already realize, this has nothing to do with whether it's in a struct.

It has everything to do with the difference between initializing a variable as you declare it and assigning to it after it's been declared.

To see the difference in action, run this shell script:
Code:
cat > 1.c <<EOD; cat -n 1.c; gcc -Wall -pthread 1.c -o 1
#include <stdlib.h>
#include <pthread.h>

int main(void)
{
  pthread_mutex_t yyy=PTHREAD_MUTEX_INITIALIZER;
  yyy=PTHREAD_MUTEX_INITIALIZER;

  return 0;

} /* main() */
The first use of PTHREAD_MUTEX_INITIALIZER is correct; the second is not. When I ran this, I got the following output:
Code:
     1  #include <stdlib.h>
     2  #include <pthread.h>
     3
     4  int main(void)
     5  {
     6    pthread_mutex_t yyy=PTHREAD_MUTEX_INITIALIZER;
     7    yyy=PTHREAD_MUTEX_INITIALIZER;
     8
     9    return 0;
    10
    11  } /* main() */
1.c: In function 'main':
1.c:7: error: expected expression before '{' token
This has nothing to do with POSIX threads particularly, and everything to do with initializing structs.

If you wish to initialize a mutex at a point other than when you declare it, a good way is to use pthread_mutex_init().

Hope this helps.
 
Old 03-31-2009, 04:31 PM   #4
icarus127
LQ Newbie
 
Registered: Aug 2007
Distribution: Slackware 12.2
Posts: 22

Rep: Reputation: 15
Quote:
Originally Posted by CoderMan View Post
Code:
struct Menu_Browser_Ctrl * create_menu_browser_ctrl(MenuItem_List_Bundle * menuitem_list_bundle)
{
  struct Menu_Browser_Ctrl * menu_browser_ctrl = malloc(sizeof(struct Menu_Browser_Ctrl));
  pthread_mutex_t mode_h = PTHREAD_MUTEX_INITIALIZER;
  menu_browser_ctrl->mode_h = mode_h;
  menu_browser_ctrl->mode = th_m_dead;
  menu_browser_ctrl->menuitem_list_bundle = menuitem_list_bundle;
  return menu_browser_ctrl;
}
And this seems to compile. I wanted to just have a default initialization inside the struct itself, but that didn't seem to compile. (Is that allowed?)
The trouble with this solution is the copy of the mutex. According to the pthread man pages operations performed on a copied mutex are undefined. In order to safely initialize a non-static mutex you must use pthread_mutex_init() as wje_lq suggested.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
compile error: expected ‘;’ before ‘*’ token osc~ Linux - Software 12 12-10-2009 11:18 AM
compile error: expected ‘;’ before ‘*’ token osc~ Linux - Software 2 12-05-2008 08:52 AM
compiling error: unexpected token `fi' rem1986 Linux - Kernel 7 12-10-2007 01:11 PM
expected primary expression before... Uint character question. RHLinuxGUY Programming 3 05-12-2006 12:08 PM
Error upgrading kernel, /sbin/mkinitrdi integer expression expected amp2000 Linux - General 2 12-05-2003 05:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration