Access Violation under VC++ 6.0

This is a discussion on Access Violation under VC++ 6.0 within the C Programming forums, part of the General Programming Boards category; got access violation running this code, and the IDE jumps into a bunch of assembly lines Code: #include <stdio.h> #include ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    12

    Question Access Violation under VC++ 6.0

    got access violation running this code, and the IDE jumps into a bunch of assembly lines
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    	char dic[][40]  = {
    		"atlas", "a volume of maps",
    			"car", "a mobilized vehicle",
    			"telephone", "a communication device",
    			"air plane", "a flying machine",
    			"", ""     /* null terminates the string */
    	};
    	int main(void)
    	{
    	char **p;
    	char word[80], ch;
    	do
    	{
    	puts("\nEnter the word you wish to seach:");
    	scanf("%s",word);
    	p=(char**)dic;
    	do
    	{
    		if(!strcmp(*p,word)){
    			puts("\n the meaning is");
    			puts(*(p+1));
    			break;
    		}
    		if(!strcmp(*p,word)) break;
    			p=p+2;
    	} while (*p);
    	if(!*p)puts("continue? (Y/N)");
    	scanf("%c%*c",&ch);
        }while(toupper(ch)!='N');
    	return 0;
    }


    and the two bold part seem redundant to me, please some one explain . I got this code from a C/C++ reference.
    Last edited by xephyr; 07-29-2004 at 08:08 PM. Reason: (missed some Q's)

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

    Pointer to a pointer

    Wouldnt you require **p instead of *p to access the values pointed by the pointer which in turn is pointed by P?
    Last edited by vsriharsha; 07-29-2004 at 10:02 PM. Reason: needs clarity
    Help everyone you can

  3. #3
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Sorry, discard my answer....
    Missed your point completely...
    Help everyone you can

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Well the cast should have told you that something was wrong.
    Seems to me you've found one of the vast number of useless tutorials out there

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    char dic[][40] = {
        "atlas", "a volume of maps",
        "car", "a mobilized vehicle",
        "telephone", "a communication device",
        "air plane", "a flying machine",
        "", ""                      /* null terminates the string */
    };
    int main(void)
    {
        char (*p)[40];
        char word[80], ch;
        do {
            puts("\nEnter the word you wish to seach:");
            scanf("%s", word);
            p = dic;
            do {
                if (!strcmp(*p, word)) {
                    puts("\n the meaning is");
                    puts(*(p + 1));
                    break;
                }
                p++;        /* next word/meaning pair */
            } while (**p);  /* **p is first char of the meaning */
            puts("continue? (Y/N)");
            scanf("%*c%c", &ch);
        } while (toupper(ch) != 'N');
        return 0;
    }
    Pay particular attention to the way the pointer is declared.

    > if (!strcmp(*p, word))
    This is hard to read, and gives the impression strcmp() returns a true/false answer - it doesn't
    if ( strcmp(*p, word) == 0 )

    > and the two bold part seem redundant to me
    Yeah, so did I, so I removed them
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Helper
    Join Date
    Jun 2004
    Posts
    255

    Angry

    Quote Originally Posted by xephyr
    got access violation running this code, and the IDE jumps into a bunch of assembly lines
    Code:
    <...>
    	char dic[][40]  = {
    		"atlas", "a volume of maps",
    			"car", "a mobilized vehicle",
    			"telephone", "a communication device",
    			"air plane", "a flying machine",
    			"", ""     /* null terminates the string */
    	};
    <...>
    	p=(char**)dic;
    <...>
    You are going to open your textbook and write 100 times

    'typecast is evil'

    When finished, you do the same with

    'an array is not a pointer'
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access Violation using memcmp?
    By someprogr in forum C Programming
    Replies: 3
    Last Post: 12-31-2007, 12:45 AM
  2. Access violation... can't figure it out...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2007, 10:52 AM
  3. makefile exported by vc 6.0 doesn't work
    By wow in forum Windows Programming
    Replies: 7
    Last Post: 03-24-2006, 03:20 PM
  4. FtpFileFind access violation with MS VC++ 6.0
    By earth_angel in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2005, 07:02 PM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 01:46 PM

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