Passing pointer argument to function error?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dissectcode
    New Member
    • Jul 2008
    • 66

    Passing pointer argument to function error?

    Hello - Isn't this right? :

    Code:
    void Function(int *d, const int c, int n);
    
    void Function(int *d, const int c, int n)
    {
       while(n) { *(d++) = c; }
    }
    
    Function(&var1, 0, sizeof(thing));
    Why do I get a warning : "passing arg1 of "Function" from incompatible pointer type" ??

    thanks!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    What is the type of 'var1'?

    kind regards,

    Jos

    Comment

    • dissectcode
      New Member
      • Jul 2008
      • 66

      #3
      Originally posted by JosAH
      What is the type of 'var1'?

      kind regards,

      Jos

      oh - var1 is a structure....

      Code:
      typedef struct st_var
      {
         Athing arr[max];
         int limit1;
         int limit 2;
      } Var;
      
      Var var1;
      please advise..
      thank you.

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        Hi,
        I don't get it. How can you pass a value of type st_var* into an argument of type int*? Is that really what you want to do? I would complain too, if I were the compiler. If this function has to work with any type, you could use a void*, but I don't think you can increment those. Why is the parameter of type int*?

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          boxfish is right you can't increment points of type void *. To be able to increment a pointer to a type you have to know the types size and by definition the size of void is 0.

          This looks to me like a self implementation of the library function memset. Assuming you can't use memset then you need to use void * as memset does and boxfish suggests. You will then be able to pass a pointer to anything to your function.

          Then internally to the function copy the void * to a char * variable and use that in you loop.

          Comment

          • arnaudk
            Contributor
            • Sep 2007
            • 425

            #6
            OP: Even when you get your types right, that Function will either run forever if n is non-zero or do strictly nothing otherwise, the only exception being if c=0 and the address of n is eventually reached by incrementing d. I'm not sure this is what you intended.

            Comment

            Working...