Re: Cannot return values of char variable




Prasad escreveu:
Pedro Pinto wrote:
Ok, here it goes.

<problem description and code snippet sniped>

For your implementation of the divide function , i have created the
following test case (which I guess was what Mark was asking you to do).

#include <stdio.h>
#include <stdlib.h>

void divide(char search_string[], char *array[], char *delimiter){
int loop;
array[0]=strtok(search_string,delimiter);

if(array[0]==NULL)
{
printf("No test to search.\n");
exit(0);
}

for(loop=1;loop<50;loop++)
{
array[loop]=strtok(NULL,delimiter);

if(array[loop]==NULL)
break;
}

for(loop=0;loop<50;loop++)
{
if(array[loop]==NULL)
break;
//printf("\nDivide - array[%d] = %s", loop,array[loop]);
}
}

int main(void) {
int i = 0 ;
char *arr[50];

divide("this,is,comma,separated,string", arr ,",");

printf("Divided contents\n");
for(i = 0; i < 50; i++)
{
if(arr[i]==NULL)
break;

printf("%s \n", arr[i]);
}
return 0;
}

Compilation :

Microsoft (R) 32-bit C/C++ Standard Compiler Version 13.00.9466 for 80x86
Copyright (C) Microsoft Corporation 1984-2001. All rights reserved.

sample.c
sample.c(6) : warning C4047: '=' : 'char *' differs in levels of
indirection from 'int'
sample.c(16) : warning C4047: '=' : 'char *' differs in levels of
indirection from 'int'
Microsoft (R) Incremental Linker Version 7.00.9466
Copyright (C) Microsoft Corporation. All rights reserved.

/out:sample.exe
sample.obj

Execution Result :

Divided contents
this
is
comma
separated
string

I am not sure what you want to return from divide function.



--
Prasad


I can do that inside main. What i can't do is with divide:

char divide(char search_string[], char *array[], char *delimiter)

and return the string you printed! Can this be done?

.