473,386 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C question: Returning string array from function

Relatively new to C coding, so any help would greatly be appreciated.
I'm having problems try to return my string array from my parsing
function. When I do a printf I am getting the correct value for my first
element but my subsequent printf's will return garbage. Can someone let
me know what I am doing wrong. Thanks in advance.

-jlewis

typedef char wrkspace[40][200]; <- Locatated in my haeder
int open_file (char *path, char *recNo, int fid) {
FILE *fp;
char str [1000];
int ret;
int count;
int inx;
int i;
wrkspace *value;

if ((fp=fopen(path, "r"))==NULL) {
printf("Cannot open file.\n");
return -1;
}

count = atoi(recNo);
memset (str, NULL, 1000);
fgets( str, sizeof(str), fp );

while (!feof(fp)) {
for (inx = 0; (inx < count ) && (!feof(fp)) && ( strlen(str) > 0
); inx++) {
<--CALLING ROUTINE --->
value = (wrkspace *) parse_record(&str, fid, count);

printf ("0:%s\n", inx, value[0]); <-- returns good data
printf ("1:%s\n", inx, value[1]); <-- Everything else garbage
printf ("2:%s\n", inx, value[2]);
printf ("3:%s\n", inx, value[3]);
printf ("4:%s\n", inx, value[4]);

memset (str, NULL, 1000);
fgets( str, sizeof(str), fp );
}
}

fclose(fp);
return ret;
}

wrkspace *parse_record(char (*record) [], int fid, int structsz) {
int i=0;
int j;
char *token;
char sep_1[] = "|";
char sep_2[] = "'";
wrkspace value;

token = strtok( *record, sep_1 );

while( token != NULL ) { // While there are tokens in "string"
memset (value[i], NULL, 200);
strcpy (value[i], token);
token = strtok( NULL, sep_1 ); // Get next token:

i++;
}

j = i - 2; // Minus 2 is remove the last '|'

for (i=0; i <= j; i++) {
token = strtok (value[i], sep_2);
strcpy (value [i], token);
}
return &value;
}

Nov 14 '05 #1
1 3298
On Mon, 28 Jun 2004 20:51:44 -0400, john <jl****@yahoo.com> wrote:
Relatively new to C coding, so any help would greatly be appreciated.
I'm having problems try to return my string array from my parsing
function. When I do a printf I am getting the correct value for my first
element but my subsequent printf's will return garbage. Can someone let
me know what I am doing wrong. Thanks in advance.

-jlewis

typedef char wrkspace[40][200]; <- Locatated in my haeder
int open_file (char *path, char *recNo, int fid) {
FILE *fp;
char str [1000];
int ret;
int count;
int inx;
int i;
wrkspace *value;

if ((fp=fopen(path, "r"))==NULL) {
printf("Cannot open file.\n");
return -1;
}

count = atoi(recNo);
memset (str, NULL, 1000);
fgets( str, sizeof(str), fp );

while (!feof(fp)) {
for (inx = 0; (inx < count ) && (!feof(fp)) && ( strlen(str) > 0
Why do you test feof(fp) twice, once in the while and once in the for?
Neither is correct. feof does not tell you there is no more data; it
tells you the last read attempted to go beyond the end of the data.
); inx++) {
<--CALLING ROUTINE --->
value = (wrkspace *) parse_record(&str, fid, count);
There is no prototype in scope for parse_record. A C89 compiler is
required to assume the function returns an int. Since it does not,
you have invoked undefined behavior. Create a prototype. Once you
have that, you can eliminate the superfluous cast.

printf ("0:%s\n", inx, value[0]); <-- returns good data
Only by accident. value is a pointer to an array of 40 arrays of 200
char. value[0] is the first array of 40 arrays of 200 char at that
address. When an array is passed to a function, the compiler actually
passes the address of the first element with type pointer to element.
Thus, the address passed is the address of the first array of 200
char. Unfortunately, it also has type pointer to array of char which
is the wrong type for the %s format. This also invokes undefined
behavior.
printf ("1:%s\n", inx, value[1]); <-- Everything else garbage
value[1] is the second array of 40 arrays of 200 char. This address
is some 800 bytes beyond value[0]. Not at all what you intended.
printf ("2:%s\n", inx, value[2]);
printf ("3:%s\n", inx, value[3]);
printf ("4:%s\n", inx, value[4]);

memset (str, NULL, 1000);
The second argument has type int. On many systems, NULL is defined as
(void*)0 which is incompatible with int. When you mean zero, use 0.
fgets( str, sizeof(str), fp );
}
}

fclose(fp);
return ret;
ret is never initialized. Attempting to evaluate an uninitialized
variable invokes undefined behavior.
}

wrkspace *parse_record(char (*record) [], int fid, int structsz) {
int i=0;
int j;
char *token;
char sep_1[] = "|";
char sep_2[] = "'";
wrkspace value;
value is an automatic variable that exists only for the duration of
this function.

token = strtok( *record, sep_1 );

while( token != NULL ) { // While there are tokens in "string"
memset (value[i], NULL, 200);
This doesn't serve any purpose. The following strcpy does not require
the target area to be initialized.
strcpy (value[i], token);
token = strtok( NULL, sep_1 ); // Get next token:

i++;
}

j = i - 2; // Minus 2 is remove the last '|'

for (i=0; i <= j; i++) {
token = strtok (value[i], sep_2);
strcpy (value [i], token);
token points somewhere in value[i]. Since the arguments to strcpy
overlap, you have invoked undefined behavior. memmove may do what you
want.
}
return &value;
You are returning the address of a variable that will disappear as
soon as the function exits. Any attempt to access the variable, as
you do in open_file, invokes undefined behavior.
}


You can solve many of these problems by:

Change the typedef for workspc to char[200]

Change the first parameter of parse_record to char* and specify
str (not &str) in the function call. This will require you to change
*record to record in the body.

In parse_record, change value to a pointer and initialize it with
the address of an allocated area obtained from malloc:
value = malloc(40 * sizeof *value);
<<Remove the del for email>>
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
5
by: shyam | last post by:
Hi All I have to write a function which basically takes in a string and returns an unknown number( at compile time) of strings i hav the following syntax in mind char *tokenize(char *) ...
4
by: John | last post by:
Hi I need to return an array of string in my own split function (access 97). I have defined the function as below but I get err on 'As String()'. What can I do to make the function return an...
4
by: cjl | last post by:
As a learning exercise, I am trying to write a web-based version of 'drawbot' in PHP. See: http://just.letterror.com/ltrwiki/DrawBot I am interested in hearing ideas about how to approach...
13
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns...
0
by: anuptosh | last post by:
Hi, I have been trying to run the below example to get a Oracle Array as an output from a Java code. This is an example I have found on the web. But, the expected result is that the code should...
8
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
26
by: Bill Cunningham | last post by:
I was talking with someone about fgets and he said that fgets puts the \n in a string but not \0. I decided to test this assumption because my documentation didn't say if fgets put \0 after a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.