|
>説明不足ですいません。
>CSVファイル(abc,def,gh…)のデータを一行ずつ抜き出してStrSplit関数内で文字列の配列に格納し、そのポインタをmain関数に渡して構造体にセットしたい。
なるほど。やりたいことはなんとなくわかりました。
すると、さっきのアドバイスはちょっとずれてますね。
malloc()を使うのはかわりませんが、こんな感じでやります。
コメント少ないので、なにをしているかきちんと把握してから実装してください。
---
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <malloc.h>
char **a(char *);
int main(void)
{
char **ppList;
int i;
static char text[40] = "abc,def,ghi,jkl,mno,pqr,stu";
/* function call */
ppList = a(text);
if( ppList != (char **)NULL ){
/* メモリが確保できていれば設定されたメッセージを表示 */
for( i = 0; i < 20; i++ ){
if( ppList[i] != NULL ){
printf( "data%02d = %s\n", i, ppList[i] );
}
}
free( ppList );
}
else{
printf( "memory allcation error!\n" );
}
return 0;
}
char **a(char *text)
{
char **ppData;
char *found, *target;
char *mem;
int cnt;
int i;
/* メモリの確保 */
cnt = 0;
ppData = (char **)malloc( sizeof(char *) * 20 );
if( ppData != (char **)NULL ){
/* 成功したら見つけた文字列へのポインタを埋める */
for( i = 0; i < 20; i++ ){
ppData[i] = (char *)NULL;
}
/* inputから","をサーチし、
文字列の先頭のポインタをポインタ配列にいれる */
target = text;
do{
found = strstr(target,",");
if( found != (char *)NULL ){
*found = '\0';
ppData[cnt] = target;
found ++;
target = found;
cnt ++;
}
else{
ppData[cnt] = target;
}
}while( found != (char *)NULL );
}
return ppData;
}
---
|