您的位置:首页 > 新闻 > 热点要闻 > 6.15 c语言

6.15 c语言

2024/7/6 19:04:03 来源:https://blog.csdn.net/jwh199694/article/details/139698944  浏览:    关键词:6.15 c语言

数组指针

#include <stdio.h>
#include <stdlib.h>
int main()
{int a[3][2] = {{1,2},{3,4},{5,6}};int (*p)[2],i,j;p = a;for(i=0;i<3;i++){for(j=0;j<2;j++){printf("%d %d\n",p[i][j],*(*(p+i)+j));}printf("\n");}return 0;
}

10.7 字符指针和字符串

c语言通过使用字符数组来处理字符串
char类型的指针变量称为字符指针变量,字符指针变量与字符有着密切关系,他也被用来处理字符串
初始化字符指针是把内存中字符串的首地址赋予指针
当一个字符指针指向一个字符串常量时,不能修改指针指向的对象的值

#include <stdio.h>
#include <stdlib.h>
int main()
{char ch1[] = "hello";char ch2[] = "hello";char *p;p = ch1;if(isalpha(*p)){if(isupper(*p)){*p = tolower(*p);}else{*p = toupper(*p);}}printf("%s %s\n",p,ch1);p = ch2;printf("%s\n",ch2);return 0;
}

静态存储区:
1、全局变量
2、static局部变量
3、字符串常量//char *p = “welcome”;
栈区:指针变量
字符串常量不能被修改,因为存储在静态存储区//会发生段错误

#include <stdio.h>
#include <stdlib.h>
int main()
{char a[] = "hello";char *p = "world";strcpy(p,a);//段错误,字符串常量被赋值puts(a);puts(p);return 0;
}
#include <stdio.h>
#include <stdlib.h>//实现字符串连接功能
int main()
{char a[100] = "hello world";char *p = "welcome";int i = 0;char *q;q = p;while(*(a+i) != '\0'){i++;}while(*p != '\0'){*(a+i) = *p;p++;i++;}*(a+i) = *p;p = q;puts(a);puts(p);return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com