1

I wrote a code for scanning a string from user using pointers and storing it in another string array and printing that string array. The output is coming quite strange. The first three characters are printed but next characters are coming as random garbage values. Please tell me the error in the code. Following is the code:

#include<stdio.h>
int main(void)
{
    char str1[8];
    char *p1=str1;
    char str2[8];
    char *p2=str2;
    printf("Enter a string\n");
    while(*p1)
        scanf("%c",p1++);
    *p1='\0';
    p1=&str1[0];
    while(*p1)
        *p2++=*p1++;
    *p2='\0';
    printf("The copied string is :");
    p2=&str2[0];
    while(*p2)
        printf("%c",*p2++);
}
8
  • what's your input? BTW the reason might be due to the missing terminating null character Commented Jan 8, 2017 at 18:01
  • while(*p1) but the variable to which p1 points, was not initialised before you test it. Commented Jan 8, 2017 at 18:02
  • I initialized the *p1 pointer to the base element of str1[8] Commented Jan 8, 2017 at 18:03
  • My input was : chayan. Output was : cha#$. The last two values were garbage as you can see Commented Jan 8, 2017 at 18:04
  • 1
    Yes p1 is initiliased, but what it points to is not. The values in str1[] are indeterminate. Moreover, you do not finalise the input with the '\0' terminator that the following loop is looking for. Commented Jan 8, 2017 at 18:05

3 Answers 3

1

You are not placing the terminating null character('\0') at the end of your strings str1[] and str2[]. And you are trying to dereference and check the value which is not initialized in your first while loop condition: while(*p1)

printf("Enter a string\n");

do{
 scanf("%c",p1++);
}while(*(p1 - 1) != '\n'); //try the do while loop

*(p1 - 1) = '\0';          //placing terminating null character

p1 = &str1[0];
while(*p1){
    *p2++ = *p1++;
}
*p2 = '\0';                 //placing terminating null character

here's the demo code: https://ideone.com/dF2QsJ

Why have you checked the condition for the new line in the do while condition? And why p1-1?

This is because you end the input by entering a '\n' which gets stored at p1 and then p1 moves to p1 + 1 at the end of each iteration. So, I check whether a '\n' is present at p1 - 1.

Sign up to request clarification or add additional context in comments.

Comments

0

okay, Why arent you using %s and get the input directly. you can get the entire string rather than looping over each character.

1 Comment

That's because I'm practicing pointers and wanted to do the simple array and strings problems using pointers.
0

This loop

while(*p1)
    scanf("%c",p1++);

checks the contents of str1 (pointed at by p1) before ever storing anything there. That uninitialized memory might contain anything, so this loop might never execute (if the first char happens to be NUL), or might run off the end of the array (corrupting memory).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.