Passing parameters to a function 
Author Message
 Passing parameters to a function

: I've function that should take the following parameters: Do_Print(x, y, z)
: where x & y are floats and z should be passed as char *val[]. Input values
: for x, y, and z come form a single file with the following format: 6.00 23.2
: 34.9 9.00 34.9 90.0 where the first column should be read as z and the other
: two as x & y. To pass these parameter values to the function, they should be
: first read from the file. I have no problem reading x & y. But how do I read
: z in order to pass it as a char *val[] to the function. Thanks in advance.

I'm not sure if you mean 'char *val[]', which makes val an array of
pointers to character, or 'char val[20]', which makes val an array of
characters, or string.  I'll assume the latter - if I'm wrong, just
change the level of indirection, and make sure you have storage
allocated to val[whatever] before you write to it.

How you read z depends on how you are reading x and y.  scanf("%[^ ]", val)
would be the easiest method, but reading the data with fgets() and splitting
it up with strtok() could be made more reliable.  If you use scanf(),
check its return value, and limit the size of the input string - that
is, if val is 20 characters long, use the format string "%19[^ ]".

Will



Sat, 15 Sep 2001 03:00:00 GMT  
 Passing parameters to a function

Quote:

> Ok, what I meant to say by using char *val[] is that it is an array of
> pointers. (Sorry I was not clear the first time). And the Input file looks
> like this: 6.00 23.2 34.9 9.00 34.9 90.0 Now the question is how to read all
> the first column values from the file into the z array of pointers so that
> the Do_Print(x, y, z) function could be called. Thanks.

I am not totally sure but I believe you are saying that you have a line of data
consisting of 6 numbers that you want to split up and use a char * array to point
to each number for the purpose of printing the numbers as strings. If this is the
case, then the function to use is strtok(). Use fgets to read the line of data in
the file into a char array and use strtok similiar to the following:

#include <stdio.h>
#include <string.h>

void do_print(char **arr);

int main(void) {
   char *s,*carr[6];
   char buf[80] = "6.00 23.2 34.9 9.00 34.9 90.0";
   int count = 0;

   s = strtok(buf," \n");
   while(s != NULL) {
      carr[count++] = s;
      s = strtok(NULL," \n");
      }
   if(count == 6) do_print(carr);
   else puts("file format error");
   return 0;
   }

void do_print(char **carr) {
   int i;

   for(i = 0;i < 6;i++)
      printf("carr[%d] points to \"%s\"\n",i,carr[i]);
   return;
   }

--
Al Bowers
Tampa, FL

http:www.gate.net/~abowers/index.html



Sat, 15 Sep 2001 03:00:00 GMT  
 Passing parameters to a function



: > I've function that should take the following parameters: Do_Print(x, y, z)
: > where x & y are floats and z should be passed as char *val[]. Input values
: > for x, y, and z come form a single file with the following format: 6.00 23.2
: > 34.9 9.00 34.9 90.0 where the first column should be read as z and the other
: > two as x & y. To pass these parameter values to the function, they should be
: > first read from the file. I have no problem reading x & y. But how do I read
: > z in order to pass it as a char *val[] to the function. Thanks in advance.
: >

: Ok, what I meant to say by using char *val[] is that it is an array of
: pointers. (Sorry I was not clear the first time). And the Input file looks
: like this: 6.00 23.2 34.9 9.00 34.9 90.0 Now the question is how to read all
: the first column values from the file into the z array of pointers so that
: the Do_Print(x, y, z) function could be called. Thanks.

   char *pc, buff[256];
   int j = 0;
   if (fgets(buff, sizeof(buff), stdin)) {
         while ((pc = strtok(j ? NULL : buff, " "))
                        && (val[j] = malloc(strlen(pc) + 1))
                strcpy(val[j++], pc);
   }

will do about what you want; however, it's overly terse and lacks error
checking.  I'm still not clear if you want to pass x and y as strings,
elements of the z array (as I've shown above) or as separate floating
point values.  If the latter, then convert the appropriate z strings
with strtod().  And don't forget to free() the memory got from malloc().

Will



Sat, 15 Sep 2001 03:00:00 GMT  
 Passing parameters to a function
Ritchie Sobell schrieb:
[snipped rpevious version of description]

Quote:
> Ok, what I meant to say by using char *val[] is that it is an array of
> pointers. (Sorry I was not clear the first time). And the Input file looks
> like this: 6.00 23.2 34.9 9.00 34.9 90.0 Now the question is how to read all
> the first column values from the file into the z array of pointers so that
> the Do_Print(x, y, z) function could be called. Thanks.

IMHO your second explanation has not made it much clearer. Do you know
what pseudo-code is ? Try to explain your problem in the form of steps to
be executed one after another, so that the given goal is achieved. One
problem I have is that you repeatedly speak of columns, but all you show
as an example input file is one *line* of values.

Pseudo-code might look like this:

  1) read one value from file
  2) store value at current position in list
  3) increase current list position list to next position
  4) continue at 1) if there are still values to be read

This is of course a very simple version. You should create a much more
detailed pseudo code description that most closely matches your problem.

Stephan
(initiator of the campaign against grumpiness in c.l.c)
(-: A brandnew excellent FAQ version has been released !!! :-)
(-: Get it: http://www.eskimo.com/~scs/C-faq/versions.html :-)



Sat, 15 Sep 2001 03:00:00 GMT  
 Passing parameters to a function

Quote:

>    while (fgets(inarray, sizeof(inarray), fp) != NULL)
>    {
>                sscanf( inarray, "%s %s %s", tep, tep1, tep2);

>   x[count] = atof(tep1);  y[count] = atof(tep2);  count++;  }

....snip......If you have confidence in the accuracy of the data in the file, you
can use
sscanf(inarray,"%s %f %f". etc.

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

void Do_Print(float,float, const char *);

int main(void) {
   float x[48], y[48];
   char inarray[40], z[48][8];
   int i,count;
   FILE *fp;

   if((fp = fopen("test.dat","r")) == NULL) {
      perror("Unable to open \"test.dat\"");
      exit(EXIT_FAILURE);
      }
   for(count = 0; count < 48;count++) {
      if(fgets(inarray, sizeof(inarray), fp) == NULL) break;
      if( 3 != sscanf( inarray, "%7s %f %f",
                       z[count],&x[count], &y[count])) {
         puts("file data format error");
         exit(EXIT_FAILURE);
         }
      }
   fclose(fp);
   for(i = 0; i < count;i++) Do_Print(x[i],y[i],z[i]);
   return 0;
   }

void Do_Print(float x,float y,const char *z) {
   printf("%s %.2f %.2f\n",z,x,y);
   return;
   }
--
Al Bowers
Tampa, FL

http:www.gate.net/~abowers/index.html



Sat, 15 Sep 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Pass parameter to a function

2. Problem passing parameter to a function

3. GetProcAddress: Problem with passing parameters to dll-functions

4. Urgent::passing parameters to exported function

5. Passing C++ Class Member Function to as a C Callback Function Parameter

6. calling C++ function from Fortran: parameter passing

7. how do I pass arrays as parameters to functions - newbie

8. parameters pass between function and main

9. Passing parameters to a Thread'ed function

10. function passed as parameter....

11. Pb with passing a function as parameter

12. passing parameters with functions

 

 
Powered by phpBB® Forum Software