请问如何改写这个函数,返回一个整型数组.

xunfengxxx 2005-01-07 10:56:50
1.我先介绍我这个函数功能:
把输入的字符串如"22222 3333 333 "
22222 3333 33三个数字存入一个整型数组
函数要求写成
int * GET(char *s)
的形式
指针无法传递
在主函数调用的时候如何帮函数局部的变量还存在?
问过一个高手,说用指针的指针可以解决,不过没有给出具体的实现,请CSDN的大虾帮忙解决


/////////////////////
以下程序是我想的笨办法,供修改使用
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
static int num[100];
static char temp[4];
int GET(char *s)//请教如何把这个函数改成int* GET(char *s)就是把字符串里的数字存入这个数组并且数组不被销毁
{
int len=strlen(s);

int count=0;

int count1=0;
for(int i=0;i<len;i++)
{

temp[count1]=s[i];
count1++;
if(isspace(s[i]))
{
//cout<<"wwww"<<temp<<endl;;
num[count]=atoi(temp);
count1=0;
count++;
}

}
return count;//数组元素个数

}

void main()
{

char *ss="21 321 33213 ";
int count=GET(ss);
for(int i=0;i<count;i++)
cout<<num[i]<<endl;


}



问题2.
请问C语言的为何char数组可以测其长度?
而int 数组为何没有提供这个方法?
...全文
276 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuelong_zl 2005-01-08
  • 打赏
  • 举报
回复
mark先
YFY 2005-01-08
  • 打赏
  • 举报
回复
oo(为了名副其实,努力学习oo技术ing) ( ) 信誉:105
对...

可用strcspn(const char *str1,const char *str2)定位空格,取数据
Flood1984 2005-01-08
  • 打赏
  • 举报
回复
其他类型可使用相同的方法
Flood1984 2005-01-08
  • 打赏
  • 举报
回复
数组的长度取得方法:
如下定义时
int a[50];
int length;

数组长度length=sizeof(a)/sizeof(int);
cat_dog 2005-01-08
  • 打赏
  • 举报
回复
上面问题忘了释放内存,在main函数的最后应该加上一条free(pcount);

问题2.
请问C语言的为何char数组可以测其长度?
而int 数组为何没有提供这个方法?

不管是char数组还是int 数组都可以知道大小,但如果是分配的内存就不能这样做了。

int a[100];
char b[100];
int iLena = sizeof(a) / sizeof(int);
int iLenb = sizeof(b) / sizeof(char);

则iLena和iLenb都是100;
cat_dog 2005-01-08
  • 打赏
  • 举报
回复
问题1:
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
static int num[100];
static char temp[4];
int* GET(char *s)//请教如何把这个函数改成int* GET(char *s)就是把字符串里的数字存入这个数组并且数组不被销毁
{
int len=strlen(s);
int count=0;
int count1=0;
int *piReturn,iLen = 0; //定义两个变量,一个是返回的数组,一个是数组的大小

//下面计算有多少个数
for(int i=0;i<len;i++)
{
temp[count1]=s[i];
count1++;
if(isspace(s[i]))
{
iLen++;
}

}

//分配内存,数组一个做数组的大小
iLen++;
piReturn = (int *)malloc(iLen * sizeof(int));


for(int i=0;i<len;i++)
{

temp[count1]=s[i];
count1++;
if(isspace(s[i]))
{
//cout<<"wwww"<<temp<<endl;;
count1=0;
count++;
piReturn = atoi(temp);

}

}
piReturn[0] = iLen - 1;

return piReturn;//数组元素个数

}

void main()
{

char *ss="21 321 33213 ";
int *pcount=GET(ss);
for(int i=;i<*pcount;i++)
cout<<pcont[i]<<*endl;


}

yeehya 2005-01-08
  • 打赏
  • 举报
回复
这种写法不太好吧;在一个子函数申请了内存,没有释放,这样很容易被遗忘的.
为什么不这样写int getArray(int *arrayname,char* s)
现在要调用该函数的地方申请合适的内存,使用后释放.
dongpy 2005-01-08
  • 打赏
  • 举报
回复
1楼的方法很好!
snow810211 2005-01-07
  • 打赏
  • 举报
回复
楼上大哥说得很好。
oo 2005-01-07
  • 打赏
  • 举报
回复
用 int * GET(char *s)
这样的格式也没问题呀
在GET函数里先 int* temp = (int*)malloc(100*sizeof(int));;//大小你自己定了
temp[0]存放count,temp[1]开始存放字符串里得到的值
最后return temp;
在调用GET的地方,int* pRet = GET(ss);调用完GET后记得 free(pRet)

请问C语言的为何char数组可以测其长度?
而int 数组为何没有提供这个方法?
-------------
你说的char数组长度是字符串长度吧,strlen(ss),能求字符串长度是因为字符串是以'\0'结束的。

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧