Domanda

I need to answer this question testing this:

Exercise 19-16: Fix the code in example so that the sample variable's value is saved before the while loop runs and is then restored afterward. Add a puts(sample) statement to the code after the while loop is done executing, to prove that the variable's original address is restored.

**Example:**
     #include <stdio.h>

    int main()
    {
        char *sample = "From whence cometh my help?\n";

        while(putchar(*sample++))
            ;
        return(0);
    }

I want to check if my answer is correct, may be you can give an explanation, because I have no clear understanding about pointers and variables. And this is my answer:

     #include <stdio.h>

        int main()
        {
            char *sample = "From whence cometh my help?\n"; //This is my string
            char StringSample[31];
            int index = 0;

            while(sample[index] != '\0') //while the char is NOT equal to empty char (the last one)
            {
                index++; //increase the index by 1
                StringSample[index] = sample[index]; //put the char "n" inside the stringSample //
                putchar(*sample++);  //write it to the screen
            }
            putchar('\n'); //A new line

            //to put the entire string on screen again
            for (index=0; index<60; index++)
            {
                putchar(StringSample[index]); 
            }
            return(0);
        }

Here is my output:

enter image description here

I don't know why the string is split it to From whence co and why the rest of the text, that as you can see, has no sense.

I'm using Xcode 5.02

È stato utile?

Soluzione

The problem is that you are trying to reference your sample variable as indexed as well as pointer . This will lead you to the wrong result.

while(sample[index] != '\0')   
{
    index++;
    StringSample[index] = sample[index];  // indexed access
    putchar(*sample++);                   // incrementing pointer sample. 
}   

You can simply achieve your goal as

#include <stdio.h>

int main()
{
    char *sample = "From whence cometh my help?\n";
    char *ptr = sample;  // Saving the value of sample

     while(putchar(*sample++))
        ;
    putchar('\n');

    sample = ptr;        // Restoring sample's value.
    puts(sample);

    return(0);
}  

Altri suggerimenti

char StringSample[31];
....
for (index=0; index<60; index++)
{
    putchar(StringSample[index]);
}

Your array has 31 cells, but you iterate far away till 60. God knows what you may access.

You made this way more complicated that it needed to be. Just save the pointer in another variable and copy it back when you're done.

 #include <stdio.h>

int main()
{
    char *sample = "From whence cometh my help?\n";
    char *temp = sample;

    while(putchar(*sample++))
        ;

    sample = temp;
    puts(sample);
    return(0);
}

Output:

From whence cometh my help?
From whence cometh my help?
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top