Some Exercises about Pointer (C Programming)

来源:互联网 发布:如何提高淘宝产品权重 编辑:程序博客网 时间:2024/06/02 00:53

1.  

#include<stdio.h>int main(){  int i = 3;  char c = 4;  int *p = &c;  printf("*p is %d\n",*p);  return 0;}

Output: *p is 4.


2.

#include<stdio.h>int main(){  int a[4] = {1,2,3,4};  int *p = a;  int i;  printf("Use pointer p to travse all the elements of array:\n");  for (i=0; i<4; i++)   {     printf("*p is %d\n",*p);    p++;    }  return 0;}

Output:  *p is 1

               *p is 2

               *p is 3

               *p is 4


3.

void change(int *b){  b[0]=10;}#include<stdio.h>int main(){  int a[4]={1,2,3,4};  printf("Before change, a[] is %d %d %d %d\n",a[0],a[1],a[2],a[3]);  change(a);  printf("After change, a[] is %d %d %d %d\n",a[0],a[1],a[2],a[3]);  return 0;}

Output: Before change, a[ ] is 1 2 3 4

              After change, a[ ] is 10 2 3 4


4.

#include<stdio.h>int main(){  char *s = "Be Always Confident!";  int l = strlen(s); // Note: Here it is strlen(s) rather than strlen(*s)  printf("The length of *s is %d\n",l);  return 0;}

Output: The length of *s  20




0 0
原创粉丝点击