syntax error Help!!

This is a discussion on syntax error Help!! within the C Programming forums, part of the General Programming Boards category; I was working on a simple example of IPC using the approach of Shared memory in C. And I have ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    5

    syntax error Help!!

    I was working on a simple example of IPC using the approach of Shared memory in C. And I have test it in the linux environment. It turned out that there could be some syntax problems with the code. I am not familiar with C programming language, and could not pinpoint the error of the code. Please help me. Any help would be appreciated. Thanks a lot. By the way, the error I got is near at the line containing main(). At least, that's what the compiler told me.


    Server.c
    Code:
    #include <sys/types.h>
    #include <sys/shm.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <curses.h>
    #include <string.h>
    #include <stdlib.h>
    #include <data.h>
    int main() {
        //shared memroy segment ID
        int segment_id;
        //shared memory buffer
        Message*  buffer;
        //Allocate a shared memory segment
        segment_id = shmget(IPC_PRIVATE, sizeof(*buffer), S_IRUSR | S_IWUSR);
        //Attach the shared memory segment
        buffer = (Message*) shmat(segment_id, NULL, 0);
        //print the shared memory segment id on the screen
        print("Shared memory segment ID is %d\n", segment_id);
        
        while (true) {
            for(; ; ) {
                sleep(100);
                if(buffer->ID==0) break;
            }    
            //process the message sent by the client
            //Reverse the message string sent by the client
            char* strpt = strrev(buffer->str);
            //sent back the result to the client
            buffer->str == strpt;
            buffer->ID = 1;
        
            if(buffer->ID==-1) {
                //Detach the shared memory segment
                shmdt(buffer);
                //Remove the shared memory segment
                shmctl(segment_id, IPC_RMID, NULL);
                break;
            }
        }
        
        return 0;
    }
    
    //Function to reverse a string 
    char* strrev(char* str) {
    
    
      int length = strlen(str);
      char* result = malloc(length + 1);
      if( result != NULL ) {
        result[length] = '\0';
        for ( int i = length - 1, int j=0;   i >= 0;   i--, j++ )  
            result[j] = string[i];
      }
      return result;
    }



    client
    Code:
    #include <sys/types.h>
    #include <sys/shm.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <curses.h>
    #include <string.h>
    #include <stdlib.h>
    #include <data.h>
    
    
    int main( ){
    
    
        //shared memory segment ID
        int segment_id;
        //shared memory buffer
         Message * buffer;
        //Manual selection of segment ID
        printf("Enter the shared memroy segment ID: ");
        scanf("%d", &segment_id);
        //Attach the shared memory segment
        buffer = ( Message* ) shmat(segment_id, NULL, 0);
        
        //Enter the Message to be sent to the client
        while(true) {
            printf("Enter the message you want to swap: ");
            gets(buffer->str);
            buffer->ID = 0;
            while(true) {
                //wait for the server to process the messa 
                    sleep(100);
                    if(buffer->ID==1) break;
            }
            //display the message replied by the server
            printf("The server reply: %s\n", buffer->str);
    
    
            //Manual selection of quit the connection and exit
            printf("Do you want to continue? Press Y to continue, N to exit.");
            char* command;
            gets(command);
            if(command=="N"|| command=="n") break;
        }
        
        //Notify the server to close the connection
        buffer->ID = -1;
        //Detach the shared memory segment
        shmdt(buffer);
        
        return 0;
    }


    The Message is simply a struct
    Code:
    struct Mystruct {
       /*Message status: 1-processed by the server. 
                         0-not processed by the server.
                         -1-notify the server to close the connection. */
       int ID;
       //string to store the message string
       char str[255];
    }Message;

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I know this is new to you... but when you're getting error messages it's generally a good idea to post them for us to see...

    Also most compiler error messages are very explicit. They will have the filename and line number the error occurred on as well as a description of the error, so it's usually just a matter of actually reading what they say...

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by liudaisuda View Post
    Server.c
    Code:
    #include <sys/types.h>
    #include <sys/shm.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <curses.h>
    #include <string.h>
    #include <stdlib.h>
    #include <data.h>
    int main() {
        //shared memroy segment ID
    client
    Code:
    #include <sys/types.h>
    #include <sys/shm.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <curses.h>
    #include <string.h>
    #include <stdlib.h>
    #include <data.h>
    
    int main( ){
    The items in red are most likely your errors, e.g. something along the lines of "couldn't find data.h". The < and > designation tells the compiler to look in its system include folders to resolve the location of the header files. In your case, data.h is most likely something you created and hence should be referenced as #include "data.h". Additionally, take a read through How to define main()-FAQ
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    Thanks, I have fixed the problem. But here's two more errors with my code
    Code:
    #include <sys/types.h>
    #include <sys/shm.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <curses.h>
    #include <string.h>
    #include <stdlib.h>
    #include "data.h"
    int main( ){
    //shared memory segment ID
    int segment_id;
    //shared memory buffer
    Message * buffer = NULL;
    //Manual selection of segment ID
    printf("Enter the shared memroy segment ID: ");
    scanf("%d", &segment_id);
    //Attach the shared memory segment
    buffer = ( Message* ) shmat(segment_id, NULL, 0);
    //Enter the Message to be sent to the client
    while(true) {
    printf("Enter the message you want to swap: ");
    gets(buffer->str);
    buffer->ID = 0;
    while(true) {
    //wait for the server to process the messa 
    sleep(100);
    if(buffer->ID==1) break;
    }
    //display the message replied by the server
    printf("The server reply: %s\n", buffer->str);
    //Manual selection of quit the connection and exit
    printf("Do you want to continue? Press Y to continue, N to exit.");
    char* command;
    gets(command);
    if(command=="N"|| command=="n") break;
    }
    //Notify the server to close the connection
    buffer->ID = -1;
    //Detach the shared memory segment
    shmdt(buffer);
    return 0;
    }
    Then the error is
    Name:  error_client.JPG
Views: 550
Size:  20.2 KB


    While for the server file, I got the similar errors with one more. Please take a look at it, if you can
    Code:
     
    
    #include <sys/types.h>
    #include <sys/shm.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <curses.h>
    #include <string.h>
    #include <stdlib.h>
    #include "data.h"
    int main() {
    //shared memroy segment ID
    int segment_id;
    //shared memory buffer
    Message * buffer=NULL;
    //Allocate a shared memory segment
    segment_id = shmget(IPC_PRIVATE, sizeof(*buffer), S_IRUSR | S_IWUSR);
    //Attach the shared memory segment
    buffer = (Message * ) shmat(segment_id,NULL,0);
    //print the shared memory segment id on the screen
    print("Shared memory segment ID is %d\n", segment_id);
    while (true) {
    for(;;) {
    sleep(100);
    if(buffer->ID==0) break;
    } 
    //process the message sent by the client
    //Reverse the message string sent by the client
    char* strpt = * strrev(buffer->str);
    //sent back the result to the client
    buffer->str == strpt;
    buffer->ID = 1;
    if(buffer->ID==-1) {
    //Detach the shared memory segment
    shmdt(buffer);
    //Remove the shared memory segment
    shmctl(segment_id, IPC_RMID, NULL);
    break;
    }
    }
    return 0;
    }
    //Function to reverse a string 
    char* strrev(char* str) {
    int length = strlen(str);
    char* result = malloc(length + 1);
    if( result != NULL ) {
    result[length] = '\0';
    int i,j;
    for ( i = length - 1, j=0; i >= 0; i--, j++ ) 
    result[j] = str[i];
    }
    return result;
    }
    The corresponding error is
    Name:  server_error.JPG
Views: 607
Size:  31.1 KB

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    Please help

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You couldn't wait 1 minute for a reply?

    So what don't you understand? "undeclared" means you are trying to use a name of something without telling the compiler what that name is (you are pretending you have a variable when you don't). That usually means you forgot to make it, or you spelled it wrong. Conflicting types means you have the same name trying to be two different things.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,287
    This was originally posted in the more appropriate "Linux Programming" forum (Shared memory IPC Help!!!), where, it seems, they were also too impatient for a reply. Interestingly enough, I replied to that thread this morning, but it appears the post never went through. *sigh*. It went something like this:


    @liudaisuda:
    Please properly indent your code, so we can read it. Not that we really need to. You are severely lacking in some C fundamentals (like structs and typedefs, and string handling basics like don't compare strings with ==). You need to do some serious studying and really make sure you have a solid understanding of C programming before tackling something like shared memory. Sit down with some tutorials, like ours here, and some others you Google for, and even crack open a few books. Work through them all, doing all the examples. After that, the answers to your questions will be trivial. It wouldn't hurt if you had a cursory knowledge of Linux processes and Linux memory architecture as well.


    You should also compile with your warnings set to the maximum. In gcc, this is the "-Wall" flag:
    Code:
    $ gcc -Wall client.c
    client.c: In function ‘main’:
    client.c:13: error: ‘buffer’ undeclared (first use in this function)
    client.c:13: error: (Each undeclared identifier is reported only once
    client.c:13: error: for each function it appears in.)
    client.c:18: error: expected expression before ‘)’ token
    client.c:26: warning: implicit declaration of function ‘sleep’
    client.c:35: warning: comparison with string literal results in unspecified behavior
    client.c:35: warning: comparison with string literal results in unspecified behavior
    1. Variables must be declared before they're used. This looks like a valid declaration, but really there's a problem in data.h.
    2. You need to #include the right file for sleep(). The documentation for sleep() will tell you what that is.
    3. You can't compare strings with ==, you need a function like strcmp()
    Code:
    $ gcc -Wall server.c
    server.c: In function ‘main’:
    server.c:13: error: ‘buffer’ undeclared (first use in this function)
    server.c:13: error: (Each undeclared identifier is reported only once
    server.c:13: error: for each function it appears in.)
    server.c:17: error: expected expression before ‘)’ token
    server.c:19: warning: implicit declaration of function ‘print’
    server.c:22: warning: implicit declaration of function ‘sleep’
    server.c:27: warning: implicit declaration of function ‘strrev’
    server.c: At top level:
    server.c:42: error: conflicting types for ‘strrev’
    server.c:27: note: previous implicit declaration of ‘strrev’ was here
    1. Again, declare before you use, and again, it looks valid, but it's the result of the same problem in data.h
    2. The "expected expression" error is also because of the error in data.h
    3. print is not a valid function, printf is
    4. Again, find the correct include file for sleep()
    5. Move this above main(). C reads top-down and only knows about what it has already seen.
    As for data.h?
    Code:
    struct MyStruct {
        ...
    }Message;
    This defines a global variable called Message, that is of type MyStruct. It does not make a type called "Message" that is a synonym for "struct MyStruct". For that you need to use typedef. Read up on how it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error "in function 'main' syntax error before 'int' Help Please
    By blackhat11907 in forum C Programming
    Replies: 5
    Last Post: 08-20-2011, 07:05 PM
  2. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  3. error C2061: syntax error : identifier
    By maninboots in forum C++ Programming
    Replies: 4
    Last Post: 07-02-2009, 05:40 AM
  4. error C2143: syntax error : missing ')' before ';'
    By steve1_rm in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 11:06 AM
  5. GCC compiler giving syntax error before 'double' error
    By dragonmint in forum Linux Programming
    Replies: 4
    Last Post: 06-02-2007, 05:38 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21