Thread: Help with error beginner

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

    Angry Help with error beginner

    I am trying to write a simple program that takes in a string, reverses the letters in it and displays the result. I have not got it finished but am stuck as I keep getting this error:

    Application Error
    The instruction at "0x004010a7" referenced memory at "0x00000065". The memory could not be "read".

    this is the code that I have is not much but it is simple and I cannot see the problem. This error only occurs when either of the printf statments are included

    [code]
    #include <stdio.h>

    char *p;
    int main(void)


    {
    char string;
    printf("Type in a string: ");
    string = getchar();
    p=string;
    printf("%s\n", string);
    printf("%c\n", *p);
    }
    [code]

    I am lost can you help please.

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    I see you made some improvements...
    Take a look again at Hammers comment in your previous thread. Don't use global vars unless you have to.
    You are using int main, so return something at the end of your main function.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    12

    still have error

    ok have pur in a return now and removed all the global variables, I have looked over the printf statment and gone through it with my book and am still none the wiser as to where the error is coming from.

    [code]
    #include <stdio.h>


    int main(void)


    {
    char *p;
    char string;
    printf("Type in a string: ");
    string = getchar();
    p=string;
    printf("%s\n", string);
    printf("%c\n", *p);
    return 0;

    }
    [code]

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    { 
    char *p; 
    char string; 
    printf("Type in a string: "); 
    string = getchar(); 
    p=string; 
    printf("%s\n", string); 
    printf("%c\n", *p); 
    return 0; 
    }
    How many threads do you want? Keep to one please. Also, the last code tag needs a forward slash to make it work (see my signature!)

    Now to store a string you need an array of chars. Calling a variable string, does not make it one. "char" is a single byte, so this:
    >char string;
    is giving you variable called string, which is one byte long. Therefore is it not a string.

    You need something like this:
    >char string[100];
    which will give you 99 bytes to store text, and 1 byte for the null terminator that ends the array, and makes it usable as a string.

    At your level, I wouldn't bother using pointers (the *p in your code). Just get to grips with basic the concept of arrays first.

    Here's one version (there are many ways) :
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char	string[100];
    
    	printf("Type in a string: ");
    
    	if ((fgets(string, 100, stdin)) != NULL)
    	{
    		printf("%s\n", string);
    	}
    
    	return 0;
    }
    Compare this, and research your books. Post again if you have any more questions.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Remember that the fgets function also puts the newline character in your buffer.
    Here's a link to the fgets manual page

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Piece of cake:
    Code:
    static void printRev ( void )
    {
      int ch;
      if ( ( ch = getchar() ) != '\n' )
        printRev();
      putchar ( ch );
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed