C语言指针造成的严重后果举例有哪些

C语言指针造成的严重后果举例有哪些,第1张

C语言指针造成的严重后果举例有:代码无法运行,计算机崩溃无反应等。

比如,你不对指针赋初值,他就会指向一个不确定的地方,万一是关键进程,系统地址什么的,你的机器就自然会崩溃。学习 C 语言的指针既简单又有趣,但是C语言对找工作也是非常有帮助的,C语言也比JAVA实用性好。

C语言指针的特点:

通过指针,可以简化一些 C 编程任务的执行,还有一些任务,如动态内存分配,没有指针是无法执行的。所以,想要成为一名优秀的 C 程序员,学习指针是很有必要的。正如所知道的,每一个变量都有一个内存位置,每一个内存位置都定义了可使用 & 运算符访问的地址,它表示了在内存中的一个地址。

#include<stdioh>

#include<stringh>

char

fun(char

s,char

t)//函数功能是在字符串S中找字符串T

{

char

p,r,a;

a=NULL;//是一个标志,开始没查找到

//开始s=&s[0]

while(s)//从S

的第一个字符开始查找,直到遇到'\0'

{

p=s;

r=t;

while(r)//从T

的第一个字符开始查找,直到遇到'\0'

if(r==p)//如果在S字符串中找到了第一个

T字符串的T[0],

{

r++;//在比较T[1]

t[2]

p++;//同上

}

else

break;

if

(r=='\0')

a=s;//表明查找到了

s++;

}

return

a;//返回NULL没到到,否则找到了

}

main()

{

char

s[100],t[100],p;

printf("\n

please

enter

string

S:");

scanf("%s",&s);

printf("\n

please

enter

string

T:");

scanf("%s",&t);

p=fun(s,t);

if(p)//真,为找到了

printf("\n

the

result

is

:%s\n",p);

else

printf("\n

not

found!\n");

}

//DEV C 

#include <stdioh>

#include <stdlibh>

#include <timeh>

#include <stringh>

#define N 20

typedef struct

{

char name[10];

int age;

int sex;

int grade;

int nclass;

int room;

}INFO;

int main()

{

char name1[20]={"张","王","李","赵","钱","孙","蒋","秦","任","刘","周","郑","吴","朱","许","何","吕","谢","方","叶"};

char name2[20]={"子","欣","晨","紫","诗","梦","嘉","一","思","静","可","佳","心","梓","俊","明","浩","博","文","天"};

char name3[20]={"轩","勇","涛","军","浩","豪","宇","俊","鸣","熙","文","国","佑","泽","然","杰","远","平","秋","飞"};

srand((unsigned)time(0));

INFO s;

s=(INFO)malloc(sizeof(INFO)N);

int n,i;

for(i=0;i<N;i++) //随机初始N个同学姓名年龄,性别,班级等数据

{

n=rand()%20;

strcpy(s[i]name,name1[n]);

n=rand()%20;

strcat(s[i]name,name2[n]);

n=rand()%20;

strcat(s[i]name,name3[n]);

//以上为随机生成姓名

s[i]age=rand()%3+16;

s[i]sex=rand()%2;

s[i]grade=rand()%3+1;

s[i]nclass=rand()%6+1;

s[i]room=100(rand()%5+1)+rand()%20+1;

}

for(i=0;i<N;i++)

{

printf("%s %d岁 ",s[i]name,s[i]age);

if(s[i]sex==1)

{

printf("男 ");

}

else

{

printf("女 ");

}

printf("%d年级%d班 %d室\n",s[i]grade,s[i]nclass,s[i]room);

}

char ch[50];

int find;

while(1)

{

find=0;

memset(ch,'\0',50);

printf("\n请输入要查询的姓名: ");

fflush(stdin);

scanf("%s",ch);

for(i=0;i<N;i++)

{

if(strcmp(s[i]name,ch)==0)

{

find=1;

printf("%s %d岁 ",s[i]name,s[i]age);

if(s[i]sex==1)

{

printf("男 ");

}

else

{

printf("女 ");

}

printf("%d年级%d班 %d室\n",s[i]grade,s[i]nclass,s[i]room);

break;

}

}

if(find==0)

{

printf("\n未找到!");

}

}

return 0;

}

int a[3];

int p=&a;

//(p+n)=a[n];

int a[3][4];

int p;

p=a[0];//注意只能是这种格式;此指针指向的是列地址。

//(p+11)=a[3][4];

int p[4];

p=a;//注意只能用这种格式,指向的是行地址。

//((p+i)+j)=a[i][j];

按照你的要求编写的C语言程序如下

include<stdioh>

int main()

{

 int a,b;

 int p=&a;

 a=30;

 printf("a=%d\n",p);

 p=&b;

 scanf("%d",p);

 a=p+24;

 printf("a=%d,b=%d",a,b);

 return 0;

}

运行结果

a=30

76

a=100,b=76

以上就是关于C语言指针造成的严重后果举例有哪些全部的内容,包括:C语言指针造成的严重后果举例有哪些、C语言指针程序、设计一个程序(C语言 指针)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/9831008.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存