Thread: Page Fault in Visual C++

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    12

    Page Fault in Visual C++

    Good evening all. I have a problem with the code I am trying to write that separates an expression into an array. I continually get a invalid page fault in Visual C++ (6). Although I didn't try it on the UNIX server, this usually indicates a segmentation fault on UNIX.

    Would someone mind looking at the code below and see why I might be getting a segmentation fault?? T-I-A


    Code:
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    
    main()
    
    	{
    
    	char array[100], exp[100], id[10], num[10], digit[10], letter[10];
    	int i=0, j=0;
    
    	printf("Please enter an expression: ");
    	scanf("%s", exp);
    
    	printf("%s\n", exp);
    
    	if (exp[i] == '(')
    		array[i] = 20;
    		else if(exp[i] == ')')
    			array[i] = 21;
    		else if(exp[i] == '+')
    			array[i] = 16;
    		else if(exp[i] == '-')
    			array[i] = 17;
    		else if(exp[i] == '*')
    			array[i] = 18;
    		else if(exp[i] == '/')
    			array[i] = 19;
    
    		else if(isdigit(exp[i]))
    			while(isdigit(exp[i])){
    				*digit = exp[i];
    				*num = strcat(num, digit);
    				++i;}
    		else if(isalpha(exp[i]))
    			while(isalpha(exp[i])){
    				*letter = exp[i];
    				*id = strcat(id, letter);
    				++i;}
    	else
    		printf("%c is invalid\n", exp[i]);
    
    for(j=1; j<100; j++)
    	{
    	printf("%c\n",array[j]);
    }
    
    
    }

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    GOT IT!
    try this
    note: scanf you forgot the & (address of)
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int main()
    
    	{
    
    	char array[100], exp[100], id[10], num[10], digit[10], letter[10];
    	int i=0, j=0;
    
    	printf("Please enter an expression: ");
    	scanf("%s", exp);
    
    	printf("%s\n", exp);
    
    	if (exp[i] == '(')
    		array[i] = 20;
    	else if(exp[i] == ')')
    		array[i] = 21;
    	else if(exp[i] == '+')
    		array[i] = 16;
    	else if(exp[i] == '-')
    		array[i] = 17;
    	else if(exp[i] == '*')
    		array[i] = 18;
    	else if(exp[i] == '/')
    		array[i] = 19;
    
    	else if(isdigit(exp[i]))
    		while(isdigit(exp[i])){
    			*digit = exp[i];
    			*num = strcat(num, digit);
    				++i;}
    		else if(isalpha(exp[i]))
    			while(isalpha(exp[i])){
    				*letter = exp[i];
    				*id = strcat(id, letter);
    				++i;}
    	else
    		printf("%c is invalid\n", exp[i]);
    
    for(j=1; j<100; j++)
    	{
    	printf("%c\n",array[j]);
    }
    
    
    }
    note: ' ' is for character comparison
    " " is for string comparison...

    -Luke
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up

    try this
    note: scanf you forgot the & (address of)
    Well, U need not specify the & (address of) operator for arrays and pointers (because, when u specify the variable name of an array, its base address is automatically referenced.

    code:--------------------------------------------------------------------------------
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>

    int main()

    {

    char array[100], exp[100], id[10], num[10], digit[10], letter[10];
    int i=0, j=0;

    printf("Please enter an expression: ");
    scanf("%s", exp);

    printf("%s\n", exp);

    if (exp[i] == '(')
    array[i] = 20;
    else if(exp[i] == ')')
    array[i] = 21;
    else if(exp[i] == '+')
    array[i] = 16;
    else if(exp[i] == '-')
    array[i] = 17;
    else if(exp[i] == '*')
    array[i] = 18;
    else if(exp[i] == '/')
    array[i] = 19;

    else if(isdigit(exp[i]))
    while(isdigit(exp[i])){
    *digit = exp[i];
    *num = strcat(num, digit);
    ++i;}
    else if(isalpha(exp[i]))
    while(isalpha(exp[i])){
    *letter = exp[i];
    *id = strcat(id, letter);
    ++i;}
    else
    printf("%c is invalid\n", exp[i]);

    for(j=1; j<100; j++)
    {
    printf("%c\n",array[j]);
    }


    }
    *letter=exp[i] etc.... Do they really work. U declared them as arrays not pointers.

    Then...

    note: ' ' is for character comparison
    " " is for string comparison...
    I've not seen direct string comparision in C. Use strcmp instead (defined in string.h)


    -Harsha.
    Help everyone you can

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    44
    Originally posted by vsriharsha

    *letter=exp[i] etc.... Do they really work. U declared them as arrays not pointers.
    I'd expect so. Array identifies when used in an expression (except of course as the destination of an assignment) evaluate to a pointer to the first element of the array.

    Ian Woods

  5. #5
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Smile

    Yes it works....I myself said that when ever an array is referred, its base address is passed...

    Did the page fault problem come to an end now, reader?? Can
    char array[100], exp[100], id[10], num[10], digit[10], letter[10];
    int i=0, j=0;

    printf("Please enter an expression: ");
    scanf("%s", exp);
    be a problem...I mean, what if the entered expression is more than 100 chars. So, scanf can be re-written as...

    scanf("%100s",exp);

    Check it out.

    -harsha.
    Help everyone you can

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Those strcat functions leave a lot to be desired - your'e not copying chars, but the whole of the remaining string.

    The assignment
    *id = strcat(id, letter);
    is also wrong.


    If you do this often enough, you're going to fill up the buffer space pretty quick

    And how did vsriharsha manage to mangle the nicely formatted code?

  7. #7
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Cool

    originally posted by salem
    And how did vsriharsha manage to mangle the nicely formatted code?
    Its the grace of one of the best features of WINDOWS... COPY-PASTE.

    -Harsha.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. What's wrong with this code??
    By Xanth in forum C Programming
    Replies: 11
    Last Post: 12-23-2004, 02:41 PM
  4. MS Visual C++ 6 wont include vector.h in a header file
    By bardsley99 in forum C++ Programming
    Replies: 9
    Last Post: 11-06-2003, 12:05 PM
  5. odd errors from msvc std library files
    By blight2c in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2002, 12:06 AM