Thread: Another question about recursion

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    5

    Question Another question about recursion



    I just did a recursive function and again it seems to suffer the 'Instruction at ".." referenced memory at "ox..." Memory cannot be written.

    Where did I go wrong? Anyway, this is the source code:
    /*recursive function PrintChar:
    string A
    int B
    How it's suppose to go:
    print out the first B characters in string A by recursively traversing the string
    A is unchanged when function exits*/

    #include<stdio.h>
    #include<string.h>

    void PrintChar(char *A, int B);

    void main()
    {
    char *str1;
    int B=0;

    printf("\nKey in string to process: ");
    scanf("%s",str1);

    PrintChar(str1,B);

    }

    void PrintChar(char *A, int B)
    {
    char *ptr;
    int limit;

    limit=strlen(A)-1;

    if(B>limit)
    printf("\nThe end");
    else
    {
    ptr=A+B;
    printf("%s",ptr);
    PrintChar(A,B+1);
    }

    }

    My thanks to any kind person out there.

    Jacquiline

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    I just did a recursive function and again it seems to suffer the 'Instruction at ".." referenced memory at "ox..." Memory cannot be written.
    The problem isn't in your recursive function, it's in your call to scanf. Remember that a local pointer starts out with a junk memory address, if you try to write to that address then you'll probably get a run-time error. There're three things you want to do when you use scanf: 1. Make sure the memory can be written to. 2. Make sure you don't overwrite your buffers. 3. Check the return value of scanf to make sure everything is kosher. The following code does all of this, if scanf doesn't return success then it doesn't call the function
    Code:
    int main(void)
    {
        char str1[1001];
        int B=0;
    
        printf("Key in string to process: ");
        /*
         * 1. str1 is an array, you own the memory
         * 2. The field width protects buffer overwrite
         * 3. A successful return is 1, we check for it.
         */
        if (scanf("%1000s",str1) == 1)
            PrintChar(str1,B);
    
        return 0;
    }
    And lastly, unless you're on a freestanding implementation the main function must return an int. If you don't know whether your on a freestanding implementation, you aren't.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    5

    Reply

    Thanks for your answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. iteration and recursion question
    By Stonehambey in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2008, 06:16 PM
  2. simple recursion question
    By salvadoravi in forum C Programming
    Replies: 4
    Last Post: 12-30-2007, 07:53 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. Recursion vs multiple functions
    By PJYelton in forum C++ Programming
    Replies: 4
    Last Post: 12-29-2002, 08:52 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM