c语言字符串初值问题!
生活随笔
收集整理的這篇文章主要介紹了
c语言字符串初值问题!
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?read_line錯在哪里了?為何不能讀入字符串?
#include <stdio.h> #define LEN 100 void read_line(char *p,int n); void print_(char *,int n); int main() {char arr[LEN];read_line(arr,LEN);print_(arr,LEN);int i = 0;for(i = 0;i != LEN; ++i)printf("%c",arr[i]);return 0; } void read_line(char *p,int n) { char ch; unsigned cnt = 0; scanf("%c",&ch); while(ch != '\n'){if(cnt < n){*p ++ = ch;++ cnt;scanf("%c",&ch);}} }void print_(char *p,int n) {printf("%s",p); } ~ ~ ~ ~ubuntu gcc編譯器下,改正方法是給 arr數組一個初值,char arr[LEN] = {'\0'};
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的c语言字符串初值问题!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 问题:python3关于json文件多余
- 下一篇: c++ primer 5th 习题11.