Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about Strings

Status
Not open for further replies.

Kaotic

Technical User
Nov 22, 2004
9
US
OK, I'm using pico on the unix system, and I'm trying to run a program with strings, arrays, functions, and file statements. How do I pass character string arrays to functions? I tried doing it like this, but I keep getting errors.

char botanical[40][n], common[40][n]; /* n defined as 30*/
/skip/
printName(writefile1, id, botanical, common); /* line 50 */
/function/
void printName(FILE *writefile1, int id[], char *botanical[][], char
*common[][])
{
int i;
for(i = 0; i < n; i++)
fprintf(writefile1, &quot;%d%4s%4s\n&quot;, id, *botanical, *common); /*
line 80 */
}
/error/
In function 'main':
prog.c:50: warning: passing arg 3 of 'printName' from incompatible
pointer type
prog.c:50: warning: passing arg 4 of 'printName' from incompatible
pointer type
prog.c: In function 'printName':
prog.c:84: arithmetic on pointer to an incomplete type
prog.c:84: arithmetic on pointer to an incomplete type
 
Hi Kaotic

Make that function ...
[ignore]
void printName(FILE *writefile1, int id[], char botanical[][], char common[][])
{
int i;
for(i = 0; i < n; i++)
fprintf(writefile1, &quot;%d %4s %4s\n&quot;, id, botanical, common );
}
[/ignore]

I think that should do it.
The reason is that char *botanical[][] makes the function expect a 2-D array of pointers to char, instead of just a 2-D array (the base address of the 2-D array, to be more precise). Same for common[][]. To know more see faq205-924. Bye.
Ankan.

Please do correct me if I am wrong. s-)
 
Okay, thanks it worked. But now I'm getting a segmentation fault for this while loop. What should I do?

while (!feof (readfile) )
{
fscanf( readfile, &quot;%d%s%s%d%d%lf&quot;, &id, botanical, common, &maxinv, &stock, cost);
i++;
}
 
Oh, and I don't know what happens to the i's. But it should come out like this.
while (!feof (readfile) )
{
fscanf( readfile, &quot;%d%s%s%d%d%lf&quot;, &id[.i], botanical[.i], common[.i], &maxinv[.i], &stock[.i], cost[.i]);
i++;
}
 
Nevermind that. I fixed it, but now I have another segmentation fault in this function for some reason.

retailHeading(writefile1);

void retailHeading(FILE *writefile1)
{
fprintf(writefile1, &quot; Beautiful Plants, Inc.\n&quot;);
fprintf(writefile1, &quot; Retail Plant List\n\n&quot;);
fprintf(writefile1, &quot;%s%6s%15s\n&quot;, &quot;Plant#&quot;, &quot;Botanical Name&quot;, &quot;Common
Name&quot;);
fprintf(writefile1, &quot;%s%6s%15s\n&quot;, &quot;------&quot;, &quot;----------------------&quot;, &quot;--------------------&quot;);
}
 
kaotic,
there is absolutely no problem in this part of your code
and there is no segmentation fault in this part

it may be a a minor compile
time fault depending upon you compiler but that has
to do with the line seperator

there may be segmentation fault in other parts

this part of your code is fault free
 
>> kaotic,
there is absolutely no problem in this part of your code
and there is no segmentation fault in this part

it may be a a minor compile
time fault depending upon you compiler but that has
to do with the line seperator

there may be segmentation fault in other parts

this part of your code is fault free >>

thanks. Well it printed something right before the function and right after the function, so all I can think of that might be wrong is that maybe I didn't open the files right. This is how I opened the files.

FILE *readfile; FILE *writefile1; FILE *writefile2;

if ( ( ( readfile = fopen( &quot;plants.data&quot;, &quot;r&quot; ) ) || ( writefile1 = fopen( &quot;retail.data&quot;, &quot;w&quot; ) ) || ( writefile2 = fopen( &quot;purchase.data&quot;, &quot;w&quot;) ) ) == NULL)
printf( &quot;File could not be opened\n&quot; );

else{
 
yes kaotic there is a problem there

what has happened is

the entire expression of your if is evaluated due
to braces and even if one file is successfully opened
the result is true

so open the file individually and check with the
condition

or if you want in a single line change as you have done

change ||(or) to &&(and)

yogesh
and this will work ,but its not clean

reply if you have problems
 
It's running okay now. But one more question. How do you align strings up? I'm trying to do this.

fprintf(writefile1, &quot;%d%6s%15s\n&quot;, id, botanical, common);

But it's coming out like this.

Thanks, it's running okay now.
But one more question. How do you align strings up? I'm trying to do this.

fprintf(writefile1, &quot;%d%6s%15s\n&quot;, id, botanical, common);

But it's coming out like this.
1933Brassaia_actinophylla Schefflera
2244Caryota_mitis Fishtail_Palm
2389Chrysalidocarpus_lutescens Areca_Palm
 
Blagh, pay no attention to the first half.
 
kaotic

use /t for a tab /b for a single blank
reply if it helped
yogesh
 
>> use /t for a tab /b for a single blank
reply if it helped
yogesh >>

It helped a little, but the names still align unevenly since each set has a different amount of characters.
 
kaotic try this and let me know if its ok

fprintf(writefile1, &quot;%-5d%-15s%-35s\n&quot;, id, botanical, common);

change the no as required
let me know if it helped
yogesh

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top