今天继续复习指针,还是很深奥的,两点注意事项:
1. int型数据可以强制类型转化赋值给指针变量,然后对该地址赋值(用在裸机上);
2.指针数组是数组,存放的是指针,数组指针是数组的指针,存放的是行指针。
利用数组指针对输入数组进行单词数目确认的函数:
#include <stdio.h>
int wordcount(char *str){
int count;
int word = 0;
while(*str){
count = 0;
while((*str>='a' && *str<='z') || (*str>='A' && *str<='Z')){
count++;
str++;
}
if(*str == ' '){
if(count>2) word++;
str++;
continue;
}
else if(*str == '\0')
{
if(count>2) word++;
break;
}
else {
do{
str++;
}while(*str != ' ' && *str!= '\0');
}
}
return word;
}
int main(int argc, const char *argv[])
{
char str[100];
printf("please input a str:");
scanf("%[^\n]",str);
int ret = -1;
ret = wordcount(str);
printf("word in str:%d\n",ret);
return 0;
}
使用指针和二级指针对数组进行单词翻转的函数
#include <stdio.h>
#include <string.h>
void swap(char **h,char **t){
while(*t > *h){
**t ^= **h;
**h ^= **t;
**t ^= **h;
(*h)++;
(*t)--;
}
}
void strchange(char *str){
char *head;
char *tail;
head = str;
tail = str + strlen(str)-1;
swap(&head,&tail);
tail = str;
head = str;
char *temp;
while(1){
while(*tail != ' ' && *tail)
tail++;
temp = (--tail);
swap(&head,&tail);
if(*(tail+1)=='\0') break;
head = temp+2;
tail = temp+2;
}
}
int main(int argc, const char *argv[])
{
char str[100]={0};
printf("please input a str:");
scanf("%[^\n]",str);
strchange(str);
printf("str revese:%s\n",str);
return 0;
}