Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22225

I want to know why this result is output

$
0
0

source code

   /*************************************************** * Copyright: 2023, 黄子涵. * File name: huangzihan_c_program_142 * Description: 改变指针的值 * Author: 黄子涵 * Version: V1.0.0 * Date: 2023-01-08 * ****************************************************/#include<stdio.h>char huangzihan[10];char huangchunqin[12];char chenlanying[12];char shejiazi[8];void name_input();void name_output();void name_output_n();void name_input(){   int i;   printf("*********************************\n");   printf("     给字符数组输入对应的名字    \n");   printf("*********************************\n");   printf("请输入黄子涵的小写拼音:");   for(i=0;i<10;i++)   {     scanf("%c",&huangzihan[i]);     if(huangzihan[i]==10)     {       huangzihan[i]='\0';       break;     }   }   getchar();   printf("请输入黄春钦的小写拼音:");   for(i=0;i<12;i++)   {     scanf("%c",&huangchunqin[i]);     if(huangchunqin[i]==10)     {       huangchunqin[i]='\0';       break;     }   }   getchar();   printf("请输入陈兰英的小写拼音:");   for(i=0;i<11;i++)   {     scanf("%c",&chenlanying[i]);     if(chenlanying[i]==10)     {       chenlanying[i]='\0';       break;     }   }   getchar();   printf("请输入佘佳梓的小写拼音:");   for(i=0;i<8;i++)   {     scanf("%c",&shejiazi[i]);     if(shejiazi[i]==10)     {       shejiazi[i]='\0';       break;     }   }   printf("\n");}void name_output(){   char *p1,*p2,*p3,*p4;    printf("*********************************\n");   printf("     将字符数组对应的名字输出    \n");   printf("*********************************\n");   printf("输出huangzihan:");   p1=huangzihan;   printf("%s",p1);   printf("\n");      printf("输出huangchunqin:");   p2=huangchunqin;   printf("%s",p2);   printf("\n");     printf("输出chenlanying:");   p3=chenlanying;   printf("%s",p3);     printf("\n");   printf("输出shejiazi:");   p4=shejiazi;   printf("%s",p4);   printf("\n");   }void name_output_n(){   int i;   char *p;   printf("\n");   printf("*********************************\n");   printf("           改变指针的值          \n");   printf("*********************************\n");   printf("你要从第几个字符输出huangzihan(共10个字符)?");   scanf("%d",&i);   p=huangzihan+i-1;   printf("这是你要输出的字符串:%s",p);   printf("\n");   printf("你要从第几个字符输出huangchunqin(共12个字符)?");   scanf("%d",&i);   p=huangchunqin+i-1;   printf("这是你要输出的字符串:%s",p);   printf("\n");   printf("你要从第几个输出chenlanying(共11个字符)?");   scanf("%d",&i);   p=chenlanying+i-1;   printf("这是你要输出的字符串:%s",p);   printf("\n");   printf("你要从第几个输出shejiazi(共8个字符)?");   scanf("%d",&i);   p=shejiazi+i-1;   printf("这是你要输出的字符串:%s",p);   printf("\n");}int main(){  extern char huangzihan[10];  extern char huangchunqin[12];  extern char chenlanying[12];  extern char shejiazi[8];  name_input();  name_output();  name_output_n();  return 0;}

At first, I thought it was the problem of not adding the end tag of the string, but I added the end tag of the string and found it didn't work.Add code as follows:

if(shejiazi[i]==10)     {       shejiazi[i]='\0';       break;     }

I want to output results

输出huangzihan:huangzihan输出huangchunqin:huangchunqin输出chenlanying:chenlanying输出shejiazi:shejiazi

Actual output results

Actual output results

I want to know why this happens. If you know, please tell me. Thank you very much。


Viewing all articles
Browse latest Browse all 22225

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>