Thread: error: expected ';' before '{' token

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    8

    error: expected ';' before '{' token

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    struct node 
    {   int vp;
        int pf;
        int accessed;
    } *pt = NULL; 
    
    
    typedef struct node pt_type;
    
    
    
    
    int pm_size;
    int page_size;
    int replace;
    int num_rows;
    int last_row = -1;
    void buildtable()
    {
        int i;
    
    
        printf("Total page frame area size(words):    ");
              scanf("%d",&pm_size);
              printf("%d\n", pm_size );
              printf("Page size(words/page):   ");
                scanf("%d", &page_size);
                printf("%d\n", page_size);
                printf("Replacement policy(0=LRU, 1=FIF0):   ");
                scanf("%d", &replace);
                printf("%d\n", replace);
            
                num_rows = pm_size / page_size;
                
                
                pt = (pt_type *)(malloc(num_rows * sizeof(pt_type)));
    
    
                for (i = 0; i < num_rows; i++)
                {
                    pt[i].vp = -1;
                    pt[i].accessed = -1;
                }
        return;
    }
    void mapping()
    {     
        int vm_addr;
        int i;
        int j;
        int temp_pf;
        int k;
        int vp;
        int offset;
        printf("Virtual address:  ");
        scanf("%d", &vm_addr);
        printf("%d\n", vm_addr);
    
    
    
    
        vp = vm_addr / page_size;
        offset = vm_addr % page_size;
       
        while ((i < num_rows) && (pt[i].vp != -1) && (pt[i].vp != vp))
        {
            pt[i].accessed++;
            i++;
        }
        if (i == num_rows)
        {
            temp_pf = pt[0].pf;
            for (j = 0; j <= num_rows - 2; j++)
            {
                pt[j] = pt[j + 1];
            }
            pt[num_rows - 1].vp = vp;
            pt[num_rows - 1].pf = temp_pf;
            pt[num_rows - 1].accessed = 0;
            printf("Page fault!\n");
        }
        else if (pt[i].vp == -1)
        {
            pt[i].vp = vp;
            pt[i].pf = i;
            pt[i].accessed = 0;
            last_row++;
            printf("Page fault!\n");
        }
        ---> else (pt[i].vp == vp)
        {
            if (replace == 0)
            {
                temp_pf = pt[i].pf;
                    for (k = i; k <= last_row - 1; k++)
                    {
                        pt[k] = pt[k + 1];
                        PT[k].accessed++;
                    }
                    pt[last_row].vp = vp ;
                    pt[last_row].pf = temp_pf ;
                    pt[last_row].accessed = 0;
                    
            }
            pm_addr = temp_pf * page_size + offset;
            printf("Virtual adrress matches physical address");
        }
    
    
    
    
    
    
    }
    
    
    void printmap()
    {
        int i;
        for (i = 0; i <= last_row; i++)
        {
    
    
            printf("VP|\n");
            printf("PF|\n");
            printf("Accessed|\n");
        }
    
    
    
    
    }
    void delete_table()
    {
    
    
        if (pt != NULL)
            free(pt);
        return;
    }
    int main()
    {
        int selection;
        while (selection != 4) {
            printf("Virtual memory to Main memory mapping:\n");
            printf("------------------------------\n");
            printf("1) Set parameters\n");
            printf("2) Map virtual address\n");
            printf("3) Print page table\n");
            printf("4) Quit\n");
            printf("\n");
    
    
            printf("Enter selection: ");
            scanf("%d", &selection);
            printf("%d\n", selection);
    
    
            switch (selection)
            {
                case 1: {
                buildtable();
                break;
                }
                case 2: {
                mapping();
                break;
                }
                case 3: {
                printmap();
                break; }
    
    
                case 4:
                {
                delete_table();
                break;
                }
            }
        }
        printf("Program Quit\n");
    
    
    
    
    
    
        return 1;
    }
    under my mapping functionat the else pt[i].vp == vp) it is giving me this error I have looked up and down in my code and I just cannot seem to find the missing semicolon. If anybody could spot it or if there is something bigger that I am missing if you could let me know that would be appreciated.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    The else statements have no conditions.

  3. #3
    Registered User
    Join Date
    Feb 2020
    Posts
    8
    My condition is my if statement above

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    Code:
      ---> else (pt[i].vp == vp)
    Again else statements have on conditions, the above should just be else.

  5. #5
    Registered User
    Join Date
    Feb 2020
    Posts
    8
    Quote Originally Posted by jimblumberg View Post
    Code:
      ---> else (pt[i].vp == vp)
    Again else statements have on conditions, the above should just be else.
    so change else if to else ? does the if also get changed as well??

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    Do you see any if in that line? It either needs to be a "bare" else or else if(pt[i].vp == vp). An else all by it's self has no conditions.

  7. #7
    Registered User
    Join Date
    Feb 2020
    Posts
    8
    okay i was able to make it run thanks for the help on that unfortunately now i am getting segmentation fault on the else if (pt[i] == -1) im not sure why could you help me on that ? its right above the second else if

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    Please post your current code, inside code tags.

  9. #9
    Registered User
    Join Date
    Feb 2020
    Posts
    8
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct node 
    {   int vp;
    	int pf;
    	int accessed;
    } *pt = NULL; 
    
    
    typedef struct node pt_type;
    
    
    
    
    int pm_size;
    int page_size;
    int replace;
    int num_rows;
    int last_row = -1;
    void buildtable()
    {
    	int i;
    
    
    	printf("Total page frame area size(words):    ");
    		  scanf("%d",&pm_size);
    		  printf("%d\n", pm_size );
    		  printf("Page size(words/page):   ");
    			scanf("%d", &page_size);
    	        printf("%d\n", page_size);
    			printf("Replacement policy(0=LRU, 1=FIF0):   ");
    		    scanf("%d", &replace);
    	        printf("%d\n", replace);
    		
    			num_rows = pm_size / page_size;
    			
    			
    			pt = (pt_type *)(malloc(num_rows * sizeof(pt_type)));
    
    
    			for (i = 0; i < num_rows; i++)
    			{
    				pt[i].vp = -1;
    				pt[i].accessed = -1;
    			}
        return;
    }
    void mapping()
    {	 
    	int vm_addr;
    	int i;
    	int j;
    	int temp_pf;
    	int k;
    	int vp;
    	int offset;
    	int pm_addr;
    	printf("Virtual address:  ");
    	scanf("%d", &vm_addr);
    	printf("%d\n", vm_addr);
    
    
    
    
    	vp = vm_addr / page_size;
    	offset = vm_addr % page_size;
       
    	while ((i < num_rows) && (pt[i].vp != -1) && (pt[i].vp != vp))
    	{
    		pt[i].accessed++;
    		i++;
    	}
    	if (i == num_rows)
    	{
    		temp_pf = pt[0].pf;
    		for (j = 0; j <= num_rows - 2; j++)
    		{
    			pt[j] = pt[j + 1];
    		}
    		pt[num_rows - 1].vp = vp;
    		pt[num_rows - 1].pf = temp_pf;
    		pt[num_rows - 1].accessed = 0;
    		printf("Page fault!\n");
    	}
    	else if(pt[i].vp == -1)
    	{
    		pt[i].vp = vp;
    		pt[i].pf = i;
    		pt[i].accessed = 0;
    		last_row++;
    		printf("Page fault!\n");
    	}
    	else if (pt[i].vp == vp)
    	{
    		if (replace == 0)
    		{
    			temp_pf = pt[i].pf;
    				for (k = i; k <= last_row - 1; k++)
    				{
    					pt[k] = pt[k + 1];
    					pt[k].accessed++;
    				}
    			    pt[last_row].vp = vp ;
    				pt[last_row].pf = temp_pf ;
    				pt[last_row].accessed = 0;
    				
    		}
    		pm_addr = temp_pf * page_size + offset;
    		printf("Virtual adrress matches physical address");
    	}
        
    
    
    
    
    }
    
    
    void printmap()
    {
    	int i;
    	for (i = 0; i <= last_row; i++)
    	{
    
    
    		printf("VP|\n");
    		printf("PF|\n");
    		printf("Accessed|\n");
    	}
    
    
    
    
    }
    void delete_table()
    {
    
    
    	if (pt != NULL)
    		free(pt);
    	return;
    }
    int main()
    {
    	int selection;
    	while (selection != 4) {
    		printf("Virtual memory to Main memory mapping:\n");
    		printf("------------------------------\n");
    		printf("1) Set parameters\n");
    		printf("2) Map virtual address\n");
    		printf("3) Print page table\n");
    		printf("4) Quit\n");
    		printf("\n");
    
    
    		printf("Enter selection: ");
    		scanf("%d", &selection);
    		printf("%d\n", selection);
    
    
    		switch (selection)
    	    {
    			case 1: {
    			buildtable();
    			break;
    			}
    			case 2: {
    			mapping();
    			break;
    			}
    			case 3: {
    			printmap();
    			break; }
    
    
    			case 4:
    			{
    			delete_table();
    			break;
    			}
    		}
    	}
    	printf("Program Quit\n");
    
    
    
    
    
    
    	return 1;
    }

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    What is the purpose of the program?

    What input did you use that caused the program to crash?

    Why all the horrible global variables? You need to learn to pass variables to and from your functions as required.

  11. #11
    Registered User
    Join Date
    Feb 2020
    Posts
    8
    It’s a computer assignment I followed how he wanted it done in his video and this is exaclty how he did it.

  12. #12
    Registered User
    Join Date
    Feb 2020
    Posts
    8
    I can show you the prompt he gave us I input the numbers that he tested.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. expected ')' before '*' token error?
    By Draymire in forum C Programming
    Replies: 2
    Last Post: 11-05-2012, 11:28 PM
  2. Error expected '=', ',' ,'asm', 'or' before token '{'
    By tmac619619 in forum C Programming
    Replies: 2
    Last Post: 10-13-2012, 02:33 PM
  3. error: expected ‘)’ before ‘*’ token
    By evb@studioe.com in forum C Programming
    Replies: 4
    Last Post: 01-10-2012, 02:13 PM
  4. Error: expected identifier or ‘(’ before ‘{’ token
    By jpcanaverde in forum C Programming
    Replies: 66
    Last Post: 06-08-2010, 12:53 PM
  5. error: expected ‘;’ before ‘:’ token
    By kris.c in forum C Programming
    Replies: 5
    Last Post: 02-10-2008, 10:26 PM

Tags for this Thread