使用指针打印字符串字符的C程序

使用指针打印字符串字符的C程序,c,loops,for-loop,pointers,c-strings,C,Loops,For Loop,Pointers,C Strings,嘿,我是C语言的初学者,我试图理解指针,所以我创建了两个循环 在第一个循环中,我打印字符串的每个字符 在第二个循环中,我尝试将内容从*w复制到*s 然后我移动指针 但是当我运行代码时,终端只显示数字2 出什么事了 我的终端显示: #包括 #包括 #包括 int main() { char sent1[]=“我的狗吠叫”; char sent2[]=“我爱我的孩子”; int i=0; char*s; char*w; s=1; w=2; 对于(s=sent1;*s;s++) { printf(“\

嘿,我是C语言的初学者,我试图理解指针,所以我创建了两个循环

在第一个循环中,我打印字符串的每个字符

在第二个循环中,我尝试将内容从*w复制到*s

然后我移动指针

但是当我运行代码时,终端只显示数字2

出什么事了

我的终端显示:

#包括
#包括
#包括
int main()
{
char sent1[]=“我的狗吠叫”;
char sent2[]=“我爱我的孩子”;
int i=0;
char*s;
char*w;
s=1;
w=2;
对于(s=sent1;*s;s++)
{
printf(“\n单词为%c”,*s);
}   
printf(“\n”);
而(*w){
*s=*w;
s++;
w++;
printf(“\n单词为%c”,*s);
}
*s=*w;
返回0;
}

首先,数组
sent1
只有元素存储默认值
“我的狗吠叫”
。禁止在最后一个元素之外写入(或读取),这样做将调用未定义的行为。您应该明确指定元素的数量以分配足够的元素

其次,在执行增量
s++之后,您正在打印
*s
的值。增量之后,
s
指向一个新的位置,
w
中的数据尚未写入。打印应先于增量

#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;
}
#包括
#包括
#包括
int main()
{
char sent1[64]=“我的狗吠叫”/*分配足够的元素*/
char sent2[]=“我爱我的孩子”;
int i=0;
char*s;
char*w;
s=1;
w=2;
对于(s=sent1;*s;s++)
{
printf(“\n单词为%c”,*s);
}   
printf(“\n”);
而(*w){
*s=*w;
printf(“\n单词为%c”,*s);/*递增前打印数据*/
s++;
w++;
}
*s=*w;
返回0;
}

对于初学者,这些标题

#include<stdlib.h>
#include<string.h>
然后在for循环中,使用相同的值重新分配指针
s

for(s=sent1;*s;s++)
所以之前的任务是多余的

尝试在使用变量的最小范围内使用变量

而不是这个代码片段

char *s;
char *w;

s=sent1;
w=sent2;

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

printf("\n the words are ";
for( const char *s = sent1; *s; s++ )
{
    printf( "%c", *s ); 
}
在你的第一个for循环之后

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

}  
指针
s
指向零终止字符。所以在下一个while循环中使用指针

while (*w) {
    *s = *w;
    s++;
    w++;
    printf("\n the words are %c" ,*s);
}
导致未定义的行为。您至少需要将指针
s
重置为数组第一个字符的地址
sent1

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

printf("\n the words are %c" ,*s);
您已经增加了指针
s

s++;
因此,将跳过第一个字符的输出

此外,数组
sent1
的大小小于数组
sent2
的大小。因此,在数组
sent1
中复制存储在数组
sent2
中的字符串会再次导致未定义的行为

如果你想按照你想做的事情的描述去做

在第一个循环中,我打印字符串的每个字符

在第二个循环中,我尝试将内容从*w复制到*s

然后程序可以按照下面的方式运行

#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;
}

通过将较长的字符串复制到较短的字符串来打破数组边界,并且由于第一个循环,
s
不再指向任何有效的位置。
#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 words are my dog barks

 the words are I love my children