您好,欢迎来到华佗小知识。
搜索
您的当前位置:首页【C\C++笔记】指针输出字符串

【C\C++笔记】指针输出字符串

来源:华佗小知识

1错误代码

#include<stdio.h>

int main(){
    char a[]="hello";
    char *p=a;
    for(int i=0;i<5;i++){
        printf("%c",*p+i);
    }    
    return 0;
}

输出

hijkl
--------------------------------
Process exited after 0.3297 seconds with return value 0

---

2正确代码(其中之一)

#include<stdio.h>

int main(){
	char a[]="hello";
	char *p=a;
	for(int i=0;i<5;i++){
		printf("%c",*(p+i));
	}	
	return 0;
}

 输出

hello
--------------------------------
Process exited after 0.2415 seconds with return value 0

正确代码2

#include<stdio.h>

int main(){
	char a[]="hello";
	char *p=a;
	printf("%s",p);
	return 0;
}

 

转载于:https://www.cnblogs.com/LPworld/p/11552245.html

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务