简体   繁体   中英

Program in C which prints characters of string using pointers

hey I am a beginner in C and I am trying to understand pointers so i created two loops

In the first loop I am print every character of my string

In the second loop I try to copy the content from *w to *s

After that i move the pointers

but when I ran the code the terminal and it shows only the number 2

What is going wrong??

My terminal shows: https://imgur.com/a/ZcXQHLT

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    char sent1[]="my dog barks";
    
    char sent2[]="I love my children";
    int i=0;
    char *s;
    char *w;
    
    s=sent1;
    w=sent2;
    
for(s=sent1;*s;s++)
{
    printf("\n the words are %c" ,*s); 
    
    }   
    
    printf("\n");
    

while (*w) {
    *s = *w;
    s++;
    w++;
    printf("\n the words are %c" ,*s);
}
*s = *w;













    
    return 0;
}

Firstly, the array sent1 has only elements to store the default value "my dog barks" . Writing (or reading) beyond the last element is prohibited and doing so will invoke undefined behavior . You should specify the number of elements explicitly to allocate enough elements.

Secondly, you are printing the value of *s after doing increment s++; . After the increment, s is pointing at a new place where the data from w is not written yet. Printing should come before the increment.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    char sent1[64]="my dog barks"; /* allocate enough elements */
    
    char sent2[]="I love my children";
    int i=0;
    char *s;
    char *w;
    
    s=sent1;
    w=sent2;
    
    for(s=sent1;*s;s++)
    {
        printf("\n the words are %c" ,*s);
    
    }   
    
    printf("\n");
    

    while (*w) {
        *s = *w;
        printf("\n the words are %c" ,*s); /* print data before incrementing */
        s++;
        w++;
    }
    *s = *w;

    return 0;
}

For starters these headers

#include<stdlib.h>
#include<string.h>

are redundant in your program because neither declaration from the headers is used in your program.

Now let's consider the first for loop.

Before the loop you assigned the pointer s with the address of the first character of the array sent1

s=sent1;

Then in the for loop you reassigned the pointer s with the same value

for(s=sent1;*s;s++)

So the previous assignment was redundant.

Try use variables in the minimal scope where they are used.

Instead of this code snippet

char *s;
char *w;

s=sent1;
w=sent2;

for(s=sent1;*s;s++)
{
    printf("\n the words are %c" ,*s); 
}

You could write

printf("\n the words are ";
for( const char *s = sent1; *s; s++ )
{
    printf( "%c", *s ); 
}

After your first for loop

for(s=sent1;*s;s++)
{
    printf("\n the words are %c" ,*s); 

}  

the pointer s points tp the zero terminating character. So using the pointer in the next while loop

while (*w) {
    *s = *w;
    s++;
    w++;
    printf("\n the words are %c" ,*s);
}

results in undefined behavior. You need at least reset the pointer s to the address of the first character of the array sent1

s = sent1;
while (*w) {
    *s = *w;
    s++;
    w++;
    printf("\n the words are %c" ,*s);
}

Also before the call of printf

printf("\n the words are %c" ,*s);

you already incremented the pointer s

s++;

So the output of the first character will be skipped.

Moreover the size of the array sent1 is less than the size of the array sent2 . Thus copying the string stored in the array sent2 in the array sent1 again results in undefined behavior.

If to follow the description of what you are trying to do

In the first loop I am print every character of my string

In the second loop I try to copy the content from *w to *s

then the program can look the following way.

#include <stdio.h>

int main(void) 
{
    char sent1[] = "I love my children";
    char sent2[ sizeof( sent1 ) ] = "my dog barks";
    
    printf( "\n the words are " );
    
    for ( const char *s = sent2; *s; ++s )
    {
        putchar( *s );
    }
    
    putchar( '\n' );
    
    const char *w = sent1;
    char *s = sent2;
    
    do
    {
        *s++ = *w;
    } while ( *w++ );
    

    printf( "\n the words are " );
    
    for ( const char *s = sent2; *s; ++s )
    {
        putchar( *s );
    }
    
    putchar( '\n' );
    
    return 0;
}

The program output is

 the words are my dog barks

 the words are I love my children

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM