Sponsored Content
Full Discussion: passing float parameter
Top Forums Programming passing float parameter Post 302583168 by migurus on Monday 19th of December 2011 07:38:52 PM
Old 12-19-2011
passing float parameter

I am surprised by GCC (this is ver. 4.2.4, Ubuntu 32 bit Intel) when a function declares a float parameter and it's prototype is missing, the parameters are messed up.

Please see my code below:
Code:
~/test$ cat x1.c
#include <stdio.h>
#include <stdlib.h>
set_p(int p1, float p2, int p3, int p4)
{
        printf("p1=%i\n", p1);
        printf("p2=%.4f\n", p2);
        printf("p3=%i\n", p3);
        printf("p4=%i\n", p4);
        return(0);
}
main(int argc, char *argv[])
{
  float x1 = (argc == 2) ? atof(argv[1]) : 1.2345;
        set_p(1, x1, 2, 3);
        set_P(1, x1, 2, 3);
        return(0);
}
~/test$ cat x2.c
#include <stdio.h>
set_P(int p1, float p2, int p3, int p4)
{
        printf("p1=%i\n", p1);
        printf("p2=%.4f\n", p2);
        printf("p3=%i\n", p3);
        printf("p4=%i\n", p4);
        return(0);
}
 
I make it like this:
cc -c x1.c
cc -c x2.c
cc x1.o x2.o

The output:
Code:
~/test$ ./a.out
p1=1
p2=1.2345
p3=2
p4=3
p1=1
p2=0.0000
p3=1072939139
p4=2

As you see, the set_P function somehow messed up parameters.

***Note: the code does work on SCO with stock compiler on similar h/w.

If I add the function prototype to x1.c

Code:
int set_P(int , float , int , int );

then everything works correctly, no surprises.


My understanding is that compiler allocates longs for longs/integers/shorts and doubles for float/double when passing parametrs by value. Then if receiver function expects a float, it will get first 32 bit, which is OK on Intel. Apparently my understanding is not correct.

The code in many places lacks function prototypes, I wander if I can somehow influence compiler to pass float/double parameters and not mess up. Any ideas?
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

parameter passing

Hallo everyone, This is my problem below: /home/cerebrus/pax=>vat class2.sh ksh: vat: not found /home/cerebrus/pax=>cat class2.sh #!/bin/ksh set -x bdf|grep appsdev|awk '{ print $5 }'> class3 dd={cat class3} echo $dd /home/cerebrus/pax=> /home/cerebrus/pax=>./class2.sh + bdf +... (8 Replies)
Discussion started by: kekanap
8 Replies

2. UNIX for Advanced & Expert Users

Parameter passing in a function

I need to pass a parameter to a function in a script. My parameter is a string. When I display the parameter within my function, I only get the first word from string I pass in. How can I make the function receive the whole string (and not terminate at the first space it encounters)?. part of... (2 Replies)
Discussion started by: fastgoon
2 Replies

3. Programming

Passing parameter to makefile?

Hi, How to pass parameter to makefile? Please let me know if any one knows and also please put an example of makefile with this feature. thanks, Manju. (3 Replies)
Discussion started by: manju_p
3 Replies

4. Shell Programming and Scripting

wrong parameter passing!

Hi all I have a script which will take input as filename and passes it to a java program. It is as follows -------------------------------- FILENAME=$1 echo $FILENAME ${JAVA_HOME}/bin/java -cp DateProvider $FILENAME ------------------------------------------------- when I execute the same... (2 Replies)
Discussion started by: malle
2 Replies

5. Shell Programming and Scripting

Passing asterisk As A Parameter

I have written a Shell Script Program which accepts 3 parameters as shown below: ./calc 20 + 2 in the above line ./calc is the Shell Script itself with 3 parameters, namely: 20 + and 2. Well, now let's look inside the Script: result=$1$2$3 echo $result The output will be as... (8 Replies)
Discussion started by: indiansoil
8 Replies

6. Shell Programming and Scripting

Parameter Passing problem

Hi All, I developed a KSH script which will accept two parameters as input. These two parameters are some directories paths. In the script i am validating the number of paramaters it received as below #-------------------------------------- # Check Command line arguments... (8 Replies)
Discussion started by: Raamc
8 Replies

7. Shell Programming and Scripting

Positional parameter passing

Hi All, When passing parameters to a sheel script, the parameters are referenced by their positions such as $1 for first parameter, $2 for second parameter. these positional values can only have values ranging from $0-$9 (0,1,2,3...9). I have a shell script meant to accept 20 parameters. for... (3 Replies)
Discussion started by: ogologoma
3 Replies

8. Shell Programming and Scripting

Passing parameter to script, and split the parameter

i am passing input parameter 'one_two' to the script , the script output should display the result as below one_1two one_2two one_3two if then echo " Usage : <$0> <DATABASE> " exit 0 else for DB in 1 2 3 do DBname=`$DATABASE | awk -F "_" '{print $1_${DB}_$2}` done fi (5 Replies)
Discussion started by: only4satish
5 Replies

9. Shell Programming and Scripting

Passing parameter more than 9

Hi, I've written a script where eleven parameter to be passed from command line which is inserting into an oracle table, it is working but the tenth and 11th parameter are not accepting as given it is referring to 1st parameter. HERE IS THE SCRIPT #!/bin/ksh #set -o echo $*... (4 Replies)
Discussion started by: sankar
4 Replies

10. Shell Programming and Scripting

Passing parameter through file

Hi , I am passing date parameter through file my shell script testing.sh is #set -x #set -v asd=$1 asd1=$2 echo $asd echo $asd1 Passing parameter as below sh testing.sh `cat file1.txt` Output (2 Replies)
Discussion started by: kaushik02018
2 Replies
GMAGICKDRAW(3)								 1							    GMAGICKDRAW(3)

The GmagickDraw class

INTRODUCTION
CLASS SYNOPSIS
GmagickDraw GmagickDraw Methods o public GmagickDraw GmagickDraw::annotate (float $x, float $y, string $text) o public GmagickDraw GmagickDraw::arc (float $sx, float $sy, float $ex, float $ey, float $sd, float $ed) o public GmagickDraw GmagickDraw::bezier (array $coordinate_array) o public GmagickDraw GmagickDraw::ellipse (float $ox, float $oy, float $rx, float $ry, float $start, float $end) o public void GmagickDraw::getfillcolor (void ) o public float GmagickDraw::getfillopacity (void ) o public string GmagickDraw::getfont (void ) o public float GmagickDraw::getfontsize (void ) o public int GmagickDraw::getfontstyle (void ) o public int GmagickDraw::getfontweight (void ) o public void GmagickDraw::getstrokecolor (void ) o public float GmagickDraw::getstrokeopacity (void ) o public float GmagickDraw::getstrokewidth (void ) o public int GmagickDraw::gettextdecoration (void ) o public string GmagickDraw::gettextencoding (void ) o public GmagickDraw GmagickDraw::line (float $sx, float $sy, float $ex, float $ey) o public GmagickDraw GmagickDraw::point (float $x, float $y) o public GmagickDraw GmagickDraw::polygon (array $coordinates) o public GmagickDraw GmagickDraw::polyline (array $coordinate_array) o public GmagickDraw GmagickDraw::rectangle (float $x1, float $y1, float $x2, float $y2) o public GmagickDraw GmagickDraw::rotate (float $degrees) o public GmagickDraw GmagickDraw::roundrectangle (float $x1, float $y1, float $x2, float $y2, float $rx, float $ry) o public GmagickDraw GmagickDraw::scale (float $x, float $y) o public GmagickDraw GmagickDraw::setfillcolor (string $color) o public GmagickDraw GmagickDraw::setfillopacity (float $fill_opacity) o public GmagickDraw GmagickDraw::setfont (string $font) o public GmagickDraw GmagickDraw::setfontsize (float $pointsize) o public GmagickDraw GmagickDraw::setfontstyle (int $style) o public GmagickDraw GmagickDraw::setfontweight (int $weight) o public GmagickDraw GmagickDraw::setstrokecolor (GmagickPixel $color) o public GmagickDraw GmagickDraw::setstrokeopacity (float $stroke_opacity) o public GmagickDraw GmagickDraw::setstrokewidth (float $width) o public GmagickDraw GmagickDraw::settextdecoration (int $decoration) o public GmagickDraw GmagickDraw::settextencoding (string $encoding) PHP Documentation Group GMAGICKDRAW(3)
All times are GMT -4. The time now is 02:36 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy