Thread: Still Practicing, Too Much To Learn

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    76

    Still Practicing, Too Much To Learn

    Did a tutorial on arrays and pointers today and wrote this little program.

    #
    Code:
    include <stdio.h> 
    
    int main()
    {
    	int position;
    	int length;
    	char arr [5];	
    	char *cptr;
    	cptr = arr;
    	printf ("Please enter a character array: ");
    	scanf ("%s", &arr);
    	length = strlen(arr);
    		while (length > 5)
    		{
    			printf ("Too big, please try again: ");
    			scanf ("%s", &arr);
    			length = strlen(arr);
    		}
    	printf ("Which letter would you like to point to? ");
    	scanf ("%d", &position);
    	position = position - 1;
    	printf("%c\n", arr[position]);
    	return 0;
    }
    It sort of works! If I enter a string of 5 characters I get the correct answer but get a run time error as well stating the stack around arr is corrupt. If i enter a string larger than 5 characters i go into the loop as expected but if I then enter a string of less than 5 characters, again i get the right answer and the same run time erros. Any suggestions?

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    I hope you know that you can only enter 5 - 1 (for the terminating NUL character) = 4 characters into your keyboard without giving your program a reason to fail.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    76
    Maybe I'm miss interpreting you but i don't beleive that is the case. I am testing the length of the string, I've run the program with letters and it works, just bombs out at the end for the message I posted above.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So that means it doesn't work, then, doesn't it.

    You have allocated room for four characters in your array. scanf has no idea how much you have room for, and it will cheerfully overwrite whatever extra pieces are needed, no matter whose memory it is (hope you didn't need that file you were working on in Photoshop).*

    *There are actually some protections, since most OS makers assume other programmers are incompetent (for just this very reason). So you're probably just trashing the extra memory that was put there so that it can detect that you've gone over the limit.
    Last edited by tabstop; 05-21-2010 at 01:20 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    76
    Is there a way to specify how much room scanf can use?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    %4s would mean "read up to 4 characters". If you want to know how scanf works, you should type "man scanf" at your terminal or at google (once you stop playing pacman).

    On the other hand, the "f" in scanf stands for "formatted", and unless you are only planning to use this code on redirected files as input, you shouldn't use scanf for monkey-at-the-keyboard input. Look at fgets.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    76
    Quote Originally Posted by tabstop View Post
    %4s would mean "read up to 4 characters". If you want to know how scanf works, you should type "man scanf" at your terminal or at google (once you stop playing pacman).

    On the other hand, the "f" in scanf stands for "formatted", and unless you are only planning to use this code on redirected files as input, you shouldn't use scanf for monkey-at-the-keyboard input. Look at fgets.
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quickly learn C# for a C++ expert
    By BigDaddyDrew in forum C# Programming
    Replies: 9
    Last Post: 06-06-2004, 04:38 PM
  2. The best place to learn
    By CougarElite in forum C Programming
    Replies: 15
    Last Post: 04-25-2004, 04:07 PM
  3. Novice trying to learn C++
    By dead in forum C++ Programming
    Replies: 10
    Last Post: 12-01-2003, 09:25 PM
  4. Witch to learn first?
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 06-17-2002, 12:06 AM
  5. Learn Win32 API or C++Builder?
    By Flucas in forum Windows Programming
    Replies: 1
    Last Post: 10-18-2001, 01:49 AM