C语言指针笔试题_第1页
C语言指针笔试题_第2页
C语言指针笔试题_第3页
C语言指针笔试题_第4页
C语言指针笔试题_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C语言指针笔试题1.使用指针实现swap函数

题目要求:编写一个swap函数,交换两个整数的值,使用指针实现。

代码示例:

```c

#include<stdio.h>

voidswap(int*a,int*b)

{

inttemp=*a;

*a=*b;

*b=temp;

}

intmain()

{

inta=5,b=10;

printf("Beforeswap:a=%d,b=%d\n",a,b);

swap(&a,&b);

printf("Afterswap:a=%d,b=%d\n",a,b);

return0;

}

```

2.使用指针实现strlen函数

题目要求:编写一个自定义的strlen函数,计算传入字符串的长度,使用指针实现。

代码示例:

```c

#include<stdio.h>

intmyStrlen(char*str)

{

intlen=0;

while(*str!='\0')

{

len++;

str++;

}

returnlen;

}

intmain()

{

charstr[]="Hello,world!";

intlength=myStrlen(str);

printf("Thelengthofthestringis:%d\n",length);

return0;

}

```

3.使用指针实现strcpy函数

题目要求:编写一个自定义的strcpy函数,将源字符串的值复制给目标字符串,使用指针实现。

代码示例:

```c

#include<stdio.h>

voidmyStrcpy(char*dest,char*src)

{

while(*src!='\0')

{

*dest=*src;

dest++;

src++;

}

*dest='\0';

}

intmain()

{

charsrc[]="Hello,world!";

chardest[20];

myStrcpy(dest,src);

printf("Thecopiedstringis:%s\n",dest);

return0;

}

```

4.使用指针实现strcmp函数

题目要求:编写一个自定义的strcmp函数,比较两个字符串的大小,使用指针实现。

代码示例:

```c

#include<stdio.h>

intmyStrcmp(char*s1,char*s2)

{

while(*s1==*s2)

{

if(*s1=='\0')

return0;

s1++;

s2++;

}

return(*s1-*s2);

}

intmain()

{

chars1[]="Hello";

chars2[]="Hello";

intresult=myStrcmp(s1,s2);

if(result==0)

printf("Thestringsareequal\n");

elseif(result<0)

printf("Thefirststringissmaller\n");

else

printf("Thesecondstringissmaller\n");

return0;

}

```

5.使用指针实现数组排序

题目要求:编写一个函数,对传入的整型数组进行排序,使用指针实现。

代码示例:

```c

#include<stdio.h>

voidsortArray(int*arr,intsize)

{

inti,j,temp;

for(i=0;i<size-1;i++)

{

for(j=0;j<size-i-1;j++)

{

if(*(arr+j)>*(arr+j+1))

{

temp=*(arr+j);

*(arr+j)=*(arr+j+1);

*(arr+j+1)=temp;

}

}

}

}

intmain()

{

intarr[]={5,2,9,1,6};

intsize=sizeof(arr)/sizeof(arr[0]);

printf("Beforesorting:");

for(inti=0;i<size;i++)

{

printf("%d",arr[i]);

}

sortArray(arr,size);

printf("\nAftersorting:");

for(inti=0;i<size;i++)

{

printf("%d",arr[i]);

}

return0;

}

```

以上是一些使用指针实现的C语言笔试题,希望对你有帮助。6.使用指针实现动态内存分配

题目要求:编写一个函数,使用指针实现动态内存分配,为整型数组分配内存,并初始化数组元素。

代码示例:

```c

#include<stdio.h>

#include<stdlib.h>

int*allocateArray(intsize)

{

int*arr=(int*)malloc(size*sizeof(int));

if(arr==NULL)

{

printf("Memoryallocationfailed\n");

exit(1);

}

for(inti=0;i<size;i++)

{

arr[i]=i+1;

}

returnarr;

}

intmain()

{

intsize;

printf("Enterthesizeofthearray:");

scanf("%d",&size);

int*arr=allocateArray(size);

printf("Thearrayelementsare:");

for(inti=0;i<size;i++)

{

printf("%d",arr[i]);

}

free(arr);

return0;

}

```

7.使用指针实现链表的创建和遍历

题目要求:编写一个函数,使用指针实现链表的创建和遍历,创建一个包含五个节点的链表,并输出链表的值。

代码示例:

```c

#include<stdio.h>

#include<stdlib.h>

typedefstructNode

{

intdata;

structNode*next;

}Node;

voidcreateLinkedList(Node**head)

{

*head=NULL;

for(inti=5;i>0;i--)

{

Node*newNode=(Node*)malloc(sizeof(Node));

newNode->data=i;

newNode->next=*head;

*head=newNode;

}

}

voidtraverseLinkedList(Node*head)

{

Node*current=head;

printf("Linkedlistelements:");

while(current!=NULL)

{

printf("%d",current->data);

current=current->next;

}

}

intmain()

{

Node*head;

createLinkedList(&head);

traverseLinkedList(head);

return0;

}

```

8.使用指针实现二维数组的动态内存分配

题目要求:编写一个函数,使用指针实现二维数组的动态内存分配,并初始化数组元素。

代码示例:

```c

#include<stdio.h>

#include<stdlib.h>

int**allocateMatrix(introws,intcols)

{

int**arr=(int**)malloc(rows*sizeof(int*));

if(arr==NULL)

{

printf("Memoryallocationfailed\n");

exit(1);

}

for(inti=0;i<rows;i++)

{

arr[i]=(int*)malloc(cols*sizeof(int));

if(arr[i]==NULL)

{

printf("Memoryallocationfailed\n");

exit(1);

}

for(intj=0;j<cols;j++)

{

arr[i][j]=i+j;

}

}

returnarr;

}

intmain()

{

introws,cols;

printf("Enterthenumberofrows:");

scanf("%d",&rows);

printf("Enterthenumberofcolumns:");

scanf("%d",&cols);

int**matrix=allocateMatrix(rows,cols);

printf("Thematrixelementsare:\n");

for(inti=0;i<rows;i++)

{

for(intj=0;j<cols;j++)

{

printf("%d",matrix[i][j]);

}

printf("\n");

}

for(inti=0;i<rows;i++)

{

free(matrix[i]);

}

free(matrix);

return0;

}

```

9.使用指针实现结构体的创建和访问

题目要求:编写一个函数,使用指针实现结构体的创建和访问,创建一个包含姓名和年龄的结构体,并输出结构体的值。

代码示例:

```c

#include<stdio.h>

typedefstructPerson

{

charname[20];

intage;

}Person;

voidcreatePerson(Person*p)

{

printf("Entername:");

scanf("%s",p->name);

printf("Enterage:");

scanf("%d",&(p->age));

}

voidprintPerson(Person*p)

{

printf("Name:%s\n",p->name);

printf("Age:%d\n",p->age);

}

intmain()

{

Personp;

createPerson(&p);

printPerson(&p);

return0;

}

```

10.使用指针实现文件的读取和写入

题目要求:编写一个函数,使用指针实现文件的读取和写入,从一个文件中读取内容,并将内容写入到另一个文件中。

代码示例:

```c

#include<stdio.h>

voidcopyFile(char*src,char*dest

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论