Thread: Error

  1. #1
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96

    Error

    Ok I was going along in my book as usual. I was at the 2nd to last page of the Structure and Union chapter when I wrote this code from the example in the book:

    All quoted text is copyright Computer Step 2004 and is taken from the book C Programming in Easy Steps by Mike McGrath
    Code:
    #include <stdio.h>
    
    typedef struct
    {
    	union
    	{
    		int num;
    		char letter;
    	};
    	char *name;
    } info;
    
    int main()
    {
    
    	/* create a struct of the info type */
    
    	info stored;
    	printf("Please enter your first name: ");
    	gets( stored.name );
    	printf("Enter a number to convert to hex? [Y or N]: ");
    	scanf("%c", &stored.letter);
    	if( stored.letter == 'y' || stored.letter == 'Y')
    	{
    		printf("OK, enter the number to be converted: ");
    		scanf("%d", &stored.num );
    		printf("Thanks %s, ", stored.name );
    		printf("%d in hex is 0x%X\n", stored.num,stored.num);
    	}
    	return 0;
    }
    There are no compiler errors or errors in the actual code that I typed out. However, when I run this program it asks for my name, I type in a name. Then a Windows box pops up saying that the program has caused in error in MSVCRT.DLL and wiill now close. On the box there was a debug option that opened up Visual C++ and I debugged it and it said it was having access violation problems writing to the address (I am thinking whatever address
    Code:
    stored.name
    is being stored in). I've never encountered this sort of error before as a programmer, and was just wondering how to get around it. ANd yes, I did try rebooting my computer like it said in the error box. Thanks for the help.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The problem is that you have allocated no memory to store the name.

    Also see the FAQ of why gets() is bad.

    To fix you can:
    1) declare it as an array
    2) use malloc to allocate some memory

  3. #3
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Quote Originally Posted by Thantos
    The problem is that you have allocated no memory to store the name.

    Also see the FAQ of why gets() is bad.

    To fix you can:
    1) declare it as an array
    2) use malloc to allocate some memory
    How am I supposed to "allocate memory"? Also, where is this faq? Is it in the C forum or in one of the FAQs in the FAQ forum because I just searched for gets() in the FAQ forum and found nothing. Thanks.

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    also, typedef is a preprocessor and require a #
    e.g.
    #typedef . . .
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    typedef is NOT a preproccessor command

    faq: http://faq.cprogramming.com

    To allocate memory you can do something such as:
    char arr[15];

    or you can do
    char *arr;
    arr = malloc(15);
    and then later
    free(arr);

  6. #6
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Quote Originally Posted by Thantos
    typedef is NOT a preproccessor command

    faq: http://faq.cprogramming.com

    To allocate memory you can do something such as:
    char arr[15];

    or you can do
    char *arr;
    arr = malloc(15);
    and then later
    free(arr);
    Ok thanks a lot. That totally cleared things up. I also checked my book in the TOC and it has a section about allocating memory. Tomorrow I will be reading the last 2 chapters of my book so I'll read more about that. Anyway, thanks a bunch.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM