Passing values to an function

Cartoondog

I am missing a fundamental point on passing values.

In my code, I wrote this prototype/function:

void drawFont (char A[],unsigned char length, char x1, char y1, uint16 FGcolor);

I call the function using a call like this:

drawFont ("William",7,15,25,YEL,0);

or

drawFont ("W",1,15,25,YEL,0);

Both of these work fine in the code. If I examine A[0] in the function, I will see a value of '57' representing an ASCII 'W'. All fine and good.

My question/problem is: When I attempt to replicate the 'W' using the ASCII value instead of the string representation, my code fails Example:

drawFont (57,1,15,25,YEL,0);

The value of A[0] in the code is: 0, but the address of A is 57. So somehow the compiler assumes that I want to pass a pointer? I'm confused, why did it pass the string fine, but not the value?

Thanks in advance from a novice C programmer.

Mark Tolonen

The prototype for your function declares that the first parameter is a pointer to a character:

void drawFont (char A[],...);

If you pass an integer, you are breaking the rules. The compiler should warn about it. Mine gives a warning about the following code on the line with func(57):

void func(char a[])
{
}

char arr[] = {87,84,70};

int main()
{
    func(57);
    func("W");
    func(arr);
}
x.c(9) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int'
x.c(9) : warning C4024: 'func' : different types for formal and actual parameter 1

If you look at the assembly output from the compiler, you can see the problem more clearly (I cut out the uninteresting parts):

PUBLIC  _arr
_DATA   SEGMENT
_arr    DB      057H                ;Here's the char array at an address in memory.
        DB      054H
        DB      046H
        ORG $+1
$SG1297 DB      'W', 00H   ;Here's the "W" at an address in memory (nul-terminated)
_DATA   ENDS

; 9    :     func(57);

  00003 6a 39            push    57                  ; Push 57 as the address, WRONG!
  00005 e8 00 00 00 00   call    _func
  0000a 83 c4 04         add     esp, 4

; 10   :     func("W");

  0000d 68 00 00 00 00   push    OFFSET $SG1297      ; Push the address of the string.
  00012 e8 00 00 00 00   call    _func
  00017 83 c4 04         add     esp, 4

; 11   :     func(arr);

  0001a 68 00 00 00 00   push    OFFSET _arr         ; Push the address of the array.
  0001f e8 00 00 00 00   call    _func
  00024 83 c4 04         add     esp, 4

The code is expecting to go to an address to read characters. Who knows what is in address 57.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing bool values to function

From Dev

Passing values to an function

From Dev

Passing in values into a jquery function with a # at the beginning

From Dev

Passing values by reference into void function

From Dev

Passing list of values as parameters to a function in R

From Dev

Getting different values by passing Pointers to a function in Go

From Dev

Passing values into a function via JQuery Onclick

From Dev

Passing array of vectors as a function parameter (values will not change)

From Dev

Function pointer array, passing values defined in array

From Dev

Python Passing values to a return statement function

From Dev

C Student Assignment, Array Values Not Passing to Function

From Dev

passing files and values as parameter to a function in python

From Dev

Passing an argument in the function to turn struct values into an array

From Dev

Passing multiple values to SUMIFS function in Excel

From Dev

Syntax for passing values to a PL/pgSQL function

From Dev

Passing from values to a php function on post

From Dev

Passing a dictionary or its keys as values to a function

From Dev

Passing an array to a function changes its values?

From Dev

Passing table values as function arguments in Lua 4 without the "call" function

From Dev

Passing 2 values to a js function from an HTML form using $_POST

From Dev

Angularjs passing parameter values in ng-click function

From Dev

Passing values to a dynamically created function call inside a loop

From Dev

Passing array of vectors as a function parameter to change the original values

From Dev

Python: Passing the default values of function' arguments to *args or **kwargs

From Dev

pagination in codeigniter and passing values to a controller function but pagination not working

From Dev

Syntax error while passing values in alloy script function

From Dev

Passing values to a dynamically created function call inside a loop

From Dev

Python: Substitute variable values before passing to another function

From Dev

R: Passing variable values to function to determine range in rowSums

Related Related

  1. 1

    Passing bool values to function

  2. 2

    Passing values to an function

  3. 3

    Passing in values into a jquery function with a # at the beginning

  4. 4

    Passing values by reference into void function

  5. 5

    Passing list of values as parameters to a function in R

  6. 6

    Getting different values by passing Pointers to a function in Go

  7. 7

    Passing values into a function via JQuery Onclick

  8. 8

    Passing array of vectors as a function parameter (values will not change)

  9. 9

    Function pointer array, passing values defined in array

  10. 10

    Python Passing values to a return statement function

  11. 11

    C Student Assignment, Array Values Not Passing to Function

  12. 12

    passing files and values as parameter to a function in python

  13. 13

    Passing an argument in the function to turn struct values into an array

  14. 14

    Passing multiple values to SUMIFS function in Excel

  15. 15

    Syntax for passing values to a PL/pgSQL function

  16. 16

    Passing from values to a php function on post

  17. 17

    Passing a dictionary or its keys as values to a function

  18. 18

    Passing an array to a function changes its values?

  19. 19

    Passing table values as function arguments in Lua 4 without the "call" function

  20. 20

    Passing 2 values to a js function from an HTML form using $_POST

  21. 21

    Angularjs passing parameter values in ng-click function

  22. 22

    Passing values to a dynamically created function call inside a loop

  23. 23

    Passing array of vectors as a function parameter to change the original values

  24. 24

    Python: Passing the default values of function' arguments to *args or **kwargs

  25. 25

    pagination in codeigniter and passing values to a controller function but pagination not working

  26. 26

    Syntax error while passing values in alloy script function

  27. 27

    Passing values to a dynamically created function call inside a loop

  28. 28

    Python: Substitute variable values before passing to another function

  29. 29

    R: Passing variable values to function to determine range in rowSums

HotTag

Archive