急,字符串查找问题
#include <stdio.h>
#include <string.h>
int *getPostion(char *str,char *findstr){
char *strTemp = str;
static int pos[100];
int m = 0;
while(1)
{
strTemp = strstr(strTemp,findstr);
if(NULL == strTemp)
break;
else
{
printf( "Position:%d\r\n ",strTemp - str);
pos[m] = strTemp - str;
m++;
strTemp += strlen(findstr);
}
}
return pos;
}
int main()
{
//char str[] = "hello world,hello,helloworld,hello,hello,world! ";
char str[] = "hello world,hello,helloworld ";
char findstr[] = "hello ";
int *pos = getPostion(str,findstr);
printf( "size:%d\r\n ",sizeof(pos));
int m = 0;
for(m = 0 ; m < sizeof(pos); m++){
printf( "Pos:%d\r\n ",pos[m]);
}
}
我想得取findstr在str串中位置,返回一个数组
但是我返回的sizeof(pos)为什么每次都是4
请问有什么办法。。。
[解决办法]
没有办法,sizeof(int *)永远都是4,sizeof(int [])也是一个定值
可以把数量存放到pos[0]
[解决办法]
pos是函数的局部数组,传不出来,只能传出数组的首地址。
所以事先用一个数组专门来存放pos的信息,见以下void main()函数。
void getPostion(int pos[],char *str,char *findstr){
//对pos的数据进行修改
}
int getLen(int a[])
{
int i=0;
while (a[i++]!=0)
return i;
}
void main()
{
int pos[10]={0,0,0,0,0,0,0,0,0,0}; //初始化全零
getPostion(pos,str,findstr);
int size_pos=getLen(pos); //得到sizeof(pos)
}
[解决办法]
加个參數放個數
[解决办法]
sizeof()返回的是类型的长度:int:4,Int*:4。
把长度放在a[]中就行了啊
[解决办法]
但是我返回的sizeof(pos)为什么每次都是4
sizeof(pos)是指针的size
[解决办法]
int *pos
4个字节