Results 1 to 8 of 8

Thread: Parameter function and generic pointer

  1. #1
    Join Date
    Aug 2010
    Posts
    55

    Parameter function and generic pointer

    Hello,
    I would like to pass a parameter to a function variable can be of different type. I thought C was the generic type void *?

    Simply put, a small example:
    Code:
    # <stdio.h>
    void display (void* to, void* z) (
      *x = 42;
      *z = *x * 0.1;
      printf("% s\ n% ft\ n", *x, *z);
    )
    
    void hand (void) (
      struct str (
        int temp,
        float ft;
      )
      struct str s,
      s.temp = 0;
      s.ft = 0.0;
      printf("% s\ n% ft\ n", s.temp, s.ft)
      displays (&s.temp, &s.ft);
    )
    But it does not compile, is this a compile version problem?

  2. #2
    Join Date
    Apr 2008
    Posts
    264

    Re: Parameter function and generic pointer

    Have you tried this
    Code:
    * (Int *) a = 42;
    * (Float *) * b = (int *) a * 0.1;
    You will notice that you loose any genericity parameters.

  3. #3
    Join Date
    Nov 2009
    Posts
    446

    Re: Parameter function and generic pointer

    Have you casted your variables
    Code:
    void display (void* x, void* b) (
    	int *pmr = x,
    	float *pb = b;
    	*pmr = 42;
    	*bp = *pmr * 0.1,
    	printf("% s\ n% f\ n", *pmr, *pb);
    )

  4. #4
    Join Date
    Nov 2009
    Posts
    343

    Re: Parameter function and generic pointer

    Well it's the same for malloc, there is an implicit cast :
    Code:
    double *p = malloc(sizeof(*p)) / / cast to double
    If you guard pointer void * malloc returns that you you can not use the allocated memory.

  5. #5
    Join Date
    Mar 2010
    Posts
    197

    Re: Parameter function and generic pointer

    You said you do not know the type of a and b and it may change as the function call. Yet you treat these variables as int and float as you make a call to printf with matching trainers. In C, you can not apply the values of generic pointers if you do not make the cast. Indeed, it can indicate a value of applying such a way (if you cast an int value 42 to bring to your variable operations performed by the processor will not be the same as if you were cast in float). If you want to be able to pass any type of variables according to your view, then we will do the necessary to determine the type (additional parameters, use of structure value to associate with type.

  6. #6
    Join Date
    Aug 2010
    Posts
    33

    Re: Parameter function and generic pointer

    For printf, it was just works for me. In my "real" code, I do not printf, I want to assign a value to a variable passed as parameter. This value is either an int, a float ... and it is the same type as the variable parameter.

  7. #7
    Join Date
    Aug 2010
    Posts
    34

    Re: Parameter function and generic pointer

    It must explicitly cast to tell the compiler how process and work with this memory address.

  8. #8
    Join Date
    Aug 2010
    Posts
    21

    Re: Parameter function and generic pointer

    What I do not understand (if you can enlighten me) is that it gives me a memory address to the function (one variable) (hence my void *), and in this function that processing and posting, there is a cast for the implicit assignment?

Similar Threads

  1. Pointer parameter in a function
    By Chrisch in forum Software Development
    Replies: 4
    Last Post: 14-12-2009, 10:57 PM
  2. Matrix parameter function
    By Chrisch in forum Software Development
    Replies: 3
    Last Post: 02-12-2009, 11:07 AM
  3. Pointer of file in parameter
    By Wyvern in forum Software Development
    Replies: 4
    Last Post: 05-11-2009, 11:50 PM
  4. What is Generic Pointer?
    By Nihir in forum Software Development
    Replies: 3
    Last Post: 29-05-2009, 01:44 PM
  5. Exercise pointer and function in C
    By Bull50 in forum Software Development
    Replies: 3
    Last Post: 09-04-2009, 11:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,667,900.63525 seconds with 16 queries