怎样让函数返回一个二维数组?解决方法
怎样让函数返回一个二维数组?
自己试验了一个程序如下,可以将字符窜按照字母一个个提取出来,存进一个二维数组,代码如下:
#include <stdio.h>
#include <string.h>
#include <iostream>
main()
{
char a[30]="apple0305book0209like1256";
static char b[5][15];
int flag;
int i=0;
int j=0;
int k=0;
char *p;
p=a;
while(i<30)
{
if(isalpha(a[i])!=0);
{
i++;
k++; //记录有多少个字母
}
strncpy(p,b[j],k+4);
p=p+k+4;
//printf("
puts(b[0]);
puts(b[1]);
}
getchar();
}
现在想将这个功能写到一个函数里面,应该怎样定义,自己试了下不行:
#include <stdio.h>
#include <string.h>
#include <iostream>
char out[10][15] loadwords(char *words)
{
char temp[10][15];
int i=0;
int j=0;
int k=0;
char *p;
p=words;
while(i<30)
{
while(isalpha(words[i])!=0)
{
i++;
k++; //记录有多少个字母
}
strncpy(out[j],p,k+4);
j++;
p=p+k+4;
i=i+4;
k=0;
}
return temp;
}
main()
{
char a[30]="apple0305book0209like1256";
//char b[100]="cook0406destination0708final1209sati1008crazy0102";
static char c[10][15];
c=loadwords(a);
puts(c[0]);
getchar();
}
------解决方案--------------------
二维数组大小是确定的吗?
有个无聊的方法
自己试验了一个程序如下,可以将字符窜按照字母一个个提取出来,存进一个二维数组,代码如下:
#include <stdio.h>
#include <string.h>
#include <iostream>
main()
{
char a[30]="apple0305book0209like1256";
static char b[5][15];
int flag;
int i=0;
int j=0;
int k=0;
char *p;
p=a;
while(i<30)
{
if(isalpha(a[i])!=0);
{
i++;
k++; //记录有多少个字母
}
strncpy(p,b[j],k+4);
p=p+k+4;
//printf("
puts(b[0]);
puts(b[1]);
}
getchar();
}
现在想将这个功能写到一个函数里面,应该怎样定义,自己试了下不行:
#include <stdio.h>
#include <string.h>
#include <iostream>
char out[10][15] loadwords(char *words)
{
char temp[10][15];
int i=0;
int j=0;
int k=0;
char *p;
p=words;
while(i<30)
{
while(isalpha(words[i])!=0)
{
i++;
k++; //记录有多少个字母
}
strncpy(out[j],p,k+4);
j++;
p=p+k+4;
i=i+4;
k=0;
}
return temp;
}
main()
{
char a[30]="apple0305book0209like1256";
//char b[100]="cook0406destination0708final1209sati1008crazy0102";
static char c[10][15];
c=loadwords(a);
puts(c[0]);
getchar();
}
------解决方案--------------------
二维数组大小是确定的吗?
有个无聊的方法
- C/C++ code
struct ma { char out[10][5] }; /*返回值类型用struct ma吧,楼主懂的*/
------解决方案--------------------
返回指针呗
------解决方案--------------------
返回一个二维指针吧....
P.S:这个指针要new出来....然后在不用的时候要记得delete....